Javascript floating point calculation error Solution

Source: Internet
Author: User

/*************************************** **************************************** */
// JavaScript has obvious errors when performing two floating point operations. Add the following methods.
// 2009-07-18 skyeah

// 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 (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)
// 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)
}
// Add a mul Method to the number type to facilitate calling.
Number. Prototype. Mul = function (ARG ){
Return accmul (ARG, this );
}
// Addition function, 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)
// Return value: the exact result 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 function, used to obtain the exact subtraction result
// Note: javascript subtraction results in errors, which are obvious when two floating point numbers are subtracted. This function returns a more accurate addition result.
// Call: accsub (arg1, arg2)
// Return value: the exact result of arg1 minus arg2

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 ));
N = (r1> = R2 )? R1: R2;
Return (arg1 * m-arg2 * m)/m). tofixed (N );
}
// Add an add method to the number type to facilitate calling.
Number. Prototype. sub = function (ARG ){
Return accsub (ARG, this );
}

/*************************************** **************************************** */

Related Article

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.