Feature #20705
closed
Should "0.E-9" be a valid float value?
Added by kou (Kouhei Sutou) 3 months ago.
Updated about 1 month ago.
Description
Ruby doesn't accept "0.E-9" as a valid float value:
$ ruby -e 'Float("0.E-9")'
<internal:kernel>:218:in 'Kernel#Float': invalid value for Float(): "0.E-9" (ArgumentError)
from -e:1:in '<main>'
But other systems accept "0.E-9" as a valid float value:
PostgreSQL:
=> select 0.E-9;
?column?
-------------
0.000000000
(1 row)
MySQL:
> select 0.E-9;
+-------+
| 0.E-9 |
+-------+
| 0 |
+-------+
1 row in set (0.00 sec)
Python:
$ python3 -c 'print(0.E-9)'
0.0
Node.js:
$ nodejs -e 'console.log(0.E-9)'
0
Should Ruby accept "0.E-9" as a valid float value?
FYI: I don't have an opinion of this. I just realized this by an issue from an user of a maintained library by me: https://github.com/apache/arrow/issues/43877
It feels reasonable to relax the to_f
conversion.
Regarding Python and Node.js examples, they are literals and different things, I think.
Note that they also treat 0.
as a floating point number literal.
$ python3 -c 'print(0.)'
0.0
$ node -e 'console.log(0.)'
0
we need to be careful with this, because while 0.1E-9
is also a valid ruby literal,
0.E-9
is not. (unknown method E for 0)
1E-9
is valid literal again
We are not going to change the literal format of floating point values. But I think it's good to make Float/to_f to accept "0.e-9".
Matz.
It seems a bug in missing/dtoa.c.
But I think it's good to make Float/to_f to accept "0.e-9"
Changing String#to_f
introduces incompatibility:
$ ruby -ve "p '1.e-9'.to_f"
ruby 3.3.4 (2024-07-09 revision be1089c8ec) [x86_64-linux]
1.0
I'd like to enhance string to float conversion in general. I might introduce small incompatibility, but impact will be small.
Matz.
- Status changed from Open to Closed
- Assignee set to nobu (Nobuyoshi Nakada)
Also available in: Atom
PDF
Like0
Like0Like1Like0Like0Like0Like0Like0Like0Like0Like0