5

A lot has been said on the topic, however I could not find the exact answer to my question.

JavaScript cannot accurately represent decimal numbers such as 0.1, and this is understandable.

For example this is true because of the rounding error that occurs during the multilication:

0.1 * 3 === 0.30000000000000004

This is fine - all according to the IEEE Standard for Floating-Point Arithmetic (IEEE 754).

What I cannot understand is why other languages that also use the standard give more accurate measurements

0.1 * 3 === 0.3

Is this because of the different roundng rules that they use? https://en.wikipedia.org/wiki/IEEE_floating_point#Rounding_rules or am I missing something?

Elenchev
  • 308
  • 1
  • 8
  • What other language(s) are you talking about specifically? Can you give examples? – Bergi Feb 13 '16 at 13:56
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Muthu Kumaran Jul 25 '17 at 06:44

1 Answers1

2

Any language math that uses IEEE 754 Floating Point will have the same rounding issues that you see in JavaScript.

Some languages, such as C# provide a Decimal type which has more accurate math handling, trading decimal precision for range.

In JavaScript there are some different floating point libraries, such as BigDecimal, but since they aren't baked into the language, you can't use the regular math operators, such as + and *.

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74