Last active
December 26, 2015 20:39
-
-
Save wteuber/7210663 to your computer and use it in GitHub Desktop.
Ruby's floating point errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Ruby's floating point errors | |
| # "Sometimes it's easier avoiding a problem than solving it." | |
| 0.2 > (1.to_r/5) # false | |
| 0.2.to_r == 0.2 # true | |
| 0.2.to_r > (1.to_r/5) # true | |
| # There is a certain range of floating point calculation errors, which is ignored. | |
| 0.2.to_r # (3602879701896397/18014398509481984) | |
| 0.2.to_r - (1.to_r/5) # (1/90071992547409920) (it should be 0) | |
| # The (sometimes) ignored floating point error is (1/90071992547409920). | |
| # Yes it's very small, but it's still there. | |
| # To avoid these kind of errors, don't use floats. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment