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

Wrong formatting of negative zero when sign is requested #24623

Closed
nagisa opened this issue Apr 20, 2015 · 3 comments · Fixed by #78618
Closed

Wrong formatting of negative zero when sign is requested #24623

nagisa opened this issue Apr 20, 2015 · 3 comments · Fixed by #78618
Labels
A-floating-point Area: Floating point numbers and arithmetic A-fmt Area: `std::fmt` C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@nagisa
Copy link
Member

nagisa commented Apr 20, 2015

use std::mem;
fn main() { 
    let neg_zero: f32 = unsafe { mem::transmute(0x8000_0000u32) };
    assert_eq!(format!("{:+}", neg_zero), "-0");
}

Fails with “assertion failed: (left == right) (left: "+0", right: "-0")”

playpen

@hanna-kruppe
Copy link
Contributor

Note that {:?+} produces the desired result. So this is consistent with the behavior agreed upon in #20596: Display treats negative zero as positive, and Debug differentiates them. In the light of that, it's not clear to me why the + format code should make a difference. It would be more confusing if {} and {:+} disagreed about the sign (since absence of a sign indicates positive).

IMHO this is "wrong" if and only if RFC PR 1074 is accepted. Whether Display should distinguish negative zero should be duked out there.

Also, by the way: You can produce a negative zero with -0.0, no need for the transmute.

@arielb1 arielb1 added the A-libs label Jun 29, 2015
@bstrie
Copy link
Contributor

bstrie commented Oct 8, 2016

Triage: still relevant.

@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-libs labels Mar 24, 2017
@Mark-Simulacrum Mark-Simulacrum added C-bug Category: This is a bug. and removed I-wrong labels Jul 22, 2017
@workingjubilee
Copy link
Member

@rustbot modify labels: +A-floating-point, +A-fmt

@rustbot rustbot added A-floating-point Area: Floating point numbers and arithmetic A-fmt Area: `std::fmt` labels Sep 21, 2020
@bors bors closed this as completed in aef1140 Mar 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-floating-point Area: Floating point numbers and arithmetic A-fmt Area: `std::fmt` C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants