DEV Community

Cover image for Decoding Why 0.6 + 0.3 = 0.8999999999999999 in JS and How to Solve?

Decoding Why 0.6 + 0.3 = 0.8999999999999999 in JS and How to Solve?

Jeeva Ramanathan on November 16, 2023

Understanding IEEE 754 Floating-Point Numbers and also exploring how 0.1+0.2 is 0.30000000000000004 step-by-step While working on an o...
Collapse
 
bbkr profile image
Paweł bbkr Pabian

Great explanation of IEEE754.

However for all precision-sensitive calculations I recommend Raku, because it automatically does fraction rationals. So if you write 0.12824 it will be represented as 1603/12500 internally:

$ raku -e 'say 0.12824.nude'
(1603 12500)
Enter fullscreen mode Exit fullscreen mode

Fraction calculations will always be performed using integer numerator and denominator, therefore will always be precise:

$ perl -E 'say 0.7 - 0.6 - 0.1'
-2.77555756156289e-17  # FAIL

$ python -c 'print(0.7 - 0.6 - 0.1)'
-2.7755575615628914e-17 # FAIL

$ raku -e 'say 0.7 - 0.6 - 0.1'
0 # CORRECT!
Enter fullscreen mode Exit fullscreen mode

As a bonus it also handles big nums out of the box. Worth trying.

Collapse
 
jeevaramanathan profile image
Jeeva Ramanathan

Interesting! Thanks for adding

Collapse
 
lnahrf profile image
Lev Nahar • Edited

Great article and solution for an issue not a lot of modern programmers know about (especially problematic when dealing with finances). Just one thing - I thought it is worth mentioning that .toFixed() in Javascript is a purely cosmetic method, it should only be used for display purposes since (like you mentioned) it returns the string representation of the decimal.

Collapse
 
jeevaramanathan profile image
Jeeva Ramanathan

Thanks for your comment! You're correct that .toFixed() is like a cosmetic method as it only affects the way the number is displayed, not its actual value.

Collapse
 
tqbit profile image
tq-bit

I didn't know.toPrecision before. Thanks for sharing, you wrote a great article there

Collapse
 
jeevaramanathan profile image
Jeeva Ramanathan

Thanks! Glad this helped you discover toPrecision().

Collapse
 
hassan_dev91 profile image
Hassan

That was so awesome :)

Collapse
 
jeevaramanathan profile image
Jeeva Ramanathan

Thank you for your feedback.

Collapse
 
manchicken profile image
Mike Stemle

Well done, friend.

Collapse
 
jeevaramanathan profile image
Jeeva Ramanathan

Thanks, friend!

Collapse
 
an_vo87376 profile image
An Võ

like

Collapse
 
jeevaramanathan profile image
Jeeva Ramanathan • Edited

🙌🏻

Collapse
 
citronbrick profile image
CitronBrick

You can enable syntax highlighting in Dev.to using

triple_backtick javascript
code
triple_backtick

Collapse
 
jeevaramanathan profile image
Jeeva Ramanathan

Yeah, Sure🙌🏻

Collapse
 
samirmishra27 profile image
Samir

One of the best in-depth technical articles I've read so far. I'm sure not many JS enthusiasts will read this or even care about the intricacies of it So I'm lucky to have read it!
Is it just JavaScript or other languages also use the same IEEE 754 standard?

Collapse
 
jeevaramanathan profile image
Jeeva Ramanathan • Edited

Glad to hear that!
The IEEE 754 standard is not exclusive to JavaScript; Many programming languages like Java, Python etc., use this standard for representing floating-point numbers.

Collapse
 
samirmishra27 profile image
Samir

Surprising! I tried this exact same expression on Java, Rust, Python and all of them yielded the same results.
I suppose they all have their own libraries to deal with this inconsistency.
Also, do you know that JavaScript loses number precision after like 18 digits? Could this be the same reason? Or are there different causes for that?