On the internet to find the Java and JS high-precision computing files, very useful. Posted out hoping someone could find it. In this special tribute to the code of the original person ...
Java:
Import Java.math.BigDecimal;
/** * Because Java's simple type does not accurately operate on floating-point numbers, this tool class provides precision floating-point operations, including subtraction and rounding.
*/public class arith{//default division precision private static final int def_div_scale = 10;
This class cannot instantiate private Arith () {}/** * provides exact addition operations.
* @param v1 summand * @param v2 addends * @return two parameters and/or public static double add (Double v1,double v2) {
BigDecimal B1 = new BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2));
Return B1.add (B2). Doublevalue ();
/** * Provides an accurate subtraction operation.
* @param v1 Bing * @param v2 * @return Two parameters of the difference * * public static double sub (double v1,double v2) {
BigDecimal B1 = new BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2));
Return B1.subtract (B2). Doublevalue ();
/** * Provides an accurate multiplication operation. * @param v1 multiplier * @param v2 multiplier * @return Two parameters of the product/public static double Mul (double v1,Double v2) {BigDecimal B1 = new BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2));
Return b1.multiply (B2). Doublevalue ();
/** * provides (relative) accurate division operations, which, when occurring in addition to the circumstances, are accurate to the 10 digits after the decimal point, after which the numbers are rounded.
* @param v1 Dividend * @param v2 divisor * @return Two parameters of the quotient/public static double div (Double v1,double v2) {
Return Div (V1,v2,def_div_scale); /** * provides (relative) precise division operations.
When it occurs, the scale parameter refers to the fixed precision, and the subsequent digits are rounded up.
* @param v1 Dividend * @param v2 divisor * @param scale indicated that the need to be accurate to several decimal places.
* @return Two-parameter quotient/public static double div (double v1,double v2,int scale) {if (scale<0) {
throw new IllegalArgumentException ("The scale must be a positive integer or zero");
} BigDecimal B1 = new BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2)); Return B1.divide (B2,SCALE,BIGDECIMAL.ROUND_HALF_UP). DOublevalue ();
/** * Provides accurate decimal rounding processing. * @param v to be rounded number * @param scale decimal point after a few * @return rounded results * * public static double round (double V, int scale) {if (scale<0) {throw new IllegalArgumentException ("The scale must be a
Positive integer or zero ");
} BigDecimal B = new BigDecimal (double.tostring (v));
BigDecimal one = new BigDecimal ("1");
Return B.divide (ONE,SCALE,BIGDECIMAL.ROUND_HALF_UP). Doublevalue ();
}
}
Js:
The Division function, which is used to get the exact division result//Description: JavaScript division results will have errors, it is more obvious when dividing two floating-point numbers.
This function returns a more precise division result.
Call: Accdiv (ARG1,ARG2)//return value: Arg1 divided by arg2 exact result 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, which is more convenient to call.
Number.prototype.div = function (ARG) {return Accdiv (this, ARG); A//multiplication function to get the exact result of multiplication//description: JavaScript multiplication results will have errors, the two floating-point numbers will be more obvious when multiplying.
This function returns a more accurate result of the multiplication. Call: Accmul (ARG1,ARG2)//return value: Arg1 times Arg2 's exact result function Accmul (arg1,arg2) {var m=0,s1=arg1.tostring (), S2=arg2.tostrin
g (); 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)}//
Adding a Mul method to the number type is more convenient to call. Number.prototype.mul = function (ARG) {return Accmul (ARG, this); The//addition function, which is used to get the exact addition result//description: The addition of JavaScript will have errors, it will be more obvious when the two floating-point numbers are added.
This function returns a more precise addition result.
Call: Accadd (ARG1,ARG2)//return value: Arg1 plus arg2 exact result 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)///+ Adds an Add method to the number type, which is easier to invoke.
Number.prototype.add = function (ARG) {return accadd (arg,this);
///In the place you want to include these functions, and then call it to calculate on it.
For example, you want to calculate: 7*0.8, then change to (7). Mul (8)//Other operations similar, you can get more accurate results.
Subtraction functions function Subtr (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 ((arg1*m-arg2*m)/m). ToFixed (n);
}