首页 > Java > java教程 > 正文

Java中的四舍五入误差

WBOY
发布: 2023-08-19 15:05:18
转载
626 人浏览过

Java中的四舍五入误差

While writing codes we all do various mistakes that lead us to errors like overflow errors and syntax errors. Rounding-off error is one of the common errors that may result in a wrong solution for a given mathematical problem. Generally, this error occurs when we work with floating point numbers.

在这篇文章中,我们将探讨这个问题的原因,并试图找到一种摆脱这种错误的方法。

舍入误差

Floating Point Types

它们也被称为实数,并且在计算需要分数值时使用。它代表具有小数点的数字。例如,表示1/5即0.2的结果,正弦和余弦的结果也需要小数点。

在Java中,有两种类型的浮点数 -

  • 浮点型 - 它可以存储最大32位的单精度值。我们使用 'float' 关键字来声明它。

  • Double − 它的大小比float大,可以存储最大64位的双精度值。它使用'double'关键字声明。

让我们讨论一个例子,在这个例子中我们可能会遇到一个四舍五入误差,然后我们将理解原因,并提供正确处理它的解决方案。

Example 1

public class Main{
   public static void main(String[] args) {
      double data1 = 0.30;
      double data2 = 0.20 + 0.10;
      System.out.println("Value after addition: " + data2);
      System.out.println(data1 == data2);
      double data3 = 0.50;
      double data4 = 0.10 + 0.10 + 0.10 + 0.10 + 0.10;
      System.out.println("Value after addition: " + data4);
      System.out.println(data3 == data4);
   }
} 
登录后复制

Output

Value after addition: 0.30000000000000004
false
Value after addition: 0.5
true
登录后复制

在上述代码中,我们在第一次相加时,‘data2’的预期值为0.30,但输出结果是错误的。然而,当我们进行另一个类似的相加操作时,我们得到了准确的结果。

Let’s take another example that shows an error.

Example 2

的中文翻译为:

示例2

public class Main {
   public static void main(String[] args) {
      double data1 = 4.30452;
      double data2 = 0.503542;
      double res = data1 + data2;
      System.out.println("Value after addition: " + res);
   }
}
登录后复制

Output

Value after addition: 4.8080620000000005
登录后复制

预期输出为4.808062,但我们这里得到了错误的输出。

Reason for errors

The ‘IEEE 754’ is a standard that is followed by Java while implementing floating point numbers. According to its ‘Rounding’ rule, when than value of mantissa is greater than 23 bits then it adds 1 to the 23rd bit to round the value. Here, the mantissa is the binary representation of fractional digits.

这就是我们得到不同值的原因。

解决错误的方法

为了避免这种舍入误差,我们有以下几种方法 -

  • BigDecimal − It is a class that can be used in the place of floating point numbers like float and double. We can perform all the normal operations that float and double datatypes provide. For example, arithmetic operations, comparison and rounding. It can handle these operations with accurate precision.

The following example demonstrates how we can use it to produce an accurate result.

Example 3

import java.math.*;
public class Main{
   public static void main(String[] args) {
      BigDecimal data1 = new BigDecimal("0.20");
      BigDecimal data2 = new BigDecimal("0.10");
      BigDecimal res = data1.add(data2); // performing addition
      System.out.println("Value after addition: " + res);
   }
}
登录后复制

Output

Value after addition: 0.30
登录后复制

In example 1, we got an error while adding two values. In the above example, we used the same values but this time we got the correct result.

  • Math.round() − 它是类‘Math’的静态方法。它以浮点数值作为参数,并返回最接近的整数值。我们使用它的类名来调用它。

The below example illustrates its use in the program.

Example 4

public class Main {
   public static void main(String[] args) {
      double data1 = 4.30452;
      double data2 = 0.503542;
      double res = data1 + data2;
      System.out.println("Value after addition: " + res);
      System.out.println("Value after rounding off the addition: " + Math.round(res));
   }
}
登录后复制

Output

Value after addition: 4.8080620000000005
Value after rounding off the addition: 5
登录后复制

Conclusion

Rounding off error is not a compile time or runtime error, the Java compiler cannot detect it. This is the reason why the programmer needs to pay extra attention to these errors otherwise we will get the wrong result. In this article, we have discussed the reason and also suggested some ways to overcome this rounding-off error.

以上是Java中的四舍五入误差的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!