I just read a book about javascript. The author mentioned a floating point arithmetic rounding error in the IEEE 754 standard.
For example adding 0.1 and 0.2 yields 0.30000000000000004 instead of 0.3.
so (0.1 + 0.2) == 0.3 returns false.
I also reproduced this error in c#.
So these are my question is:
How often this error occurs?
What is the best practice workaround in c# and javascript?
Which other languages have the same error?
It’s not an error in the language. It’s not an error in IEEE 754. It’s an error in the expectation and usage of binary floating point numbers. Once you understand what binary floating point numbers really are, it makes perfect sense.
The best practice in C# is to use
System.Decimal(akadecimal) which is a decimal floating point type, whenever you’re dealing with quantities which are naturally expressed in decimal – typically currency values.See my articles on .NET binary floating point and decimal floating point for more information.