Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The problem of loss of calculation accuracy of double Type and its solution

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you the problem of loss of accuracy in double type computing and its solutions. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can gain something through the detailed introduction of this article.

Public class Test {public static void main (String [] args) {System.out.println (0.06mm 0.01); System.out.println (1.00.42); System.out.println (4.015mm 100); System.out.println (303.1amp 1000);}}

Output

0.069999999999999990.5800000000000001401.499999999999940.30310000000000004

We found that the calculated values were not consistent with our expected results. The reason is that our computer is binary. There is no way to accurately represent floating-point numbers in binary. The CPU of the computer shows that a floating-point number consists of two parts: an exponent and a Mantissa. This representation generally loses a certain degree of accuracy, and some floating-point operations also produce certain errors. For example, the binary representation of 2.4 is not the exact 2.4. On the contrary, the closest binary representation is 2.3999999999999999. The value of a floating point number is actually calculated by a specific mathematical formula. Please refer to http://blog.csdn.net/abing37/article/details/5332798

So how can we get the expected results we want? Especially in dealing with the calculation of amount transactions. In fact, the float of java can only be used for scientific calculation or engineering calculation. In most business calculations, the java.math.BigDecimal class is generally used for accurate calculation. When using the BigDecimal class for calculation, it is mainly divided into the following steps:

(1) build a BigDecimal object with float or double variables. It is common to use the constructor of BigDecimal or the valueOf () method of static methods to build basic types of variables into BigDecimal objects.

(2) the arithmetic operation is carried out by calling the corresponding methods of addition, subtraction, multiplication and division of BigDecimal.

(3) convert BigDecimal objects to float,double,int and other types.

Basic introduction to BigDecimal class

Before we modify the instance, let's take a brief look at the constructor and member methods of the BigDecimal class.

BigDecimal (int var) / / creates an object with the integer value specified by the parameter. BigDecimal (double var) / / creates an object with the double-precision value specified by the parameter. BigDecimal (long var) / / creates an object with the long integer value specified by the parameter. BigDecimal (String var) / / creates an object with a numeric value specified by the parameter as a string.

Member method (BigDecimal's operation method does not support +-* / this kind of operation it has its own operation method)

BigDecimal add (BigDecimal augend) / / addition BigDecimal subtract (BigDecimal subtrahend) / / subtraction BigDecimal multiply (BigDecimal multiplicand) / / multiplication BigDecimal divide (BigDecimal divisor) / / Division

Well, now that we know the way, let's use a new method to solve the above problem. Modify the code as follows:

Import java.math.*;public class Test {public static void main (String [] args) {double D1 = 0.06; double d2 = 0.01; BigDecimal b1 = new BigDecimal (D1); BigDecimal b2 = new BigDecimal (d2); System.out.println (b1.add (b2). DoubleValue ()); double d3 = 1.0; double d4 = 0.42, BigDecimal b3 = new BigDecimal (d3); BigDecimal b4 = new BigDecimal (d4); System.out.println (b3.subtract (b4). DoubleValue ()); double d5 = 4.015; double d6 = 100; BigDecimal b5 = new BigDecimal (d5); BigDecimal b6 = new BigDecimal (d6); System.out.println (b5.multiply (b6). DoubleValue ()) Double D7 = 303.1; double D8 = 1000; BigDecimal b7 = new BigDecimal (D7); BigDecimal b8 = new BigDecimal (D8); System.out.println (b7.divide (b8). DoubleValue ());}}

Output

0.069999999999999990.5800000000000001401.499999999999940.30310000000000004

We found that the result was still wrong. From the above example, we know that the constructor called is BigDecimal (double var). BigDecimal (doubleval) converts double to BigDecimal, which is an accurate decimal representation of the binary floating-point value of double. The scale of the returned BigDecimal is the minimum value that makes (10scale × val) an integer. There are also a few things to pay special attention to here:

(1) the result of this construction method is unpredictable to a certain extent. One might think that the BigDecimal created by writing new BigDecimal (0.1) in Java is exactly equal to 0.1 (a non-scalar value of 1 with a scale of 1), but it is actually equal to 0.10000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be accurately represented as double (or, in this case, as any finite length binary decimal). In this way, the value passed into the constructor is not exactly equal to 0.1 (although ostensibly equal to that value).

(2) on the other hand, the String construction method is completely predictable: writing new BigDecimal ("0.1") creates a BigDecimal that is exactly equal to the expected 0.1. Therefore, by comparison, it is generally recommended that the String construction method be preferred.

(3) when double must be used as the source of BigDecimal, note that this constructor provides an exact transformation; it does not provide the same result as using the Double.toString (double) method, and then using the BigDecimal (String) constructor to convert double to String. To get this result, use the static valueOf (double) method.

According to the above description, we will continue to modify the following example, which is as follows:

Import java.math.*;public class Test {public static void main (String [] args) {double D1 = 0.06; double d2 = 0.01; BigDecimal b1 = new BigDecimal (Double.toString (D1)); BigDecimal b2 = new BigDecimal (Double.toString (d2)); System.out.println (b1.add (b2). DoubleValue ()); double d3 = 1.0 Double d4 = 0.42; BigDecimal b3 = new BigDecimal (Double.toString (d3)); BigDecimal b4 = new BigDecimal (Double.toString (d4)); System.out.println (b3.subtract (b4). DoubleValue ()); double d5 = 4.015; double d6 = 100; BigDecimal b5 = new BigDecimal (Double.toString (d5)); BigDecimal b6 = new BigDecimal (Double.toString (d6)) System.out.println (b5.multiply (b6). DoubleValue ()); double D7 = 303.1; double D8 = 1000; BigDecimal b7 = new BigDecimal (Double.toString (d7)); BigDecimal b8 = new BigDecimal (Double.toString (D8)); System.out.println (b7.divide (b8). DoubleValue ());}}

Output

0.070.58401.50.3031

The calculation accuracy is correct.

Summary

(1) when we need to accurately represent two decimal places, we need to convert them into BigDecimal objects, and then do the operation.

(2) the loss of precision still exists when using the BigDecimal (doubleval) constructor, so it is recommended to use BigDecimal (String val). This requires converting the double type (calling Double.toString (double var)) to a string and then as an argument to the BigDecimal (String val) constructor. Add, subtract, multiply and divide after converting to a BigDecimal object, so that there is no problem with precision. This is why BigDecimal is used to store data about money.

The above content is the problem and solution of the loss of accuracy of double type calculation. have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report