Home >Java >javaTutorial >Detailed explanation of examples of float and double precision loss in Java
1.The value range of int, float, long, double in java
[java] view plain copy
public class TestOutOfBound {
public static void main( String[] args) {
System.out.println(Integer.MAX_VALUE-(-Integer.MAX_VALUE)); //Memory overflow
System.out. println(Integer.MAX_VALUE); //2 to the 31st power -1,10 digits, about 2 billion, which may not be enough for money
System.out.println(Integer.MIN_VALUE) ; //Negative 2 to the 31st power
System.out.println(Long.MAX_VALUE); //2 to the 64th power - 1, 19 digits, which is very large and can be used for money with confidence Above
System.out.println(Long.MIN_VALUE); //Negative 2 to the 64th power
System.out.println(Float.MAX_VALUE); //2 to the 128th power Power -1, 38 digits, twice as many as long. This is mainly used for simple mathematical precise operations.
System.out.println(Float.MIN_VALUE); //2 to the power of -149
System.out.println(Double.MAX_VALUE); //2 to the 1024th power - 1,308 digits, which is 10 times the number of float digits. It is mainly used for complex operations and astronomical operations
System.out.println(Double.MIN_VALUE); //2 to the power of -1074
}
}
2.float and Double precision loss problem
Example:
[java] view plain copy
Example: double result = 1.0 - 0.9;
Needless to say this result, we all know it, 0.09999999999999998
Why does this problem occur? This is a problem that occurs in java and other computer languages. Let’s analyze why this occurs. Question:
The float and double types are mainly designed for scientific calculations and engineering calculations. They perform binary floating-point arithmetic, which is carefully designed to provide fast approximate calculations with high accuracy over a wide range of numbers. However, they do not provide completely accurate results and should not be used for exact calculations. The float and double types are especially unsuitable for monetary operations, because it is impossible for a float or double to accurately represent 0.1 or any other negative power of 10 (in fact, the reason is very simple. Can it be accurately represented in the decimal system? What about 1/3? The same binary system cannot accurately represent 1/10).
Floating point operations are rarely accurate, and errors will occur as long as they exceed the range that the precision can represent. The error often occurs not because of the size of the number, but because of the precision of the number. Therefore, the results produced are close to but not equal to the desired results. Be especially careful when using float and double for exact operations.
Now let’s analyze in detail why floating-point operations cause precision loss?
# 2/2=1 0 End The binary representation of 11 is (from bottom to top): 1011
Here’s a point: As long as the result after division is 0, it’s over. Think about it, everyone. Does dividing by 2 definitely result in 0? In other words, will the algorithm that converts all integers into binary numbers continue in an infinite loop? Absolutely not, integers can always be represented accurately in binary, but decimals are not necessarily represented.
(2) How to convert decimal numbers into binary numbers
The algorithm is to multiply by 2 until there are no decimals. For example, 0.9 indicates the binary number
# 0.9*2 = 1.8 Take the integer part 1
# 0.8 (1.8 decimal part)* 2=1.6 Get the integer part 1 0.6*2=1.2 Get the integer part 1 0.2*2=0.4 Get the integer part 0 0.4*2=0.8 Take the integer part 0 0.8*2=1.6 Take the integer part 10.6*2 = 1.2 Take the integer part 0 # ......... 0.9 binary representation (from top to bottom): 1100100100100 ..... . Obviously, the binary representation of decimals is sometimes impossible to be accurate. In fact, the reason is very simple. Can 1/3 be accurately represented in the decimal system? Likewise, the binary system cannot accurately represent 1/10. This also explains why floating-point subtraction has the problem of "inexhaustible" loss of precision.
Then the question comes, imagine, if we want to do an addition operation of floating point data, we need to convert the two floating point numbers into String data first, and then use BigDecimal (String value) to construct a BigDecimal, and then To call the add method on one of them, pass in the other as a parameter, and then convert the result of the operation (BigDecimal) into a floating point number. If you have to do this every time you do calculations on floating-point data, can you endure such a cumbersome process? At least I can't. So the best way is to write a class and complete these cumbersome conversion processes in the class. In this way, when we need to perform floating point data calculations, we only need to call this class. Some experts on the Internet have provided us with a tool class Arith to complete these conversion operations. It provides the following static methods, which can complete the addition, subtraction, multiplication and division operations of floating-point data and round the results:
public static double add(double v1,double v2)
public static double sub (double v1,double v2)
public static double mul(double v1,double v2)
public static double div(double v1,double v2)
public static double div(double v1,double v2,int scale)
public static double round(double v,int scale)
The source code of Arith will be attached below. You only need to compile and save it. When you want to perform floating point calculations, in your By importing the Arith class into the source program, you can use the above static methods to perform precise calculations of floating point numbers.
Appendix: Arith source code
[java] view plain copy
import java.math.BigDecimal;
/**
* Since Java's simple types cannot perform precise operations on floating-point numbers, this tool class provides precise
* operations on floating-point numbers, including addition, subtraction, multiplication, division and rounding.
*/
public class Arith{
//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 Addend
* @param v2 Addend
* @return The sum of the two parameters
*/
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 operations.
* @param v1 Minuend
* @param v2 Minuend
* @return The difference between the 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 precise multiplication operations.
* @param v1 Multiplicand
* @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) accurate division operations. When division occurs, the calculation is accurate to
* 10 decimal places after the decimal point, and subsequent numbers will be rounded.
* @param v1 Divisor
* @param v2 Divisor
* @return Quotient of the two parameters
*/
public static double div(double v1,double v2){
return div(v1,v2,DEF_DIV_SCALE);
}
/**
* Provides (relatively) accurate division operations. When inexhaustible division occurs, the precision is specified by the scale parameter
* and subsequent numbers are rounded off.
* @param v1 dividend
* @param v2 divisor
* @param scale means it needs to be accurate to a few decimal places.
* @return The quotient of the 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();
}
/**
* Provide precise decimal place rounding processing.
* @param v The number that needs to be rounded
* @param scale The number of decimal places to keep
* @return The rounded result
*/
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();
}
};
The above is the detailed content of Detailed explanation of examples of float and double precision loss in Java. For more information, please follow other related articles on the PHP Chinese website!