public class Arith {/** * because Java's simple type does not accurately calculate floating-point numbers, this tool class provides precise floating-point arithmetic, including subtraction and rounding. *///Default division operation precision private static final int def_div_scale = 10;//This class cannot instantiate private Arith () {}/** * provides precise addition operations. * * @param v1 * summand * @param v2 * addend * @return two parameters and */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 accurate subtraction operations. * * @param v1 * minuend * @param v2 * meiosis * @return Two parameter 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 accurate multiplication operations. * * @param v1 * by multiplier * @param v2 * multiplier * @return two parameters of 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, when there are no more than an endless number of cases, accurate to 10 digits after the decimal point, the subsequent numbers rounded. * * @param v1 * Dividend * @param v2 * divisor * @return two parameters of quotient */public static double div (double v1, double V2) {return div (v1, v2, def_div_scale);} /** * provides (relative) accurate division operations. When an exception occurs, the precision is specified by the scale parameter, and the subsequent number is rounded. * * @param v1 * Dividend * @param v2 * divisor * @param scale * Indicates the need to be accurate to several decimal places. * @return two parameters of quotient */public static double div (double v1, double v2, int scale) {if (Scale < 0) {throw new illegalargument Exception ("The scale must is 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 precise rounding of decimal digits. * * @param v * number to be rounded * @param scale * Keep several after decimal point * @return rounded results */public static double round ( Double V, int scale) {if (Scale < 0) {throw new IllegalArgumentException ("The scale must is 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 (); /** * provides exact type conversion (Float) * * @param v * required number to be converted * @return return conversion result */public static Float convertstofloat (Double V {BigDecimal b = new BigDecimal (v); return B.floatvalue ();} /** * provides exact type conversions (INT) without rounding * * @param v * need to be converted number * @return return the conversion result */public static Int Convertstoint (double V) {BigDecimal b = new BigDecimal (v); return B.intvalue ();} /** * provides exact type conversion (Long) * * @param v * Number required to be converted * @return Returns the result of the conversion */public static Long Convertstolong (double v) { BigDecimal B = new BigDecimal (v); return B.longvalue ();} /** * Returns a value that is larger in two numbers * * @param v1 * The first number to be compared * @param v2 * The second number to be compared * @return returns a value of two in the large number */publi C Static Double Returnmax (double v1, double v2) {BigDecimal B1 = new BigDecimal (v1); BigDecimal b2 = new BigDecimal (v2); return B1.max (B2). Doublevalue (); /** * returns twoA value that is small in number * * @param v1 * The first number to be compared * @param v2 * The second number to be compared * @return returns a value of two decimal values */public static Double Returnmin (Double v1, double v2) {BigDecimal B1 = new BigDecimal (v1); BigDecimal b2 = new BigDecimal (v2); return B1.min (B2). Doublevalue (); /** * Accurate comparison of two numbers * * @param v1 * need to be compared to the first number * @param v2 * need to be compared to the second number * @return If two number is the same return 0, if the first number than the second A large number returns 1, and vice versa returns-1 */public static int compareTo (double v1, double v2) {BigDecimal B1 = new BigDecimal (v1); BigDecimal b2 = new BigDecimal (v2); return B1.compareto (B2);}}
Because Java's simple type does not accurately operate on floating-point numbers, this tool class provides precise floating-point arithmetic, including subtraction and rounding