Skip to content

Debug-print negative not-a-number as "-NaN" #54235

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/libcore/num/flt2dec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,13 @@ pub enum Sign {
/// It can be either `b""`, `b"+"` or `b"-"`.
fn determine_sign(sign: Sign, decoded: &FullDecoded, negative: bool) -> &'static [u8] {
match (*decoded, sign) {
(_, Sign::MinusRaw) => if negative { b"-" } else { b"" },
(_, Sign::MinusPlusRaw) => if negative { b"-" } else { b"+" },
(FullDecoded::Nan, _) => b"",
(FullDecoded::Zero, Sign::Minus) => b"",
(FullDecoded::Zero, Sign::MinusRaw) => if negative { b"-" } else { b"" },
(FullDecoded::Zero, Sign::MinusPlus) => b"+",
(FullDecoded::Zero, Sign::MinusPlusRaw) => if negative { b"-" } else { b"+" },
(_, Sign::Minus) | (_, Sign::MinusRaw) => if negative { b"-" } else { b"" },
(_, Sign::MinusPlus) | (_, Sign::MinusPlusRaw) => if negative { b"-" } else { b"+" },
(_, Sign::Minus) => if negative { b"-" } else { b"" },
(_, Sign::MinusPlus) => if negative { b"-" } else { b"+" },
}
}

Expand Down
28 changes: 24 additions & 4 deletions src/libcore/tests/num/flt2dec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,18 +501,23 @@ pub fn to_shortest_str_test<F>(mut f_: F)
assert_eq!(to_string(f, -0.0, MinusPlus, 8, true), "+0.00000000");
assert_eq!(to_string(f, -0.0, MinusPlusRaw, 8, true), "-0.00000000");

let neg_nan = -(0.0/0.0);
assert_eq!(to_string(f, 1.0/0.0, Minus, 0, false), "inf");
assert_eq!(to_string(f, 1.0/0.0, MinusRaw, 0, true), "inf");
assert_eq!(to_string(f, 1.0/0.0, MinusPlus, 0, false), "+inf");
assert_eq!(to_string(f, 1.0/0.0, MinusPlusRaw, 0, true), "+inf");
assert_eq!(to_string(f, 0.0/0.0, Minus, 0, false), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusRaw, 1, true), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusPlus, 8, false), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusPlusRaw, 64, true), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusPlusRaw, 64, true), "+NaN");
assert_eq!(to_string(f, -1.0/0.0, Minus, 0, false), "-inf");
assert_eq!(to_string(f, -1.0/0.0, MinusRaw, 1, true), "-inf");
assert_eq!(to_string(f, -1.0/0.0, MinusPlus, 8, false), "-inf");
assert_eq!(to_string(f, -1.0/0.0, MinusPlusRaw, 64, true), "-inf");
assert_eq!(to_string(f, neg_nan, Minus, 0, false), "NaN");
assert_eq!(to_string(f, neg_nan, MinusRaw, 1, true), "-NaN");
assert_eq!(to_string(f, neg_nan, MinusPlus, 8, false), "NaN");
assert_eq!(to_string(f, neg_nan, MinusPlusRaw, 64, true), "-NaN");

assert_eq!(to_string(f, 3.14, Minus, 0, false), "3.14");
assert_eq!(to_string(f, 3.14, MinusRaw, 0, false), "3.14");
Expand Down Expand Up @@ -594,18 +599,23 @@ pub fn to_shortest_exp_str_test<F>(mut f_: F)
assert_eq!(to_string(f, -0.0, MinusPlus, (-9, -5), true), "+0E0");
assert_eq!(to_string(f, -0.0, MinusPlusRaw, ( 5, 9), false), "-0e0");

let neg_nan = -(0.0/0.0);
assert_eq!(to_string(f, 1.0/0.0, Minus, (-4, 16), false), "inf");
assert_eq!(to_string(f, 1.0/0.0, MinusRaw, (-4, 16), true), "inf");
assert_eq!(to_string(f, 1.0/0.0, MinusPlus, (-4, 16), false), "+inf");
assert_eq!(to_string(f, 1.0/0.0, MinusPlusRaw, (-4, 16), true), "+inf");
assert_eq!(to_string(f, 0.0/0.0, Minus, ( 0, 0), false), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusRaw, ( 0, 0), true), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusPlus, (-9, -5), false), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusPlusRaw, ( 5, 9), true), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusPlusRaw, ( 5, 9), true), "+NaN");
assert_eq!(to_string(f, -1.0/0.0, Minus, ( 0, 0), false), "-inf");
assert_eq!(to_string(f, -1.0/0.0, MinusRaw, ( 0, 0), true), "-inf");
assert_eq!(to_string(f, -1.0/0.0, MinusPlus, (-9, -5), false), "-inf");
assert_eq!(to_string(f, -1.0/0.0, MinusPlusRaw, ( 5, 9), true), "-inf");
assert_eq!(to_string(f, neg_nan, Minus, ( 0, 0), false), "NaN");
assert_eq!(to_string(f, neg_nan, MinusRaw, ( 0, 0), true), "-NaN");
assert_eq!(to_string(f, neg_nan, MinusPlus, (-9, -5), false), "NaN");
assert_eq!(to_string(f, neg_nan, MinusPlusRaw, ( 5, 9), true), "-NaN");

assert_eq!(to_string(f, 3.14, Minus, (-4, 16), false), "3.14");
assert_eq!(to_string(f, 3.14, MinusRaw, (-4, 16), false), "3.14");
Expand Down Expand Up @@ -707,18 +717,23 @@ pub fn to_exact_exp_str_test<F>(mut f_: F)
assert_eq!(to_string(f, -0.0, MinusPlus, 8, true), "+0.0000000E0");
assert_eq!(to_string(f, -0.0, MinusPlusRaw, 8, false), "-0.0000000e0");

let neg_nan = -(0.0/0.0);
assert_eq!(to_string(f, 1.0/0.0, Minus, 1, false), "inf");
assert_eq!(to_string(f, 1.0/0.0, MinusRaw, 1, true), "inf");
assert_eq!(to_string(f, 1.0/0.0, MinusPlus, 1, false), "+inf");
assert_eq!(to_string(f, 1.0/0.0, MinusPlusRaw, 1, true), "+inf");
assert_eq!(to_string(f, 0.0/0.0, Minus, 8, false), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusRaw, 8, true), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusPlus, 8, false), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusPlusRaw, 8, true), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusPlusRaw, 8, true), "+NaN");
assert_eq!(to_string(f, -1.0/0.0, Minus, 64, false), "-inf");
assert_eq!(to_string(f, -1.0/0.0, MinusRaw, 64, true), "-inf");
assert_eq!(to_string(f, -1.0/0.0, MinusPlus, 64, false), "-inf");
assert_eq!(to_string(f, -1.0/0.0, MinusPlusRaw, 64, true), "-inf");
assert_eq!(to_string(f, neg_nan, Minus, 8, false), "NaN");
assert_eq!(to_string(f, neg_nan, MinusRaw, 8, true), "-NaN");
assert_eq!(to_string(f, neg_nan, MinusPlus, 8, false), "NaN");
assert_eq!(to_string(f, neg_nan, MinusPlusRaw, 8, true), "-NaN");

assert_eq!(to_string(f, 3.14, Minus, 1, true), "3E0");
assert_eq!(to_string(f, 3.14, MinusRaw, 1, false), "3e0");
Expand Down Expand Up @@ -927,18 +942,23 @@ pub fn to_exact_fixed_str_test<F>(mut f_: F)
assert_eq!(to_string(f, -0.0, MinusPlus, 8, true), "+0.00000000");
assert_eq!(to_string(f, -0.0, MinusPlusRaw, 8, true), "-0.00000000");

let neg_nan = -(0.0/0.0);
assert_eq!(to_string(f, 1.0/0.0, Minus, 0, false), "inf");
assert_eq!(to_string(f, 1.0/0.0, MinusRaw, 1, true), "inf");
assert_eq!(to_string(f, 1.0/0.0, MinusPlus, 8, false), "+inf");
assert_eq!(to_string(f, 1.0/0.0, MinusPlusRaw, 64, true), "+inf");
assert_eq!(to_string(f, 0.0/0.0, Minus, 0, false), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusRaw, 1, true), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusPlus, 8, false), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusPlusRaw, 64, true), "NaN");
assert_eq!(to_string(f, 0.0/0.0, MinusPlusRaw, 64, true), "+NaN");
assert_eq!(to_string(f, -1.0/0.0, Minus, 0, false), "-inf");
assert_eq!(to_string(f, -1.0/0.0, MinusRaw, 1, true), "-inf");
assert_eq!(to_string(f, -1.0/0.0, MinusPlus, 8, false), "-inf");
assert_eq!(to_string(f, -1.0/0.0, MinusPlusRaw, 64, true), "-inf");
assert_eq!(to_string(f, neg_nan, Minus, 0, false), "NaN");
assert_eq!(to_string(f, neg_nan, MinusRaw, 1, true), "-NaN");
assert_eq!(to_string(f, neg_nan, MinusPlus, 8, false), "NaN");
assert_eq!(to_string(f, neg_nan, MinusPlusRaw, 64, true), "-NaN");

assert_eq!(to_string(f, 3.14, Minus, 0, false), "3");
assert_eq!(to_string(f, 3.14, MinusRaw, 0, false), "3");
Expand Down