IEEE 754 floating point arithmetic rounding error in c# and javascript

前端 未结 4 1984
野性不改
野性不改 2020-12-06 21:21

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

4条回答
  •  我在风中等你
    2020-12-06 21:44

    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 (aka decimal) 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.

提交回复
热议问题