Bug #6944

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

Added by Adriano Mitre almost 3 years ago. Updated almost 3 years ago.

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

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)

Associated revisions

Revision 36854
Added by Nobuyoshi Nakada almost 3 years ago

bigdecimal.c: check underflow

  • ext/bigdecimal/bigdecimal.c (BigDecimal_to_f): check underflow since strtod() sets errno to ERANGE at underflow too. [Bug #6944]

Revision 36854
Added by Nobuyoshi Nakada almost 3 years ago

bigdecimal.c: check underflow

  • ext/bigdecimal/bigdecimal.c (BigDecimal_to_f): check underflow since strtod() sets errno to ERANGE at underflow too. [Bug #6944]

History

#1 Updated by Nobuyoshi Nakada almost 3 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r36854.
Adriano, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


bigdecimal.c: check underflow

  • ext/bigdecimal/bigdecimal.c (BigDecimal_to_f): check underflow since strtod() sets errno to ERANGE at underflow too. [Bug #6944]

Also available in: Atom PDF