// 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 );
};
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.