Js follows IEEE 754, quite sure that's expected behaviour
-
-
Hector Martin Retweeted Hector Martin
Nope, it doesn't. This is *specified* in IEEE 754. JS is violating the spec.https://twitter.com/marcan42/status/1009008156322623489 …
Hector Martin added,
Hector Martin @marcan42Here's the IEEE 754 spec explicitly saying conversions to integer must use the exact integer when the value is representable in both formats. JS is violating the IEEE 754 standard. Please don't make excuses about different representations for the same float. This is *broken*. pic.twitter.com/pUEdUCxGEdShow this thread2 replies 0 retweets 1 like -
Replying to @marcan42 @kentaromiura and
I think you’re confused about what double-precision floating-point means. See: https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type … https://en.wikipedia.org/wiki/Double-precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64 … https://developers.google.com/web/updates/2018/05/bigint … https://mathiasbynens.be/demo/integer-range … For integers, JavaScript uses 53 bits to store the digits and + 1 for the sign.
2 replies 0 retweets 4 likes -
Replying to @mathias @kentaromiura and
I think you're confused about what "integer" means. The 64-bit IEEE754 floating point value represented by the (big-endian) bytes 43d0000000000000 *represents* the int 4611686018427387904. It does *not* represent the int 4611686018427388000. The latter is not representable.
1 reply 0 retweets 1 like -
You can *round* 4611686018427388000 to the nearest representable integer in that format, which is 4611686018427387904. But there is no reason to conjure up 4611686018427388000 when converting back to integer. That is just wrong and broken.
1 reply 0 retweets 1 like -
Yes, the ECMAScript spec says toString must be implemented in this (broken) way. This is insane, and no other sensible language does this. It is misleading and wrong. https://tc39.github.io/ecma262/#sec-tostring-applied-to-the-number-type …
1 reply 0 retweets 0 likes -
Numbers are floats in Lua <5.3 too, but it never pretends floats are integers that they aren't. Smaller exact-int floats are stringified as integers, then after a threshold (lower than the 53-bit limit) it switches to exp notation. string.format will give you all the digits.pic.twitter.com/cimunNayeM
1 reply 0 retweets 0 likes -
Ultimately this is a problem of the principle of least surprise. To any reasonable human, a string of digits with no decimal point is an *integer*. However, to JavaScript, it's a *float*. JS is abusing this point to produce unexpected results from toString.
1 reply 0 retweets 0 likes -
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.
1 reply 0 retweets 0 likes -
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.
-
-
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 - Show replies
New conversation -
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.