在Ruby中,为什么“100.7”.to_f.modulo(1) = 0.700000000000003?

发布于 2024-08-01 18:58:26 字数 266 浏览 3 评论 0原文

这对我来说很奇怪:

irb(main):012:0> "100.7".to_f.modulo(1)
=> 0.700000000000003

为什么最后是3?

irb(main):019:0> "10.7".to_f.modulo(1)
=> 0.699999999999999

同样的事情......我们只是得到这个值除以一的余数。 应该是准确的。

This is very strange to me:

irb(main):012:0> "100.7".to_f.modulo(1)
=> 0.700000000000003

Why the 3 at the end?

irb(main):019:0> "10.7".to_f.modulo(1)
=> 0.699999999999999

Same thing here...we are only getting the remainder of this value divided by one. It should be exact.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ぺ禁宫浮华殁 2024-08-08 18:58:26

欢迎来到浮点数学。 有许多数字无法用标准浮点表示法表示,并且结果略有偏差。

这很容易说明如下:

(1..10).collect do |i|
  v = ((10**i).to_f + 0.7)
  puts "%13.1f = %.30f" % [ v, v.modulo(1) ]
end

结果是:

         10.7 = 0.699999999999999289457264239900
        100.7 = 0.700000000000002842170943040401
       1000.7 = 0.700000000000045474735088646412
      10000.7 = 0.700000000000727595761418342590
     100000.7 = 0.699999999997089616954326629639
    1000000.7 = 0.699999999953433871269226074219
   10000000.7 = 0.699999999254941940307617187500
  100000000.7 = 0.700000002980232238769531250000
 1000000000.7 = 0.700000047683715820312500000000
10000000000.7 = 0.700000762939453125000000000000

请注意,数字越大,小数点后的精度就越低。 这是因为有固定的精度可用于表示整个数字。

Welcome to floating point math. There are many numbers which cannot be represented in standard floating point notation and come out just a tiny bit off.

This is easily illustrated as follows:

(1..10).collect do |i|
  v = ((10**i).to_f + 0.7)
  puts "%13.1f = %.30f" % [ v, v.modulo(1) ]
end

Where the result is:

         10.7 = 0.699999999999999289457264239900
        100.7 = 0.700000000000002842170943040401
       1000.7 = 0.700000000000045474735088646412
      10000.7 = 0.700000000000727595761418342590
     100000.7 = 0.699999999997089616954326629639
    1000000.7 = 0.699999999953433871269226074219
   10000000.7 = 0.699999999254941940307617187500
  100000000.7 = 0.700000002980232238769531250000
 1000000000.7 = 0.700000047683715820312500000000
10000000000.7 = 0.700000762939453125000000000000

Notice that the larger the number gets, the lower the precision beyond the decimal place. This is because there is a fixed amount of precision available to represent the entire number.

鸵鸟症 2024-08-08 18:58:26

这是典型的浮点舍入。 您根本无法用 Float 中的固定位数表示每个十进制数,因此某些值会四舍五入到可以表示的最接近的值。

因此,建议您不要比较 Floats 是否相等。 比较小于或大于,但决不完全相等。

http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding

简单来说,并不是“应该准确”的情况。 不要指望浮点小数会出现这种情况。

This is typical floating point rounding. You simply cannot express every single decimal number in the fixed number of bits in a Float, so some values are rounded to the nearest value that can be represented.

Due to this, it is suggested that you don't compare Floats for equality. Compare for less than or greater than, but never exact equality.

http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding

Simply, it is not the case that "it should be exact". Don't expect that from floating point decimals.

So尛奶瓶 2024-08-08 18:58:26

这是因为不可能精确地表示所有浮点数。

this is because it's not possible to represent all floating point numbers exactly.

筱武穆 2024-08-08 18:58:26

浮点数并不精确。 简而言之,不可能在有限数量的位中存储无限数量的值。

较长的版本是每个计算机科学家应该了解的浮点运算

Floating points are not exact. The short version is it's not possible to store an infinite amount of values in a finite amount of bits.

The longer version is What Every Computer Scientist Should Know About Floating-Point Arithmetic

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文