Backport #2364
Float conversion of NaN in 1.8.x
| Status: | Closed | Start date: | 11/14/2009 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | % Done: | 100% |
||
| Category: | - | |||
| Target version: | - |
Description
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)
Associated revisions
* object.c (rb_Float): Allow results of to_f to be NaN [ruby-core:26733]
object.c: Backport #2364 [ruby-core:26733]; Allow result of to_f to be NaN to permit conversion from BigDecimal('NaN') to Float.
History
Updated by ujihisa . about 2 years ago
- Status changed from Open to Assigned
- Assignee set to Nobuyoshi Nakada
Updated by Marc-Andre Lafortune about 2 years ago
- Status changed from Assigned to Closed
- % Done changed from 0 to 100
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.
Updated by Shyouhei Urabe almost 2 years ago
- Status changed from Closed to Assigned
- Assignee changed from Nobuyoshi Nakada to Shyouhei Urabe
Updated by Shyouhei Urabe almost 2 years ago
- Assignee changed from Shyouhei Urabe to Kirk Haines
1.8.6 has this issue.
Updated by Kirk Haines over 1 year ago
- Status changed from Assigned to Closed
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.