JS floating point calculation problem

Source: Internet
Author: User
Tags mul pow

Problem

Using JS to calculate floating-point numbers, the results may "exceed expectations", most of the results are still right, but we do not want to calculate such a rigorous thing and unexpected surprises. Like what:

    • 0.3 + 0.6 = 0.8999999999999999
    • 0.3-0.2 = 0.09999999999999998
    • 0.3 * 1.5 = 0.44999999999999996
    • 0.3/0.1 = 2.9999999999999996

After reading the results of these calculations, if you have not used JS, you may be a bit of a crash. I can only say that this is the charm of JS.

Analysis

Before you do this, you need to know the following points:

    • The number type in JS only numbers;
    • JS number is the IEEE 754 standard 64-bits double-precision value

There are many explanations of this problem on the Internet, because the computer uses binary to store and process the numbers, it can't express the floating-point number accurately, and there is no corresponding encapsulation class to handle the floating-point arithmetic in JS, the computation precision can be lost by direct calculation. In fact, the high-level language (C#,java) also exists in this problem, but they do their own internal processing, the accuracy of the difference is masked out. Some decimals convert to bits number is infinite (there is a loop), but the 64-bit small number is only 52 bits, so the equivalent of more than the number of bits is intercepted, resulting in the loss of precision. This address can be used for floating point and IEEE 754 standard 64-bits (behind binary conversion), with this we will verify the next 0.3-0.2.

    • 0.3 after conversion to 0.299999999999999988897769753748
    • 0.2 after conversion to 0.200000000000000011102230246252
    • 0.299999999999999988897769753748-0.200000000000000011102230246252=0.099999999999999977795539507496

This and JS direct calculation results 0.09999999999999998 want to match.

Analysis down, and finally understand that not JS self-development, but not in time to replenish nutrition, we can only think of another way out.

Workaround

There are a lot of solutions on the Internet, I do not have a special method here, but there are a lot of methods on the Internet only half, there are still bugs. The idea of most of the solutions is to convert floating-point calculations to integer calculations, and the integer calculation is of course no bug. In front of the online part of the method only make the right half, the right half is the multiplication method, add and subtract still have problems, because the addition and subtraction there is also a direct operation of floating point numbers.

Attached: code with no bugs.

functionAdd (A, b) {varC, D, E; Try{C= A.tostring (). Split (".") [1].length; } Catch(f) {C= 0; }    Try{d= B.tostring (). Split (".") [1].length; } Catch(f) {d= 0; }    returnE = Math.pow (Math.max (c, D)), (Mul (A, E) + mul (b, E))/e;}functionSub (A, b) {varC, D, E; Try{C= A.tostring (). Split (".") [1].length; } Catch(f) {C= 0; }    Try{d= B.tostring (). Split (".") [1].length; } Catch(f) {d= 0; }    returnE = Math.pow (Math.max (c, D)), (Mul (A, E)-Mul (b, E))/e;}functionMul (A, b) {varc = 0, D=a.tostring (), E=b.tostring (); Try{C+ = D.split (".") [1].length; } Catch(f) {}Try{C+ = E.split (".") [1].length; } Catch(f) {}returnNumber (D.replace (".", "")) * Number (E.replace (".", ""))/Math.pow (10, c);}functionDiv (A, b) {varC, d, e = 0, F= 0; Try{e= A.tostring (). Split (".") [1].length; } Catch(g) {}Try{f= B.tostring (). Split (".") [1].length; } Catch(g) {}returnc = Number (a.tostring (). Replace (".", "")), d = number (b.tostring (). Replace (".", ""), Mul (C/D, Math.pow (F-e));}

JS floating point calculation 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.