Search code examples
ruby-on-railsbigdecimal

BigDecimal to_s does not match to_f


Is the BigDecimal class broken? It seems like the following should never, ever occur: Note that a.to_f != a.to_s.to_f

a.class => BigDecimal

a.to_f => 18658.1072928

a.to_s => "10865.81072928"

b.class => BigDecimal

b.to_f => 10000.0

b.to_s => "10000.0"

(a - b).to_f => 865.81072928

a.to_f - b.to_f => 8658.1072928

Any ideas as to what might be going wrong? We are running ruby 1.8.7p72 on our servers and 1.8.7p173 on our local machines.


Solution

  • If you're running Mac OS X 10.6.3 then this might be an explanation: http://www.abletech.co.nz/2010/03/osx-10-6-3-installs-new-ruby-with-faulty-bigdecimal/

    To get rid of the problem you could use RVM.