Because Java's simple types cannot accurately perform floating-point operations, this tool provides precise floating-point operations, including addition, subtraction, multiplication, division, rounding, and rounding.
Public class Arith {/*** Because Java's simple type cannot accurately perform floating point operations, this tool class provides precise floating point operations, including addition, subtraction, multiplication, division, and rounding. * /// Default division operation precision: private static final int DEF_DIV_SCALE = 10; // This class cannot be instantiated. private Arith () {}/ *** provides precise addition operations. ** @ Param v1 * add Number * @ param v2 * add Number * @ return 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 precise subtraction. ** @ Param v1 * subtrahend * @ param v2 * subtrahend * @ return Difference Between Two Parameters */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 exact multiplication. ** @ Param v1 * multiplier * @ param v2 * multiplier * @ return product of two parameters */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 (relatively) Precise Division operations. In case of Division, the Division is precise to 10 digits after the decimal point, and the subsequent digits are rounded down. ** @ Param v1 * divisor * @ param v2 * divisor * @ return two parameters */public static double div (double v1, double v2) {return div (v1, v2, DEF_DIV_SCALE);}/*** provides (relatively) accurate Division operations. In case of division, the scale parameter determines the precision, and the subsequent numbers are rounded down. ** @ Param v1 * divisor * @ param v2 * divisor * @ param scale * indicates the number of digits after the decimal point. * @ Return operator of two parameters */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 precise rounded decimal places. ** @ Param v * number to be rounded off * @ param scale * number of digits to be retained after the decimal point * @ return result after rounding */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 ();}/*** provides precise Float) ** @ param v * number to be converted * @ return returns the Conversion Result */public static float convertsToFloat (double v) {BigDecimal B = new BigDecimal (v); return B. floatValue ();}/*** provides exact type conversion (Int) returns the Conversion Result */public static int convertsToInt (double v) {BigDecimal B = new BigDecimal (v ); return B. intValue ();}/*** provides precise type conversion (Long) ** @ param v * number to be converted * @ return returns the Conversion Result */public static long convertsToLong (double v) {BigDecimal B = new BigDecimal (v); return B. longValue ();} /*** return a large value of the two numbers ** @ param v1 * The first number to be compared * @ param v2 * The second number to be compared * @ return returns the two numbers. A large value */public static double returnMax (double v1, double v2) {BigDecimal b1 = new BigDecimal (v1); BigDecimal b2 = new BigDecimal (v2); return b1.max (b2 ). doubleValue ();} /*** returns a small value of two numbers ** @ param v1 * The first number to be compared * @ param v2 * The second number to be compared * @ return returns two numbers. A small value */public static double returnMin (double v1, double v2) {BigDecimal b1 = new BigDecimal (v1); BigDecimal b2 = new BigDecimal (v2); return b1.min (b2 ). doubleValue ();} /*** exact comparison of two numbers ** @ param v1 * The first number to be compared * @ param v2 * The second number to be compared * @ return returns if the two numbers are the same 0, if the first number is greater than the second number, 1 is returned. Otherwise,-1 */public static int compareTo (double v1, double v2) {BigDecimal b1 = new BigDecimal (v1) is returned ); bigDecimal b2 = new BigDecimal (v2); return b1.compareTo (b2 );}}