Project

General

Profile

Actions

Backport #2364

closed

Float conversion of NaN in 1.8.x

Added by marcandre (Marc-Andre Lafortune) over 14 years ago. Updated almost 13 years ago.


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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0