Import java.math.bigdecimal;/** * This tool class provides accurate floating-point arithmetic, including subtraction and rounding, because Java's simple type does not accurately perform operations on floating-point numbers. */public class Arith {//default division precision. When dividing, the default is exactly 10 bits after the decimal point 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 = newBigDecimal (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) {//throws an exception indicating an illegal or incorrect pass to the method Parameter///When an exception is thrown in the method, the code after the exception is not executed.//The parameter passed in by the user is an invalid parameter, and there is no need to perform the following operation. This is the usual way to throw new IllegalArgumentException ( "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 * retained after decimal pointSeveral * @return rounded results */public static double round (double v, int scale) {if (Scale < 0) {throw new Illegalargumentexcep tion ("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 ();}}
Tool class---provides accurate floating-point arithmetic