Project

General

Profile

Backport #2364

Float conversion of NaN in 1.8.x

Added by marcandre (Marc-Andre Lafortune) over 9 years ago. Updated about 8 years ago.

Status:
Closed
Priority:
Normal
[ruby-core:26733]

Description

=begin
Trying to fix issue #2349 for 1.8.8 but running into a small problem: conversion to a float will fail if the value converted corresponds to NaN:

$ ruby19 -r bigdecimal -e "p Float(BigDecimal('NaN'))"
NaN
$ ruby18 -r bigdecimal -e "p Float(BigDecimal('NaN'))"
ArgumentError: invalid value for Float()

This is true of any class for which #to_f returns NaN.

This makes it impossible for BigDecimal("NaN") to be converted to a float in 1.8.

Can anyone think of a reason not to bring 1.8 inline with 1.9 (by allowing NaN)?

diff --git a/object.c b/object.c
index 4704ebf..dd67039 100644
--- a/object.c
+++ b/object.c
@@ -2486,13 +2486,7 @@ rb_Float(val)
break;

    default:
  • {
  • VALUE f = rb_convert_type(val, T_FLOAT, "Float", "to_f");
  • if (isnan(RFLOAT(f)->value)) {
  • rb_raise(rb_eArgError, "invalid value for Float()");
  • }
  • return f;
  • }
  • return rb_convert_type(val, T_FLOAT, "Float", "to_f"); } }

(Doesn't generate any error in make test or test-all)
=end

Associated revisions

Revision 2c0b73f1
Added by wyhaines (Kirk Haines) about 9 years ago

object.c: Backport #2364 [ruby-core:26733]; Allow result of to_f to be NaN to permit conversion from BigDecimal('NaN') to Float.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_6@28392 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

History

#1

Updated by ujihisa (Tatsuhiro Ujihisa) over 9 years ago

  • Status changed from Open to Assigned
  • Assignee set to nobu (Nobuyoshi Nakada)

=begin

=end

#2

Updated by marcandre (Marc-Andre Lafortune) over 9 years ago

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

=begin
This issue was solved with changeset r26032.
Marc-Andre, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

=end

#3

Updated by shyouhei (Shyouhei Urabe) over 9 years ago

  • Status changed from Closed to Assigned
  • Assignee changed from nobu (Nobuyoshi Nakada) to shyouhei (Shyouhei Urabe)

=begin

=end

#4

Updated by shyouhei (Shyouhei Urabe) over 9 years ago

  • Assignee changed from shyouhei (Shyouhei Urabe) to wyhaines (Kirk Haines)

=begin
1.8.6 has this issue.
=end

#5

Updated by wyhaines (Kirk Haines) about 9 years ago

  • Status changed from Assigned to Closed

=begin
This issue was solved with changeset r28392.
Marc-Andre, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

=end

Also available in: Atom PDF