Why 0.1+0.2 != 0.3

Summary

Floating-point arithmetic in languages like JavaScript, C++, and Java often produces unexpected results, such as 0.1 + 0.2 not equaling 0.3. This behavior stems from the IEEE 754 standard, which dictates how numbers are represented in binary. Due to finite memory, like JavaScript's 64-bit representation with a 52-bit mantissa, decimal fractions like 0.1 cannot be precisely represented in binary, leading to rounding errors. Developers must be cautious with float comparisons, typically addressing this by checking if the absolute difference between two numbers falls within a small epsilon range, or by using precision-setting methods like toPrecision or toFixed for string comparisons in JavaScript.

In programming languages such as JavaScript, c/c++, Java and Matlab, you will find that you will get unexpected result when doing float point calculation. For example, when calculating 0.1 + 0.1, you will not get 0.3:

> 0.1 + 0.2 == 0.3
false

> 0.1 + 0.2
0.30000000000000004

Don't be surprised of this result, this is the end result of IEEE 754 standard, float point number cannot be accurately represented according to IEEE 754 because:

  • No enough memory is allocated for representing the number
  • It needs a range to ensure the accuracy

JavaScript uses 64-bit floating point representation, which is the same as Java's double.A float number consists of three components : sign bit+exponent+mantissa. The number 1/10 can be expressed as 0.1 in decimal, but it is 0.0001100110011001100110011001100110011001100110011001….. in binary. Because there s only 52 bit significant number, starting from bit 53, the number will be rounded. 

You need to be very careful when handling float point number calculations, especially when you do float point number comparisons for equality. How to do comparisons of float point number if you have to? There are two solutions:

1. Compare whether the subtraction of the two numbers is in a range

x = 0.2;
y = 0.3;
equal = (Math.abs(x - y) < 0.000001)

2. Use toPrecision or toFixed in JavaScript

(0.1 + 0.2).toPrecision(10) == 0.3
> true

(0.1 + 0.2).toFixed(10) == 0.3
> true

Reference : http://ourjs.com/detail/javascript%E7%9A%84%E8%AE%BE%E8%AE%A1%E7%BC%BA%E9%99%B7-%E6%B5%AE%E7%82%B9%E8%BF%90%E7%AE%97-0-1-0-2-0-3

JAVASCRIPT FLOAT POINT COMPARISON

  RELATED

  COMMENTS

0

No comment for this article.