How to solve the floating point computing BUG in js _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to solve the floating point computing BUG in js. If you need it, you can refer to it and hope to help you with projects that have been used in the past, I found this code on the Internet before, but there will still be bugs in division and addition operations under specific conditions. I am slightly optimized.

The Code is as follows:


// Division function, used to obtain accurate division results
// Note: the division result of javascript has an error, which is obvious when two floating point numbers are separated. This function returns a more precise division result.
// Call: accDiv (arg1, arg2)
// Return 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 accMul (r1/r2), pow (10, t2-t1 ));
}
}


The Code is as follows:


// 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)
// Return value: the 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)
}


The Code is as follows:


// Intermediate solution for addition operations
Function accAdd (arg1, arg2 ){
Var r1, r2, m, c;
Try {r1 = arg1.toString (). split (".") [1]. length} catch (e) {r1 = 0}
Try {r2 = arg2.toString (). split (".") [1]. length} catch (e) {r2 = 0}
C = Math. abs (r1-r2 );
M = Math. pow (10, Math. max (r1, r2 ))
If (c> 0 ){
Var cm = Math. pow (10, c );
If (r1> r2 ){
Arg1 = Number (arg1.toString (). replace (".",""));
Arg2 = Number (arg2.toString (). replace (".", "") * cm;
}
Else {
Arg1 = Number (arg1.toString (). replace (".", "") * cm;
Arg2 = Number (arg2.toString (). replace (".",""));
}
}
Else {
Arg1 = Number (arg1.toString (). replace (".",""));
Arg2 = Number (arg2.toString (). replace (".",""));
}
Return accDiv (arg1 + arg2), m );
}

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.