Project

General

Profile

Actions

Bug #6944

closed

BigDecimal#to_f breaks for numbers too small to be representable in Float

Added by adrianomitre (Adriano Mitre) over 11 years ago. Updated over 11 years ago.

Status:
Closed
Assignee:
-
Target version:
ruby -v:
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
Backport:
[ruby-core:47342]

Description

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)

Actions

Also available in: Atom PDF

Like0
Like0