Analysis and Solution of JavaScript floating point operation

Source: Internet
Author: User
Analysis

Javascript has only one numeric typeNumberAnd all numbers in JavaScript are represented in IEEE-754 standard format. The precision of floating point numbers is not unique to Javascript, because some decimals represent infinite digits in binary format:

 
Decimal binary 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.10.6 0.1001 1001 1001 1001...

So for example1.1, ItsProgramIn fact, it 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 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
Solution

So how can we avoid this type1.0-0.9! = 0.1What happens to 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:

 
(1.0-0.9 ). tofixed (digits) // The tofixed () precision parameter must be between 0 and 20, and the returned data type is stringparsefloat (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.
Method Extraction
  // 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  
  
reprint: http://madscript.com/javascript/javscript-float-number-compute-problem/

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.