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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This sounds like a typical floating point problem, but hidden inside. While 0.025 may be represented exactly, the function
to_rwill no doubt perform various floating-point operations internally which are necessarily inexact. The result3602879701896397/144115188075855872will no doubt match the intermediate, transformed version ofamore closely than your proposal1/40.Now
3602879701896397/144115188075855872is extremely close to being the same as1/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
Cimplementation ofFloat#to_rfor more details.