How to solve 0.1+0.2 in JavaScript is not equal to 0.3

Source: Internet
Author: User
Tags abs pow

  

Console.log (0.1+0.2===0.3)//  true  or  false??

In normal mathematical logic, the logic of 0.1+0.2=0.3 is correct, but in JavaScript 0.1+0.2! ==0.3, what is this for? This question is also occasionally used as a face test to examine the interviewer's

The degree to which JavaScript values are understood.

Binary floating-point numbers 0.1 and 0.2 in JavaScript are not very precise, and the result of their addition is not exactly equal to 0.3, but rather a closer number of 0.30000000000000004, so the condition evaluates to False.

So what should be done to solve 0.1+0.2 equals 0.3? The best way to do this is to set an error range value, often referred to as "machine precision," and for JavaScript, this value is usually 2^-52, and in ES6, we have provided such a

Property: Number.epsilon, and this value is equal to 2^-52. This value is very, very small, and the underlying computer has already worked for us and is infinitely close to 0, but not equal to 0. This time we just have to judge (0.1+0.2)-0.3 less than

Number.epsilon, the 0.1+0.2===0.3 is determined to be true within the range of this error.

  

    function Numbersequal (A, b        ) {return Math.Abs (a) <Number.epsilon;    }         var a=0.1+0.2, b=0.3;    Console.log (Numbersequal (A, b)  ); // true

But here to consider the issue of compatibility, in Chrome support this property, but IE does not support (damn ie, the author's version is IE10 incompatible), so we also have to solve the incompatibility problem of IE.

 

Number.epsilon= (function(){//Resolving compatibility issues                returnNumber.epsilon? Number.EPSILON:Math.pow (2,-52); })();//above is a self-calling function, when the JS file is just loaded into memory, it will be judged and return a result, compared to if (! Number.epsilon) {   //Number.epsilon=math.pow (2,-52);    //This code is more cost-efficient and more aesthetically pleasing. functionNumbersequal (A, b) {returnMath.Abs (A-B) <Number.epsilon; }//Then we'll judge .        vara=0.1+0.2, b=0.3;  Console.log (Numbersequal (A, b)); //this is true.   

  

How to solve 0.1+0.2 in JavaScript is not equal to 0.3

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.