1 + 2 = 3, then why doesn’t 0.1 + 0.2 = 0.3 in JavaScript? The answer has to do with computer science and floating point math.

If you have never done so, I would encourage you to open your browser’s console and enter 0.1 + 0.2 to check out the result.

No, you do not need to adjust your browser — this is actually how it is supposed to work, according to the ECMAScript standard that defines the JavaScript programming language:

“The Number type has exactly 18437736874454810627 (that is, 2^64 - 2^53 + 3) values, representing the double-precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic” —ECMAScript Language Specification

JavaScript represents numeric values using the number primitive type, and all JavaScript numbers are actually floating point values — even integers.

The key point here is that JavaScript implements the IEEE Standard for floating point arithmetic. Let’s see what that means.

#javascript #programming

Why 0.1 + 0.2 ≠ 0.3 in JavaScript
6.85 GEEK