红宝石浮点精度 [英] Ruby float precision

查看:69
本文介绍了红宝石浮点精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,Ruby(1.9.2)浮点数的精度为15个十进制数字.因此,我希望将浮点数 x 舍入到15个小数位将等于 x .对于这种计算,情况并非如此.

As I understand it, Ruby (1.9.2) floats have a precision of 15 decimal digits. Therefore, I would expect rounding float x to 15 decimal places would equal x. For this calculation this isn't the case.

x = (0.33 * 10)
x == x.round(15) # => false

顺便说一句,四舍五入到16位将返回true.

Incidentally, rounding to 16 places returns true.

你能给我解释一下吗?

推荐答案

部分问题是0.33不能以基本格式精确表示,因为它不能由一系列 1/2表示 n 字词.因此,当乘以 10 时,将乘以与 0.33 略有不同的数字.

Part of the problem is that 0.33 does not have an exact representation in the underlying format, because it cannot be expressed by a series of 1 / 2n terms. So, when it is multiplied by 10 a number slightly different than 0.33 is being multiplied.

为此, 3.3 也没有确切的表示形式.

For that matter, 3.3 does not have an exact representation either.

第一部分

当数字不具有精确的以10为基数的表示形式时,转换尾数中存在信息的最低有效数字时将出现余数.其余的可能会永远传播到右侧,但是在很大程度上是没有意义的.此错误的明显随机性是由于与解释 Matchu 的四舍五入明显相同的原因造成的.这是第二部分.

When numbers don't have an exact base-10 representation, there will be a remainder when converting the least significant digit for which there was information in the mantissa. This remainder will propagate to the right, possibly forever, but it's largely meaningless. The apparent randomness of this error is due to the same reason that explains the apparently-inconsistent rounding you and Matchu noticed. That's in part two.

第二部分

并且此信息(最右边的位)与单个十进制数字所传达的信息不是整齐地对齐,因此,十进制数字通常会比其原始精度更高时的值小一些.

And this information (the right-most bits) is not aligned neatly with the information conveyed by a single decimal digit, so the decimal digit will typically be somewhat smaller than its value would have been if the original precision had been greater.

这就是为什么转换可能将15位舍入为1,将16位舍入为0.x的原因:因为较长的转换对于尾数尾部右边的位没有值.

This is why a conversion might round to 1 at 15 digits and 0.x at 16 digits: because a longer conversion has no value for the bits to the right of the end of the mantissa.

这篇关于红宝石浮点精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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