Analysis and Solution to the precision of Javascript floating-point operations

Source: Internet
Author: User

The precision of floating point numbers is not unique to JavaScript, because some decimals represent infinite digits in binary format.

Analysis 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 in JavaScript, because some decimal places indicate that the number of digits is infinite: decimal binary 0.1 0.0001 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.10.6 0.1001 1001 1001 1001... therefore, for example, 1.1, the program cannot really represent '1. 1 ', but can only achieve a certain degree of accuracy, this is an unavoidable loss of precision: 1.09999999999999999 in JavaScript problems are more complex, here only some test data in Chrome: input and Output 1.0-0.9 = 0.1 False1.0-0.8 = 0.2 False1.0-0.7 = 0.3 False1.0-0.6 = 0.4 True1.0-0.5 = 0.5 True1.0-0.4 = 0.6 True1.0-0.3 = 0.7 True1.0-0.2 = 0.8 True1.0-0.1 = 0.9 True how to avoid such 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) // toFixed () The precision parameter must be between 0 and 20 parseFloat (1.0-0.9 ). toFixed (10) = 0.1 // The result is TrueparseFloat (1.0-0.8 ). toFixed (10) = 0.2 // The result is TrueparseFloat (1.0-0.7 ). toFixed (10) = 0.3 // The result is TrueparseFloat (11.0-11.8 ). toFixed (10) ===- 0.8 // The result is True. The extracted 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 mode, 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.