Created
May 21, 2011 02:39
-
-
Save tiegz/984177 to your computer and use it in GitHub Desktop.
Argh
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
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 |
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
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
guao.