0

0

Java中的四舍五入误差

WBOY

WBOY

发布时间:2023-08-19 15:05:18

|

902人浏览过

|

来源于tutorialspoint

转载

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中,有两种类型的浮点数 -

立即学习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,但我们这里得到了错误的输出。

百度智能云·曦灵
百度智能云·曦灵

百度旗下的AI数字人平台

下载

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速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

37

2026.01.14

php与html混编教程大全
php与html混编教程大全

本专题整合了php和html混编相关教程,阅读专题下面的文章了解更多详细内容。

19

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

37

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

19

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

16

2026.01.13

PHP缓存策略教程大全
PHP缓存策略教程大全

本专题整合了PHP缓存相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.13

jQuery 正则表达式相关教程
jQuery 正则表达式相关教程

本专题整合了jQuery正则表达式相关教程大全,阅读专题下面的文章了解更多详细内容。

3

2026.01.13

交互式图表和动态图表教程汇总
交互式图表和动态图表教程汇总

本专题整合了交互式图表和动态图表的相关内容,阅读专题下面的文章了解更多详细内容。

45

2026.01.13

nginx配置文件详细教程
nginx配置文件详细教程

本专题整合了nginx配置文件相关教程详细汇总,阅读专题下面的文章了解更多详细内容。

9

2026.01.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 0.7万人学习

Rust 教程
Rust 教程

共28课时 | 4.4万人学习

Kotlin 教程
Kotlin 教程

共23课时 | 2.5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号