Ruby thinks (19.99 * 100).to_i = 1998
March 26th, 2009 By: Daniel>> (19.99 * 100).to_i => 1998 >> (9.99 * 100).to_i => 999
Someone please tell me what’s up with that!
In the meantime:
>> (19.99 * 100).to_s.to_i => 1999
>> (19.99 * 100).to_i => 1998 >> (9.99 * 100).to_i => 999
Someone please tell me what’s up with that!
In the meantime:
>> (19.99 * 100).to_s.to_i => 1999
April 6th, 2009 at 2:18 pm
That is crazy! I am not going to use ruby to do math.
April 15th, 2009 at 5:27 pm
“This behavior is not specific to Ruby. The reason Ruby behaves like this is that it adheres
to the IEEE standard for floating point numbers, which is common to most programming languages.”
From http://www.ruby-forum.com/topic/178503
You can read more about it at:
http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
Or a lot more about it at:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
May 16th, 2009 at 9:28 pm
Wes: I’m not surprised that this is an IEEE float issue.
Leo: Wrong conclusion. The right conclusion: don’t use *IEEE floats* to do math. Ruby has a BigDecimal library that completely gets around this problem by not using IEEE floats. Use it for math.
May 19th, 2009 at 2:47 pm
Just to verify what Marnen wrote, using a BigDecimal works with the example Daniel posted.
>> require ‘bigdecimal’
>> (BigDecimal.new(’19.99′) * 100).to_i
=> 1999