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...
For further actions, you may consider blocking this person and/or reporting abuse
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 as1603/12500
internally:Fraction calculations will always be performed using integer numerator and denominator, therefore will always be precise:
As a bonus it also handles big nums out of the box. Worth trying.
Interesting! Thanks for adding
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.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.I didn't know.toPrecision before. Thanks for sharing, you wrote a great article there
Thanks! Glad this helped you discover toPrecision().
Well done, friend.
Thanks, friend!
like
🙌🏻
That was so awesome :)
Thank you for your feedback.
You can enable syntax highlighting in Dev.to using
triple_backtick javascript
code
triple_backtick
Yeah, Sure🙌🏻
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?
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.
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?