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
Integers are not always printed as integers #811
Comments
TBH I'm not sure why it is printing this way, I thought I'm using |
OK, Go's
I am now foraging the strconv package for a more ideal choice. |
I researched how Firefox and the Lua CLI print numbers. Both JavaScript and Lua only have floating point numbers. It is not really a matter of being a whole number; they actually use similar heuristics as Go's %g, just with larger thresholds. For example, In Firefox, the threshold is 21 for positive exponents, and -7 for negative exponents:
In Lua CLI, the thresholds are 14 and -5
In Go's
The threshold may be actually based on the binary exponent, not the decimal exponent. |
It is not possible to change the threshold, other than copying But we can implement the threshold by not using
|
Corrections for step 2:
|
For the interest of choosing a good threshold, I surveyed more languages. This is the complete result of all languages I surveyed:
I will go with 14 and -5. |
Hmm, the heuristics should be a bit more sophisticated than that: the positive threshold should apply to the number of trailing zeros, not the exponent. |
Hmm, at least Racket and Chez Scheme's algorithms are both more sophisticated than just using the number of trailing zeroes:
|
Here is another go at reverse-engineering Racket's algorithm. I think the part with the positive threshold is more sophisticated:
|
I think it would be preferable if “proper” integers would be represented as such and not in scientific notation. Assuming that the internal representation of numbers is an IEEE double, that means any integer whose absolute value is less than 2^53 (9007199254740992), since these are exactly representable as doubles. In particular, I find the following behaviour less than optimal:
It gets in the way of serving those numbers to other programs that might expect integers, not floating point numbers.
Larger numbers, though they too may represent integers, should still be printed in scientific format to indicate loss of accuracy.
The text was updated successfully, but these errors were encountered: