1. Introduction
In the Effactive Java book, the main design goals for float and double are for scientific calculations and engineering calculations. They perform binary floating-point operations, which are designed to provide more accurate, fast approximation calculations in wide-area numerical ranges. However, they do not provide completely accurate results, so they should not be used in situations where precise results are required. But business computing often requires accurate results, and this is where BigDecimal comes in handy.
2.BigDecimal Introduction
The BigDecimal consists of an arbitrary precision integer Non scale value and a 32-bit integer scale (scale). If it is zero or positive, the scale is the number of digits after the decimal point. If it is a negative number, multiply the value of the scale's scale by 10 of the negative scale power. Therefore, the value represented by BigDecimal is (Unscaledvaluex10-scale).
3. Test code
3.1 Constructors (two commonly used constructors with the main test parameter type double and string)
Copy Code code as follows:
BigDecimal adouble =new BigDecimal (1.22);
System.out.println ("construct with a Double value:" + adouble);
BigDecimal astring = new BigDecimal ("1.22");
System.out.println ("construct with a String value:" + astring);
What do you think the output will be? If you do not think the first one will output 1.22, then congratulations to you correctly, the output is as follows:
Copy Code code as follows:
Construct with a doublevalue:1.2199999999999999733546474089962430298328399658203125
Construct with a String value:1.22
Description of JDK:
1. The result of the method of constructing the parameter type double is somewhat unpredictable. One might think that writing to Newbigdecimal (0.1) in Java is exactly equal to 0.1 (BigDecimal value 1 with a scale of 1). But it actually equals 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be accurately represented as a double (or, for that case, cannot be represented as any finite-length binary decimal). In this way, the value passed in to the constructed method does not exactly equal 0.1 (although it is equal to the value on the surface).
2, on the other hand, the String construction method is completely predictable: writing to Newbigdecimal ("0.1") creates a BigDecimal that is exactly equal to the expected 0.1. Therefore, in comparison, it is generally recommended that you use string construction methods preferentially.
3. When double must be used as a source for BigDecimal, note that this constructor provides an exact conversion; it does not provide the same result as the following: First use the double.tostring (double) method, and then use BigDecimal ( String) and converts a double to a string. To get the result, use the static valueof (double) method.
3.2 Addition operation
Copy Code code as follows:
BigDecimal a =new BigDecimal ("1.22");
System.out.println ("construct with a String value:" + a);
BigDecimal b =new BigDecimal ("2.22");
A.add (b);
System.out.println ("Aplus B is:" + a);
It is easy to think that the output will be:
Copy Code code as follows:
Construct with a stringvalue:1.22
A plus b is:3.44
But actually a plus B is:1.22
4. Source Code Analysis
4.1 valueof (Doubleval) method
Copy Code code as follows:
public static BigDecimal valueof (double val) {
Reminder:a zero Double returns ' 0.0 ', so we cannotfastpath
To use the constant ZERO. This might is important enough to
Justify a factory approach, a cache, or a few private
Constants, later.
Returnnew BigDecimal (Double.tostring (val))/See 3.1 3rd on JDK description
}
4.2 Add (BigDecimal augend) method
Public BigDecimal Add (BigDecimal augend) {long xs =this.intcompact;//integer numeric representation of BigDecimal, example a intcompact value is 122 long ys = augend.intcompact;//ditto BigInteger fst = (this.intcompact!=inflated)? null:this.intval;//Initialize the BigInteger value,
Intval is a bigdecimal biginteger type of attribute BigInteger snd = (augend.intcompact!=inflated)? Null:augend.intVal;
int Rscale =this.scale;//decimal digits long Sdiff = (long) rscale-augend.scale;//number of decimal places if (Sdiff!= 0) {////decimal number of decimal places with multiple digits
if (Sdiff < 0) {int raise =checkscale (-sdiff);
Rscale =augend.scale;
if (xs ==inflated | |
(xs = longmultiplypowerten (xs,raise)) ==inflated) FST =bigmultiplypowerten (raise);
}else {int raise =augend.checkscale (Sdiff); if (ys ==inflated | |
(Ys =longmultiplypowerten (ys,raise)) ==inflated) snd = Augend.bigmultiplypowerten (raise);
} if (xs!=inflated && ys!=inflated) {Long sum = xs + ys; if ((sum ^ xs) & (sum ^ ys)) >= 0L)//To determine whether there is overflow return bigdecimal.valueof (Sum,rscale)//Returns the BigDecimal instance that was obtained using the BigDecimal static Factory method} if (FST ==null) FST =biginteger.valueof (
XS);//biginteger static Factory method if (snd ==null) snd =biginteger.valueof (YS);
BigInteger sum =fst.add (SND); return (Fst.signum = = snd.signum)? New BigDecimal (sum,inflated, Rscale, 0): New BigDecimal (Sum,compactvalfor (sum), Rsca
Le, 0)//Returns the BigDecimal object that was obtained by other construction methods}
The above is only the analysis of the addition of the source, Division and Division in fact ultimately return is a new BigDecimal object, because BigInteger and BigDecimal are immutable (immutable), in each step of the operation, will produce a new object, So A.add (b); Although the addition operation, but a does not save the value of the operation, the correct use should be A=a.add (b);
5. Summary
(1) Commercial computing uses BigDecimal.
(2) Use the constructor with the parameter type string as far as possible.
(3) BigDecimal are immutable (immutable), each step of the operation, will produce a new object, so in doing subtraction operations must save the value of the operation.
(4) We tend to overlook some of the implementation details at the bottom of the JDK, resulting in errors that require extra attention.
Reference: Class BigDecimal