浮点数表示法,Java示例

编程入门 行业动态 更新时间:2024-10-24 00:30:40
本文介绍了浮点数表示法,Java示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请您解释一下,为什么我得到下一个结果:

Could you please explain, why I got next result:

运行此命令时:

System.out.println((0.2 - 0.1));

我得到:0.1

运行此命令时:

System.out.println((0.3 - 0.2));

我得到了:0.09999999999999998

I got: 0.09999999999999998

我知道数字"0.1"没有二进制的有限表示形式,但是没有解释上面的结果.这很可能与特定语言无关,而是与计算机中数字的存储方式有关.

I know that number "0.1" doesn't have finite representation in binary, but it doesn't explain the results above. Most likely this is not about particular language but about how digits are stored in computer.

推荐答案

Java使用 IEEE浮点代表double值.它不是一个精确的表示,并且一些计算会导致微小的错误,这种错误会以这种方式显现出来.

Java uses IEEE floating point to represent double values. It is not a precise representation, and some calculations result in tiny errors that manifest themselves in this way.

更多推荐

浮点数表示法,Java示例

本文发布于:2023-11-29 21:57:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647739.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   浮点数   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!