Skip to content
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

Real.toString ignores the sign of zero #604

Open
p-ouellette opened this issue Feb 15, 2025 · 2 comments
Open

Real.toString ignores the sign of zero #604

p-ouellette opened this issue Feb 15, 2025 · 2 comments

Comments

@p-ouellette
Copy link

val _ = print (Real.toString ~0.0 ^ "\n")

This prints "0". I expected it to print "~0", as SML/NJ does.

@MatthewFluet
Copy link
Member

It seems that it does so purposefully; the code for Real.toString (which uses the GEN 12 adaptive notation) is at:
https://github.com/MLton/mlton/blob/master/basis-library/real/real.sml#L561
The determination of the sign is via x < 0.0 and floating-point comparisons ignore the sign of zero. This code for the adaptive notation is taken straight from the Basis Library specification:
https://smlfamily.github.io/Basis/string-cvt.html#SIG:STRING_CVT.realfmt:TY
(See the code under Discussion at the bottom of the page.)

But, maybe there is precedent in other languages. And, I'm not sure what happens with the other real format specifiers.

@fweimer
Copy link

fweimer commented Feb 17, 2025

Java, Python, contemporary C implementations (and thus Lua), C++ implementations, Javascript all print -1e-308 / 1e308 as some variant of -0.0. I'm surprised about how uniform it is. (Not sure if it's part of the referenced numerics standards for C and C++, or if it's just wide-spread implementation practice.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants