5.1 * 100 != 510 🤯 , Why you have to be careful with Doubles and Floats !

Internally Integers and Floats are stored differently.
This is how integers are stored depending upon the machine the number of bits will be different. ( Normally it will be 32 or 64 bit)

By default most of the modern languages save a float/double


This content originally appeared on DEV Community and was authored by Shrihari Mohan

Internally Integers and Floats are stored differently.
This is how integers are stored depending upon the machine the number of bits will be different. ( Normally it will be 32 or 64 bit)

Integers Representation

By default most of the modern languages save a float/double
as double precision( 64 Bit) But for this blog I will show you a 32 bit model! For the same value 20 but 20.0 will be saved in the IEEE754 Double Precision Format.

Below is the image showing single precision format
32 Bit Single Precision

But why ?

This is because we can save integers easily in the binary form but not floats. Floats are special numbers and always require special care when dealing them especially if you're using them for calculations. This talks about Floats and Binary

5.1

We can see there are some errors during the conversion of this float to binary. This is why we get this title.

5.1 * 100

In our company we deal with a lot of transactions and taxes! Hence we always take care of floating points. Recently we found this exact bug where we missed to deal with floats, it cost us with wrong calculations. But we knew what was wrong immediately. Peace 🕊

If you are here it means you may have enjoyed reading this blog. Just follow me @shrihari which will motivate to write more and contribute open source and may considering a buttermilk 🥛.

If you want to receive these blogs in your mail from @Medium. Subscribe to my blogs

Shrihari

I write stuff on DevTo and Medium

favicon shrihari-portfolio.vercel.app

More Free Articles From me


This content originally appeared on DEV Community and was authored by Shrihari Mohan


Print Share Comment Cite Upload Translate Updates
APA

Shrihari Mohan | Sciencx (2022-07-23T10:26:53+00:00) 5.1 * 100 != 510 🤯 , Why you have to be careful with Doubles and Floats !. Retrieved from https://www.scien.cx/2022/07/23/5-1-100-510-%f0%9f%a4%af-why-you-have-to-be-careful-with-doubles-and-floats/

MLA
" » 5.1 * 100 != 510 🤯 , Why you have to be careful with Doubles and Floats !." Shrihari Mohan | Sciencx - Saturday July 23, 2022, https://www.scien.cx/2022/07/23/5-1-100-510-%f0%9f%a4%af-why-you-have-to-be-careful-with-doubles-and-floats/
HARVARD
Shrihari Mohan | Sciencx Saturday July 23, 2022 » 5.1 * 100 != 510 🤯 , Why you have to be careful with Doubles and Floats !., viewed ,<https://www.scien.cx/2022/07/23/5-1-100-510-%f0%9f%a4%af-why-you-have-to-be-careful-with-doubles-and-floats/>
VANCOUVER
Shrihari Mohan | Sciencx - » 5.1 * 100 != 510 🤯 , Why you have to be careful with Doubles and Floats !. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/23/5-1-100-510-%f0%9f%a4%af-why-you-have-to-be-careful-with-doubles-and-floats/
CHICAGO
" » 5.1 * 100 != 510 🤯 , Why you have to be careful with Doubles and Floats !." Shrihari Mohan | Sciencx - Accessed . https://www.scien.cx/2022/07/23/5-1-100-510-%f0%9f%a4%af-why-you-have-to-be-careful-with-doubles-and-floats/
IEEE
" » 5.1 * 100 != 510 🤯 , Why you have to be careful with Doubles and Floats !." Shrihari Mohan | Sciencx [Online]. Available: https://www.scien.cx/2022/07/23/5-1-100-510-%f0%9f%a4%af-why-you-have-to-be-careful-with-doubles-and-floats/. [Accessed: ]
rf:citation
» 5.1 * 100 != 510 🤯 , Why you have to be careful with Doubles and Floats ! | Shrihari Mohan | Sciencx | https://www.scien.cx/2022/07/23/5-1-100-510-%f0%9f%a4%af-why-you-have-to-be-careful-with-doubles-and-floats/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.