Thursday, April 29, 2010

Bug with BigDecimal in Ruby 1.8.7 (patchlevel 173)

got out my brand new iMac today, and when I checked out my project, I had a bunch of tests failing. I thought I might have checked out an old version, but no everything was current and passing on my old dev machine. It wasn't in in my code. It wasn't even in rails. It was a problem with Ruby 1.8.7 (patchlevel 173). After hours of digging, behold the following console output in irb:


>> require 'bigdecimal'
=> true
>> BigDecimal.new(103123.98.to_s).to_f
=> 103123.98
>> BigDecimal.new(100123.98.to_s).to_f
=> 101239.8

Strangely enough, if you have a "0" as the fourth digit to the left from the decimal point when you build a big decimal object, it cuts it out. I have no idea why this is. I don't have time to investigate it. But I do know it doesn't happen with ruby 1.8.6, which is what I'm running in production, so for the moment it's not a real problem for me (except I had to change all my tests that had a 0 in that decimal place temporarily while I sort out bringing an older version of ruby onto my machine).

*sigh*

2 comments:

Piyush said...

I cannot reproduce this. I am on ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]

Ethan Vizitei said...

Could it be affected by the fact that I'm on Mac OS X 10.6? Maybe the problem is deeper than ruby?