Skip to content

Instantly share code, notes, and snippets.

@wteuber
Last active December 26, 2015 20:39
Show Gist options
  • Select an option

  • Save wteuber/7210663 to your computer and use it in GitHub Desktop.

Select an option

Save wteuber/7210663 to your computer and use it in GitHub Desktop.
Ruby's floating point errors
# 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