Analysis and Solution to the Javascript floating point operation _ javascript skills

Source: Internet
Author: User
JavaScript has only one numeric type Number, and all numbers in Javascript are represented in IEEE-754 standard format. The precision of floating point numbers is not unique to JavaScript, because some decimal places indicate that the digits are infinite decimal binary digits.
0.1 0.0001 1001 1001 1001...
0.2 0.0011 0011 0011 0011...
0.3 0.0100 1100 1100 1100...
0.4 0.0110 0110 0110 0110...
0.5 0.1
0.6 0.1001 1001 1001 1001...
Therefore, for example, in 1.1, the program cannot really represent '1. 1', but can only be accurate to a certain extent. This is an unavoidable loss of precision:

1.09999999999999999
The problem in JavaScript is more complicated. Here we only give some test data in Chrome:

Input and Output
1.0-0.9 = 0.1 False
1.0-0.8 = 0.2 False
1.0-0.7 = 0.3 False
1.0-0.6 = 0.4 True
1.0-0.5 = 0.5 True
1.0-0.4 = 0.6 True
1.0-0.3 = 0.7 True
1.0-0.2 = 0.8 True
1.0-0.1 = 0.9 True
Solution
How can we avoid this type of 1.0-0.9! = What happens to 0.1 of non-bug issues? The following provides a more widely used solution. The accuracy of the computing result is reduced before the floating point calculation result is judged, because the accuracy is automatically rounded down:

The Code is as follows:


(1.0-0.9). toFixed (digits) // The toFixed () precision parameter must be between 0 and 20
ParseFloat (1.0-0.9). toFixed (10) === 0.1 // The result is True.
ParseFloat (1.0-0.8). toFixed (10) === 0.2 // The result is True.
ParseFloat (1.0-0.7). toFixed (10) === 0.3 // The result is True.
ParseFloat (11.0-11.8). toFixed (10) ===- 0.8 // The result is True.


Method Extraction

The Code is as follows:


// Use the isEqual tool to determine whether the values are equal
Function isEqual (number1, number2, digits ){
Digits = undefined? 10: digits; // The default precision is 10.
Return number1.toFixed (digits) === number2.toFixed (digits );
}

IsEqual (1.0-0.7, 0.3); // return true

// Native extension method, prefer the object-oriented Style
Number. prototype. isEqual = function (number, digits ){
Digits = undefined? 10: digits; // The default precision is 10.
Return this. toFixed (digits) === number. toFixed (digits );
}

(1.0-0.7). isEqual (0.3); // return true

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.