-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Debug-print negative not-a-number as "-NaN" #54235
Conversation
5.12.1 allows "with an optional preceding sign" for nans
r? @aidanhs (rust_highfive has picked a reviewer for you, use r? to override) |
I'm not sure this is a good idea. The benefit is small because the sign of a NaN is even less important than the sign of zero. For example, Additionally, there are potential costs: it could cause some confusion, and like any other formatting change it's risky because third party code may not be prepared to handle it – e.g., there might be code comparing the formatting result to the string "NaN". |
What was the original purpose of giving a sign to NaNs? What's the cost of not printing their sign? |
I don't know the IEEE 754 working group's intent, but if I had to guess I'd say it is just a byproduct of the sign-magnitude encoding, it's not used for anything and in fact explicitly unspecified in most cases. As §6.3 states, the standard "does not interpret the sign of a NaN" except when it comes to "operations on bit strings".
Well, people won't see in their debugging output that a NaN has a negative sign bit. As the sign bit is almost entirely meaningless for NaNs (see earlier comment), I can't really imagine this ever making a difference, but you never know. |
Some do seem to use it; like (I definitely agree with not having signs on NaN in |
Yes, sorry if I wasn't clear: I only meant it's irrelevant even more often than signed zeroes, not that it never matters. The standard also names negate, abs and copySign as operations that interpret the sign, though for abs that just refers to the result (the sign bit is cleared even for NaN inputs) and I don't think we have a general
Huh, if you told me that yesterday I would have said it's a bug (and I wrote that code!). I am reasonably sure it's unintentional and we don't have tests for it, though of course we can't just change it now. |
|
I do not have an opinion in terms of what the Debug impl should do, though. On one hand more information is good, on the other hand if we are printing the sign, why not the payload as well? |
Because these are different paragraphs in 5.12.1:
|
Triage; @aidanhs Hello, are you able to review this PR? |
Ping from triage @rkruppe / @scottmcm / @rust-lang/libs: This PR seems to have somewhat stalled. Could you comment on how you think this can move forward? (Also, would it make sense to have a crater run to see how many crates rely on the formatting of NaN in their tests, even though they probably shouldn't?) |
This seems like a good implementation to me in that |
As discussed up-thread, although the sign of a NaN is not completely unobservable, it is almost so (only via |
I would lean toward not doing this. |
I opened this after working on the float total ordering PR, where it looked weird to have some NaNs on one side of the actual numbers, and some on the other side. Basically, the more functions we add that do care about signs on NaNs, the more reasonable this is. But I'm not all that attached to it; if libs decides it's better closed that's fine by me. |
I personally ran into this awhile ago when working with spec tests for wasm, where the spec verifies actual bit patterns of floats and requires you to produce negative NaN in some situations, and then verifies that. I don't really understand the harm here in printing a |
Should we also be including the signal bit and payload? |
While I don't think we'd go out of our way to implement that I wouldn't personally reject a PR doing so. There's a canonical encoding for NaN that almost everything uses which we could render as |
@sfackler If we were to decide we want to truly include all information that we possibly can, then yes, certainly the signaling bit and payload should be included as well -- they too are observable using @alexcrichton Thank you for pointing out that debugging output could also be used while tracking down a difference in float bit patterns rather than float numerical values or program behavior caused by numerical values. Keep in mind though that in those cases the sign bit is just a small part of the picture and the programmer also needs to see the signaling bit and payload, so I'm a bit surprised to see you apparently less enthusiastic about including that information than about including the sign. Still, I think you're not doing my position justice when you summarize it as just "isn't used most of the time". It's not that the sign bit of a NaN can't be relevant for debugging, clearly it can be, it's just that this benefit is real but very small so it does not outweigh the (also small) risk of breaking user code. People implement their own string -> float conversions, they compare If this change were proposed pre-1.0, a little breakage would not be a concern and I would welcome this PR, but at this point in time tweaking the Debug output format doesn't seem worth risking for the (real, but extremely rare) improvement in debugging experience to me. Ultimately this is a judgement call and I'm happy to let the libs team make it, I just want to make sure we all agree about what things are on each side of the scale. From your comments here so far I am not sure whether you've fully recognized and weighed everything I see on the scale. If you have and just see the scales tip the other way, great. |
Sorry I don't mean to discount anything. If we want to close this I'm all for that, I was just trying to provide a counterpoint. I don't really feel that strongly about this, and seems like others want to close. @rfcbot fcp close |
@rfcbot fcp close |
Team member @alexcrichton has proposed to close this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Hang on, I might have missed something in the discussion above, but haven't we always said that the exact output of a |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
Correct, we are free to change Debug representations including this one if we wanted to. |
5.12.1 allows "with an optional preceding sign" for nans
cc @rkruppe, #53938 (comment)
Posted for "is this a bad idea?" and travis right now. I want to add some doc text and tests about it before it's ready to be merged.