开发者

Ruby float to_r calculation. decimals to fractions

Can someone clarify why开发者_如何学运维 ruby returns such big numbers when using to_r

for example:

a = 0.025
a.to_r
3602879701896397/144115188075855872.

Why not use 1/40?


This sounds like a typical floating point problem, but hidden inside. While 0.025 may be represented exactly, the function to_r will no doubt perform various floating-point operations internally which are necessarily inexact. The result 3602879701896397/144115188075855872 will no doubt match the intermediate, transformed version of a more closely than your proposal 1/40.

Now 3602879701896397/144115188075855872 is extremely close to being the same as 1/40. But it is not quite equal, so is not simplified.

For more information, look at some of the previous questions related to inexact floating point. This is a nuanced case and a good question therefore, but has its fundamentals in the same things. I'm looking into the ruby C implementation of Float#to_r for more details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜