-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Serialization of float minus zero should include the minus sign. #20596
Comments
While that's clearly desirable for the Show trait, it's not obvious that fmt::String should have that behaviour? |
I don’t see why not. +0 and -0 are different values in IEEE 754. |
Right, but as per the recent RFC, the String trait (being renamed to Display) is intended for user-facing output. For users, negative zero makes no sense, as they're unlikely to even be aware of IEEE 754. The Show trait (being renamed to Debug) is what's used for debugging, so that should distinguish the two. |
shrug |
@aturon what do you think? |
Even if it's not the default, this behaviour should be available somehow. At the moment I have to do silly things like |
I agree with @Diggsey that doing this in |
I think I'd prefer that we be consistent and transparent, and print
Regardless, if the new behavior is desired, then we should test for the edge cases I've described. Correction: C# does not print |
I noticed that this test case currently prints println!("{}", -1e-9); |
@rprichard Yes, my PR does not (well, should not) change behavior for small negative numbers. The existing code is pretty naive w.r.t. rounding (and probably rather inexact at the rounding it does perform), but this is a separate issue. As for scientific notation, I'm not sure if this is desired: like |
Fixes rust-lang#20596 by making `Debug` render negative zero with a `-` without affecting the behavior of `Display`. While I was at it, I also removed some dead code from `float_to_str_bytes_common` (the one from `libcore/fmt/float.rs`, not the function of the same name in `libstd/num/strconv.rs`). It had support for different bases, and for negative numbers, but the function is internal to core and the couple places that call it (all in `libcore/fmt/mod.rs`) never use those features: They pass in `num.abs()` and base 10.
The user could easily be another computer system or another programmer, which is frequently the case for, say, the @SimonSapin Both Neither Further comments on other languages:
Basically, I think it should be the programmer's responsibility to convert negative zero to positive, if that's what's appropriate for the given context. |
@SimonSapin Could you answer the question I posed above? i.e. Why does CSS serialization care about negative zero, but is OK with truncating small floating-point values (e.g. |
It’s a combination of multiple things. In CSS you can write things like Additionally, invariants like Taken together, the serialization tests of rust-cssparser fail if the sign of negative zero is not accounted for. This is a very contrived situation, and arguably this specific issue could be worked around in other ways. But I maintain that |
(if I had my druthers, |
Test case:
println!("{}", -0_f64)
, expected result-0
, actual result:0
.The text was updated successfully, but these errors were encountered: