JS floating-point arithmetic appears as decimal

Source: Internet
Author: User
Tags mul pow

/**************************************** solve the error of JS floating point (decimal) calculation subtraction start****************************************/

Problem: JS floating-point arithmetic problem---inexplicable number of decimal places

Cause: This is due to the operation of converting the floating-point number into binary before the operation,
But some decimals have an infinite loop after the binary code,
This results in an error in the calculation and a similar problem in other languages.

/**
* * Additive function for accurate addition results
* * Description: JavaScript addition results will be error, when the two floating-point number added will be more obvious. This function returns a more accurate addition result.
* * Call: Accadd (ARG1,ARG2)
* * return value: Arg1 plus arg2 accurate results
**/

functionAccadd (arg1, arg2) {if(IsNaN (arg1)) {arg1= 0; }    if(IsNaN (arg2)) {arg2= 0; } arg1=Number (ARG1); Arg2=Number (ARG2); varR1, 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) {        varCM = 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(Arg1 + arg2)/m;}

Adding an Add method to the number type makes it more convenient to call.

function (ARG) {    return accadd (this, arg);};

/**
* * Subtraction function to get accurate subtraction results
* * Description: JavaScript subtraction results will be error, when the two floating-point subtraction will be more obvious. This function returns a more accurate subtraction result.
* * Call: Accsub (ARG1,ARG2)
* * return value: Arg1 plus arg2 accurate results
**/

functionaccsub (arg1, arg2) {if(IsNaN (arg1)) {arg1= 0; }    if(IsNaN (arg2)) {arg2= 0; } arg1=Number (ARG1); Arg2=Number (ARG2); varR1, 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 (Ten, Math.max (R1, r2));//Last Modify by Deeka//dynamic control accuracy lengthn = (r1 >= r2)?R1:R2; return((ARG1 * M-ARG2 * m)/m). toFixed (n);}

Adding a Mul method to the number type makes it more convenient to call.

function (ARG) {    return accsub (this, arg);};

/**
* * Multiplication function to get accurate multiplication results
* * Note: The multiplication result of JavaScript will be error, it will be more obvious when two floating-point numbers are multiplied. This function returns a more accurate multiplication result.
* * Call: Accmul (ARG1,ARG2)
* * return value: Arg1 times the exact result of arg2
**/

functionAccmul (arg1, arg2) {if(IsNaN (arg1)) {arg1= 0; }    if(IsNaN (arg2)) {arg2= 0; } arg1=Number (ARG1); Arg2=Number (ARG2); varm = 0, S1 = arg1.tostring (), S2 =arg2.tostring (); Try{m+ = S1.split (".") [1].length; }    Catch(e) {}Try{m+ = S2.split (".") [1].length; }    Catch(e) {}returnNumber (S1.replace (".", "")) * Number (S2.replace (".", ""))/Math.pow (10, M);}

Adding a Mul method to the number type makes it more convenient to call.

function (ARG) {    return accmul (this, arg);};

/**
* * Division function to get accurate division results
* * Description: JavaScript division results will be error, when the two floating point number is more obvious when dividing. This function returns a more accurate division result.
* * Call: Accdiv (ARG1,ARG2)
* * return value: Arg1 divided by the exact result of arg2
**/

functionAccdiv (arg1, arg2) {if(IsNaN (arg1)) {arg1= 0; }    if(IsNaN (arg2)) {arg2= 0; } arg1=Number (ARG1); Arg2=Number (ARG2); varT1 = 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 (T2-t1); }}

Adding a Div method to the number type makes it easier to call.

function (ARG) {    return accdiv (this, arg);};


/**************************************** solve the error of JS floating point (decimal) calculation subtraction end****************************************/

JS floating-point arithmetic appears as decimal

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.