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)
Fraction calculations will always be performed using integer numerator and denominator, therefore will always be precise:
I'm a selftaught (web) developer. On sunny days, you can find me hiking through the Teutoburg Forest, on rainy days coding or with a good fiction novel in hand.
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.
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?
We're a place where coders share, stay up-to-date and grow their careers.
Great explanation of IEEE754.
However for all precision-sensitive calculations I recommend Raku, because it automatically does fraction rationals. So if you write
0.12824it will be represented as1603/12500internally: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
I didn't know.toPrecision before. Thanks for sharing, you wrote a great article there
Thanks! Glad this helped you discover toPrecision().
That was so awesome :)
Thank you for your feedback.
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.like
🙌🏻
Thanks, friend!
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?