From: "adrianomitre (Adriano Mitre)" Date: 2012-08-28T17:52:08+09:00 Subject: [ruby-core:47342] [ruby-trunk - Bug #6944][Open] BigDecimal#to_f breaks for numbers too small to be representable in Float Issue #6944 has been reported by adrianomitre (Adriano Mitre). ---------------------------------------- Bug #6944: BigDecimal#to_f breaks for numbers too small to be representable in Float https://bugs.ruby-lang.org/issues/6944 Author: adrianomitre (Adriano Mitre) Status: Open Priority: High Assignee: Category: lib Target version: 1.9.2 ruby -v: ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux] BigDecimal numbers too small to be representable in Float are incorrectly converted to +/-Infinity instead of 0.0 (or -0.0). Things seem to broke down when exponent is about -308. # with 307, ok BigDecimal('1e-307').to_f # => 1.0e-307 BigDecimal('-1e-307').to_f # => -1.0e-307 # with 308, broken but signedness is correct BigDecimal('1e-308').to_f # => Infinity BigDecimal('-1e-308').to_f # => -Infinity # with 350 not even signedness is correct BigDecimal('1e-350').to_f # => -Infinity BigDecimal('-1e-350').to_f # => -Infinity # Workaround: resort to BigDecimal#to_s and Float(.) x=BigDecimal('1e-350') Float(x.to_s) -- http://bugs.ruby-lang.org/