Project

General

Profile

Actions

Feature #20705

closed

Should "0.E-9" be a valid float value?

Added by kou (Kouhei Sutou) 4 months ago. Updated 2 months ago.

Status:
Closed
Target version:
-
[ruby-core:118979]

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

Updated by nobu (Nobuyoshi Nakada) 4 months ago

It feels reasonable to relax the to_f conversion.

Regarding Python and Node.js examples, they are literals and different things, I think.

Updated by mame (Yusuke Endoh) 4 months ago

Note that they also treat 0. as a floating point number literal.

$ python3 -c 'print(0.)'
0.0


$ node -e 'console.log(0.)'
0

Updated by Hanmac (Hans Mackowiak) 4 months ago

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

Updated by matz (Yukihiro Matsumoto) 3 months ago

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.

Updated by nobu (Nobuyoshi Nakada) 3 months ago

It seems a bug in missing/dtoa.c.

Updated by kou (Kouhei Sutou) 3 months ago

Implementation: https://github.com/ruby/ruby/pull/11559

Should we also accept "0." as @mame (Yusuke Endoh) showed in #2? The implementation includes "0." support.

FYI: to_f already accepts "0.". Float didn't accept "0.".

Updated by mrkn (Kenta Murata) 3 months ago ยท Edited

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

Updated by matz (Yukihiro Matsumoto) 3 months ago

I'd like to enhance string to float conversion in general. I might introduce small incompatibility, but impact will be small.

Matz.

Updated by kou (Kouhei Sutou) 3 months ago

@nobu (Nobuyoshi Nakada) Could you open a PR based on your implementation https://github.com/nobu/ruby/tree/float-dtoa ?
My implementation doesn't accept 0xf.p0 but your implementation does.

Updated by kou (Kouhei Sutou) 2 months ago

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

Also available in: Atom PDF

Like0
Like0Like1Like0Like0Like0Like0Like0Like0Like0Like0