Today's Question:  What are you most afraid of as a programmer?        GIVE A SHOUT

Technical Article => Web =>  JavaScript

Why 0.1+0.2 != 0.3

  sonic0002      2014-11-19 05:32:46      3,809    0    0

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

  SAVE AS PDF   MARK AS READ   MARK AS IMPORTANT

Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious

  RELATED


  0 COMMENT


No comment for this article.


  WRITE ARTICLE

git merge

By sonic0002