如果0.1没有二进制表示,为什么我得到0.1 [英] if 0.1 has no binary representation, why I get 0.1

查看:57
本文介绍了如果0.1没有二进制表示,为什么我得到0.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跑步时:

    System.out.println(1f - 0.9f);

我得到:

    0.100000024

这是因为0.1没有二进制表示形式.

This is because 0.1 has no representation in binary.

那为什么当我打印这个时:

Then why when I print this:

    System.out.println(0.1f);

我明白了:

    0.1

推荐答案

0.1可以比0.9更好地表示为浮点数.松散地说,这是因为0.1较小并且更接近于其最近的二元有理数.

0.1 can be represented better in floating point than 0.9. Loosely speaking that's because 0.1 is smaller and closer to its nearest dyadic rational.

所以从1.0减去时的误差会更大.

So the error when subtracting from 1.0 is larger.

因此两个值不同.

println中的嵌入式格式化启发式算法在使用0.1时效果更好

The embedded formatting heuristics in println do a better job with the 0.1

这篇关于如果0.1没有二进制表示,为什么我得到0.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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