tc39 / proposal-number-fromstring

{BigInt,Number}.fromString(string, radix)
https://mathiasbynens.github.io/proposal-number-fromstring/
65 stars 7 forks source link

Edge cases, some questions #24

Open zloirock opened 11 months ago

zloirock commented 11 months ago

I see some inconsistency in the readme and the spec draft, let's clarify some edge cases.

NaN - it's a valid value for the number type, a valid output for Number#toString, but it's not a number - should it be parsed?

Infinity - almost the same.

-0 is a valid number, but not a valid output for Number#toString, should it throw?

Should something like Number.fromString('20000000000001', 16) throw an error? It's a valid mathematical number, but it can't be represented in the number type / Number#toString.

zloirock commented 11 months ago

Number.fromString('00101010', 2) in the readme is a valid example, however, '00101010' is not a valid output for Number#toString.

zloirock commented 11 months ago

What about exponential notation, like Number.fromString('1e+128', 10) (also a valid output for Number#toString)? Here is nothing about it.

ljharb commented 11 months ago

NaN and Infinity are both numbers.

zloirock commented 11 months ago

They are number by type, but are not numbers in math. They are not valid values in the spec draft. NaN case in the readme is not clear, Infinity is just missed.

ljharb commented 11 months ago

JS doesn't have math, so that's irrelevant. JS has IEEE 754, in which NaN and Infinity are both number values.

If the spec draft considers them invalid, then the spec draft is incorrect, but for a stage 1 proposal not yet seeking advancement, the spec draft is also irrelevant.

zloirock commented 11 months ago

Not exactly. This proposal operates mathematical number values, for the ES spec they both are not mathematical values.

ljharb commented 11 months ago

In stage 1, this proposal is just a problem to solve. In order to solve it, we'd have to fully support JS number values, including those.

The intention as stated in the readme is

method which acts as the inverse of {BigInt,Number}.prototype.toString(radix = 10). It accepts only strings that can be produced by {BigInt,Number}.prototype.toString(radix = 10), and throws an exception for any other input.

Thus, only things that can be the output of toString would be accepted, and implied is that everything that Number toString can output must also be accepted. The spec details aren't worth thinking about until the proposal is ready for stage 2.

zloirock commented 11 months ago

It's the reason why I raised this issue, however too much in the description of the problem and solution contradicts this. Let's wait for an explanation from the proposal authors / champions -)