Yes, the output string equals the float when spat back into a JS interpreter, but that's besides the point. It's not what a *human* expects. This is like the people complaining about things like undefined behavior in C. Except the latter is at least often useful for performance.
-
-
This stuff hurts people trying to learn the language, because instead of doing what's natural and obvious, things will work until suddenly they don't. The person learning will be bit by bugs, and will be told "that's just the way it is", but it shouldn't *be* that way.
1 reply 0 retweets 0 likes -
You work on TC39, so suggestion: change toString so that any numbers ≥ 2⁵³ or ≤ -2⁵³ are stringified in e-notation, or, alternatively, with the full integer digit expansion and a trailing ".". This makes it clear when precision is limited, without phantom/incorrect digits.
1 reply 0 retweets 2 likes -
Replying to @marcan42 @kentaromiura and
I doubt that’s feasible. I bet it would break lots of existing code.
@littledan1 reply 0 retweets 0 likes -
toString always outputs a string which, when parsed as a Number, rounds to the same value. There are multiple strings which would round to the same Number. I think ending it with a bunch of 0's is often more intuitive, which would motivate that choice.
1 reply 0 retweets 1 like -
Replying to @littledan @mathias and
If you'd like to see the fun floating point bits, if you're trying to learn how floats work, you can do that with Number.prototype.toPrecision, toExponential, toFixed, which omit the logic to put 0's at the end.
1 reply 0 retweets 0 likes -
Replying to @littledan @mathias and
My issue is the lack of any evidence that the number is a float; that rounding occurred. JS is in the awkward position of using floats for the use cases of ints. Presumably this drove the decision to have toString output just digits when the float is an exact integer value.
1 reply 0 retweets 0 likes -
Replying to @marcan42 @littledan and
But then toString goes and *rounds* larger ints - at which point they're not floats trying to be ints any more - yet it still outputs just digits. And that is just confusing and messy.
1 reply 0 retweets 0 likes -
Replying to @marcan42 @littledan and
Sure, there are ways to get the full value out. My point here is that the default behavior is unintuitive and misleading in this particular case. Nobody expects an integer to have been rounded. Having to deal with the 53-bit ceiling is bad enough; toString hiding it is worse!
1 reply 0 retweets 1 like -
Unfortunately, the fact that JavaScript Numbers are IEEE 64-bit floats is just something JS devs will have to learn. I don't think toString is hiding anything here--ending in a bunch of zeros implies not as much precision as if it ended in a lot of non-zero digits.
2 replies 0 retweets 1 like
There's just no reason to have that awkward gap between where it pads with zeroes and where it switches to e-notation. If the goal was to convey the loss of precision, then it should use e-notation or at least a decimal point, like other languages do.
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.