浮点数表示法,Java示例 [英] Floating point number representation, Java example

查看:64
本文介绍了浮点数表示法,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示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆