JS small number of difference, compare size

Source: Internet
Author: User

         varA =0.3-0.2; varb = 0.4-0.3; Alert (A+"&"+b); if(A = =b) {alert ("true"); } Else{alert ("false"); }

0.09999999999999998&0.10000000000000003

The result is false

Avascript has only one numeric type number, and all the numbers in JavaScript arerepresented in the ieee-754 standard format. The accuracy of floating-point numbers is not specific to JavaScript, because some decimal places are infinite in binary notation: When calculating decimal in Java and JavaScript, decimal decimals are first converted to the corresponding binary, and some decimals are not fully converted to binary. This is where the first error occurs. After the decimal is converted to binary, the binary operation is performed, and the binary result is obtained. The binary results are then converted to decimal, where the second error is usually present. To avoid this, you can usually enlarge the integer multiples of the same 10 at the same time, and after the calculation is complete, the integer multiples added before being removed. 
decimal binary binary0.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...

JS small number of difference, compare size

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.