Bug #2364: Float conversion of NaN in 1.8.x
http://redmine.ruby-lang.org/issues/show/2364

Author: Marc-Andre Lafortune
Status: Open, Priority: Normal
Category: core, Target version: Ruby 1.8.8
ruby -v: ruby 1.8.8dev (2009-11-14) [i386-darwin10.0.0]

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)


----------------------------------------
http://redmine.ruby-lang.org