Home Java javaTutorial Detailed explanation of examples of float and double precision loss in Java

Detailed explanation of examples of float and double precision loss in Java

Jun 28, 2017 am 11:38 AM
java numerical value floating point calculate avoid

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?



##[java] view plain copy

First we need to figure out the following two questions:

(1) How to convert decimal integers into binary numbers

The algorithm is very simple. For example, 11 is expressed as a binary number:

#                  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 1

0.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.


3. Solution 1:


#If you don’t mind recording the decimal point yourself and the value is not large, Then you can use basic types such as long and int. Whether you use int or long depends on the size of the numerical range involved. The disadvantage is that you have to handle the decimal point yourself. The most obvious way is to use cents to calculate currency instead of yuan (only involving addition). reduce).

For example:

[java] view plain copy

int resultInt = 10 - 9;

double resultInt = (double) resultInt / 100;/ /In the end, you control the decimal point yourself

4. Solution two:

Use BigDecmal, and you need to use String type in the construction parameters.

A solution is given in the book "Effective Java". The book also points out that float and double can only be used for scientific calculations or engineering calculations. In precise calculations such as business calculations, we have to use java.math.BigDecimal.

The BigDecimal class has 4 methods. We only care about the methods that are useful for accurate calculations of floating-point data, that is,

BigDecimal(double value) // Convert double data Into BigDecimal type data

The idea is very simple. We first convert the double type data into BigDecimal data through the BigDecimal(double value) method, and then we can perform precise calculations normally. After the calculation is completed, we can do some processing on the results, such as rounding the results that cannot be divided. Finally, the result is converted from BigDecimal data back to double data.

This idea is correct, but if you look carefully at the detailed description of BigDecimal in the API, you will know that if precise calculation is required, we cannot use double directly, but must use String to construct BigDecimal. ! Therefore, we start to care about another method of the BigDecimal class, that is, the BigDecimal(String value) method that can help us complete precise calculations correctly.

// BigDecimal(String value) can convert String data into BigDecimal data

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

See all articles