Analysis and solution of Javascript floating-point arithmetic precision problem

Source: Internet
Author: User
Tags arithmetic

  The precision of floating-point numbers is not JavaScript-specific, because some decimal places are infinite in binary notation

Analytics   JavaScript only has one numeric type number, and all the numbers in JavaScript are represented in IEEE-754 standard format. The precision problem with floating-point numbers is not JavaScript-specific, because some decimal digits are infinite in binary notation:   decimal           binary 0.1       &NB Sp      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 10 1001   So for example 1.1, its program actually can't really represent ' 1.1 ', but only to some extent accurate, which is unavoidable precision loss:   1.09999999999999999   in JavaScript The problem is more complicated, here only to some test data in Chrome:     input               Output 1.0-0.9 = 0.1   &NB Sp 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   & nbsp True   Resolve   How do you avoid this type of 1.0-0.9!= 0.1 bug-type problem? The following gives a more current solution, the result of the calculation of floating-point operations before the results of precision reduction, because in the process of narrowing the precision will always be automatically rounded:     code as follows: (1.0-0.9). toFixed (digits)                  //toFixed () The precision parameter shall be parsefloat between 0 and 20 (1.0-0.9). toFixed (10)) = = = 0.1  //result is true parsefloat (1.0-0.8). toFixed) = = 0.2  //result is true parsefloat ((1.0-0.7). toFixed (10)) = = 0 .3  //result is true parsefloat ((11.0-11.8). toFixed) = = -0.8  //result is true   method to refine the code as follows://To judge the value by means of the IsEqual tool is equality function isequal (number1, number2, digits) { digits = digits = undefined? 10:digits;//default precision is  return n umber1.tofixed (digits) = = number2.tofixed (digits); }   IsEqual (1.0-0.7, 0.3);  //return True  //native extension way, prefer object-oriented style Number.prototype.isEqual = function (number, digits) { digits = digit s = = UNdefined? 10:digits; The default precision is  return this.tofixed (digits) = = = Number.tofixed (digits); }   (1.0-0.7). IsEqual (0.3); return true  
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.