Let's start by looking at the following code example:
public class Test_1 {public static void main (string[] args) {System.out.println (0.06+0.01); System.out.println (1.0-0.42); System.out.println (4.015*100); System.out.println (303.1/1000); } }
The results of the operation are as follows.
0.06999999999999999
0.5800000000000001
401.49999999999994
0.30310000000000004
You think you're wrong, but it turns out that's the case. Where is the problem? The reason is that our computer is binary. Floating-point numbers do not have to be represented in a binary way. Our CPU indicates that the floating-point number is composed of two parts: exponent and mantissa, so the expression method generally loses certain accuracy, some floating-point arithmetic will produce some error. For example, a binary representation of 2.4 is not exactly 2.4. Instead the nearest binary representation is 2.3999999999999999. The value of a floating-point number is actually calculated from a specific mathematical formula.
In fact, the Java float can only be used for scientific calculations or engineering calculations, in most commercial calculations, the general use of the Java.math.BigDecimal class for accurate calculation.
When using the BigDecimal class to perform calculations, the main steps are divided into the following:
1. Construct BigDecimal object with float or double variable.
2, by calling BigDecimal the addition, subtraction, multiplication, in addition to the corresponding method of arithmetic operations.
3, convert the BigDecimal object to Float,double,int and other types.
In general, you can use the BigDecimal constructor or the valueof () method of a static method to build a variable of the basic type into a BigDecimal object.
1 BigDecimal b1 = new BigDecimal (double.tostring (0.48)); 2 BigDecimal b2 = bigdecimal.valueof (0.48);
For commonly used add, subtract, multiply, divide, the BigDecimal class provides the corresponding Member method.
1 public BigDecimal Add (BigDecimal value); Addition 2 public BigDecimal subtract (BigDecimal value); Subtraction 3 public BigDecimal multiply (BigDecimal value); Multiplication 4 public BigDecimal divide (BigDecimal value); Division
After the corresponding calculations, we may need to convert the BigDecimal object to a variable of the corresponding base data type, using methods such as Floatvalue (), Doublevalue (), and so on.
Below is a tool class that provides add, subtract, multiply, and divide operations.
public class arith { /** * Add method for accurate addition calculations * @param value1 summand * @param value2 addend * @return two parameters and */ public static double add (double value1,double value2) { bigdecimal b1 = new bigdecimal ( Double.valueof (value1)); bigdecimal b2 = new bigdecimal (double.valueof (value2)); return b1.add (B2 ). Doublevalue (); } /** * Sub method that provides exact subtraction operations * @param value1 meiosis * @param value2 * @return Two parameter difference */ public static double sub (double value1,double value2) { bigdecimal b1 = new bigdecimal (Double.valueOf (value1)) ; bigdecimal b2 = new bigdecimal ( Double.valueof (value2)); return b1.subtract (B2). Doublevalue (); } /** * Mul methods for accurate multiplication * @param value1 by multiplier * @param value2 multiplier * @return product of two parameters */ public static double mul (double Value1,double value2) { bigdecimal b1 = new bigdecimal (Double.valueOf (value1)) ; bigdecimal b2 = new bigdecimal ( Double.valueof (value2)); return b1.multiply (B2). Doublevalue (); } /** * provides accurate method of division div * @param value1 Dividend * @param value2 divisor * @param scale Precision Range * @return Two-parameter dealers * @throws IllegalAccessException */ public static Double div (Double value1,double value2,int scale) throws illegalaccessexception{ //if the exact range is less than 0, throw exception information if (scale<0) { Throw new illegalaccessexception ("Accuracy cannot be less than 0"); } bigdecimal b1 = new bigdecimal (Double.valueOf ( value1)); bigdecimal b2 = new bigdecimal ( Double.valueof (value2)); return b1.divide (B2, scale). Doublevalue (); }}
Using Java BigDecimal for precise operations