Solution to JS floating point operation bug

Source: Internet
Author: User

I. Events

Recently, the project management fee was written in JS on the client. A major problem was found, such as 0.7*8.1 = 5.6699999999, 10.3-9.2 = 100000000014. Why are these problems? Is it a bug in JS's floating point operations? What should I do?

2. Response

"Baidu cannot be asked in case of internal affairs, but Google cannot be asked in case of foreign affairs" ah, I searched for information online, confirmed my thoughts, and found a solution. At the same time, I have my own solutions.

1. The online solution is to overwrite the floating point operations,CodeAs follows: (reproduced)

Code
// Division function, used to obtain accurate division results
// Note: There will be an error in the division result of JavaScript, which will be obvious when two floating point numbers are separated. This function returns a more precise division result.
// Call: accdiv (arg1, arg2)
// Returned value: the exact result of dividing arg1 by arg2
Function Accdiv (arg1, arg2 ){
VaR T1 = 0 , T2 = 0 , R1, R2;
Try {T1 = Arg1.tostring (). Split ( " . " )[ 1 ]. Length} Catch (E ){}
Try {T2 = Arg2.tostring (). Split ( " . " )[ 1 ]. Length} Catch (E ){}
With (Math ){
R1 = Number (arg1.tostring (). Replace ( " . " , "" ));
R2 = Number (arg2.tostring (). Replace ( " . " , "" ));
Return (R1 / R2) * POW (10, t2-t1 );
}
}
// Add a div Method to the number type to facilitate calling.
Number. Prototype. Div =   Function (ARG ){
Return Accdiv ( This , ARG );
};
// Multiplication function, used to obtain accurate multiplication results
// Note: There is an error in the Javascript multiplication result, which is obvious when two floating point numbers are multiplied. This function returns a more accurate multiplication result.
// Call: accmul (arg1, arg2)
// Returned value: exact result of multiplying arg1 by arg2
Function Accmul (arg1, arg2)
{
VaR M = 0 , S1 = Arg1.tostring (), S2 = Arg2.tostring ();
Try {M + = S1.split ( " . " )[ 1 ]. Length} Catch (E ){}
Try {M + = S2.split ( " . " )[ 1 ]. Length} Catch (E ){}
Return Number (s1.replace ( " . " , "" )) * Number (s2.replace ( " . " , "" )) / Math. Pow (10, M );
}
// Add a mul Method to the number type to facilitate calling.
Number. Prototype. Mul =   Function (ARG ){
Return Accmul (ARG, This );
};
// The addition function is used to obtain accurate addition results.
// Note: The addition result of JavaScript has an error, which is obvious when two floating point numbers are added. This function returns a more accurate addition result.
// Call: accadd (arg1, arg2)
// Returned value: Exact Results of adding arg2 to arg1
Function Accadd (arg1, arg2 ){
VaR R1, R2, M;
Try {R1 = Arg1.tostring (). Split ( " . " )[ 1 ]. Length} Catch (E) {r1 = 0 }
Try {R2 = Arg2.tostring (). Split ( " . " )[ 1 ]. Length} Catch (E) {r2 = 0 }
M = Math. Pow ( 10 , Math. Max (R1, R2 ));
Return (Arg1 * M + Arg2 * M) / M;
}
// Add an add method to the number type to facilitate calling.
Number. Prototype. Add =   Function (ARG ){
Return Accadd (ARG, This );
}
// Subtraction Functions
Function Accsub (arg1, arg2 ){
VaR R1, R2, M, N;
Try {R1 = Arg1.tostring (). Split ( " . " )[ 1 ]. Length} Catch (E) {r1 = 0 }
Try {R2 = Arg2.tostring (). Split ( " . " )[ 1 ]. Length} Catch (E) {r2 = 0 }
M = Math. Pow ( 10 , Math. Max (R1, R2 ));
// Last modify by deeka
// Dynamic control precision length
N = (R1 > = R2) ? R1: R2;
Return (Arg2 * M - Arg1 * M) / M). tofixed (N );
}
// /Add a sub method to the number class to facilitate calling.
Number. Prototype. sub =   Function (ARG ){
Return Accsub (ARG, This );
}

In this way, if you want to calculate 0.7*8.1, you only need to call the Mul method (0.7). Mul (8.1) to get an accurate answer.

2. My Solutions

This is a good method. However, I want to convert the floating point type into an integer and then perform an operation. For example, 0.7*8.1, we can calculate (0.7*10) * (8.1*10)/100, and the result is 5.67, obviously. Through project practice, we can get accurate results.

Iii. Summary:

1. Do not use JavaScript for complex operations, especially floating point operations.

2. If you must perform a floating point number operation, convert the floating point number to an integer before performing the operation.

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.