Skip to content

Instantly share code, notes, and snippets.

@tiegz
Created May 21, 2011 02:39
Show Gist options
  • Save tiegz/984177 to your computer and use it in GitHub Desktop.
Save tiegz/984177 to your computer and use it in GitHub Desktop.
Argh
puts a = (2500 * 56) / 10000.0 # => 14.0
puts b = (250 * 5.6) / 100.0 # => 14.0
puts c = 25 * 0.56 # => 14.0
puts a % 1 # => 0 ... Expected
puts b % 1 # => 0 ... Expected
puts c % 1 # => 1.77635683940025e-15 .. Not Expected
@iamjwc
Copy link

iamjwc commented May 21, 2011

guao.

@iamjwc
Copy link

iamjwc commented May 21, 2011

all better!

ruby-1.8.7-p330 :001 > c = 25 * 0.56
=> 14.0
ruby-1.8.7-p330 :002 > c % 1
=> 1.77635683940025e-15
ruby-1.8.7-p330 :003 > class Float; alias :old_mod :%; def %(rhs); old_mod(self + 1 - 1) end end
=> nil
ruby-1.8.7-p330 :004 > c % 1
=> 0.0

@tiegz
Copy link
Author

tiegz commented May 23, 2011

hahaha; the power of Ruby lies in the ease to fix itself :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment