Java中的四舍五入误差

2023年 8月 28日 58.8k 0

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中的四舍五入误差的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论