-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected values for certain Integer literals #530
Comments
My suggestion would be:
This would allow literals that cannot be stored with perfect precision, and would be smaller than the full range of values possible in JS, but to me seems like a reasonable compromise for cross-platform code. |
+1, the JVM backend already performs this kind of validation. |
I use a BigInteger to parse natural literals, so I can just check that they are less than 64 bits long. |
This one is pretty weird: // min in hex
print (-#8000_0000_0000_0000); // negative, same as Java
print (-(#8000_0000_0000_0000)); // positive, opposite of Java
// so...
print (-#8000_0000_0000_0000 == -(#8000_0000_0000_0000)); // ** false! In Java, negating min has no effect, but the operation does of course change the sign in JS. If 64-bit hex literals are to be supported in JS, perhaps it makes sense for all of the above numbers to be positive. Conceptually, I guess, the literal would not be considered to include the "-" sign. |
Negative decimals still produce unexpected results: // min
print (-9223372036854775808); // ok, good
// one less than min 64
print (-9223372036854775809); // no compile error
// a lot less than min: -(2^64 - 1)
print (-18446744073709551615); // -1 |
Perhaps I should revert to parsing Long directly instead of using BigInteger? |
@chochos why, precisely do you need to parse it at all? |
Because I need to convert it to base 10, and check that it's a value within valid range |
Ah, so only for binary and hex literals? |
Binary and hex literals need to be parsed and converted to base 10. Base 10 literals need to be validated against the max range, so I parse all number literals. |
@jvasileff can you check if this is working ok now? I've tested all the cases mentioned in this issue and they work the same way as in JVM. |
@chochos I tried out several things, and going with the 64-bit scheme, I have a couple thoughts on negatives.
print(-9223372036854775808); // ALLOWED
print(-(9223372036854775808)); // DISALLOWED
print(9223372036854775808.negated); // DISALLOWED In the disallowed cases, the literal would actually be a positive value that is out of range. Of course, due to platform differences, we correctly have: print(-9223372036854775808); // negative on JVM & JS
print(-(-9223372036854775808)); // negative on JVM, positive on JS
So, I believe all of the following should print the same positive number on JS (even though they are all negative on the JVM): print(-#8000_0000_0000_0000);
print(#8000_0000_0000_0000.negated);
print(-(#8000_0000_0000_0000));
print(-(-(-#8000_0000_0000_0000))); And, the following should print the same negative number on both JS & JVM: print(#8000_0000_0000_0000);
print(-#8000_0000_0000_0000.negated);
print(-(-#8000_0000_0000_0000)); |
Worth noting, an adjacent print(-9223372036854775808.negated); |
Moving to 1.3, that's really edge-case. |
Integer literals of the following forms produce unexpected values:
The text was updated successfully, but these errors were encountered: