Skip to content

Commit 74db93e

Browse files
Preserve signed zero on roundtrip
This commit removes the previous mechanism of differentiating between "Debug" and "Display" formattings for the sign of -0 so as to comply with the IEEE 754 standard's requirements on external character sequences preserving various attributes of a floating point representation. In addition, numerous tests are fixed.
1 parent 588cc64 commit 74db93e

File tree

5 files changed

+102
-200
lines changed

5 files changed

+102
-200
lines changed

library/alloc/tests/fmt.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ fn test_format_macro_interface() {
154154
t!(format!("{:+10.3e}", -1.2345e6f64), " -1.234e6");
155155

156156
// Float edge cases
157-
t!(format!("{}", -0.0), "0");
158-
t!(format!("{:?}", -0.0), "-0.0");
157+
t!(format!("{}", -0.0), "-0");
159158
t!(format!("{:?}", 0.0), "0.0");
160159

161160
// sign aware zero padding

library/core/src/fmt/float.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,14 @@ where
5454
}
5555

5656
// Common code of floating point Debug and Display.
57-
fn float_to_decimal_common<T>(
58-
fmt: &mut Formatter<'_>,
59-
num: &T,
60-
negative_zero: bool,
61-
min_precision: usize,
62-
) -> Result
57+
fn float_to_decimal_common<T>(fmt: &mut Formatter<'_>, num: &T, min_precision: usize) -> Result
6358
where
6459
T: flt2dec::DecodableFloat,
6560
{
6661
let force_sign = fmt.sign_plus();
67-
let sign = match (force_sign, negative_zero) {
68-
(false, false) => flt2dec::Sign::Minus,
69-
(false, true) => flt2dec::Sign::MinusRaw,
70-
(true, false) => flt2dec::Sign::MinusPlus,
71-
(true, true) => flt2dec::Sign::MinusPlusRaw,
62+
let sign = match force_sign {
63+
false => flt2dec::Sign::Minus,
64+
true => flt2dec::Sign::MinusPlus,
7265
};
7366

7467
if let Some(precision) = fmt.precision {
@@ -156,14 +149,14 @@ macro_rules! floating {
156149
#[stable(feature = "rust1", since = "1.0.0")]
157150
impl Debug for $ty {
158151
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
159-
float_to_decimal_common(fmt, self, true, 1)
152+
float_to_decimal_common(fmt, self, 1)
160153
}
161154
}
162155

163156
#[stable(feature = "rust1", since = "1.0.0")]
164157
impl Display for $ty {
165158
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
166-
float_to_decimal_common(fmt, self, false, 0)
159+
float_to_decimal_common(fmt, self, 0)
167160
}
168161
}
169162

library/core/src/num/flt2dec/mod.rs

+6-26
Original file line numberDiff line numberDiff line change
@@ -399,45 +399,25 @@ fn digits_to_exp_str<'a>(
399399
/// Sign formatting options.
400400
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
401401
pub enum Sign {
402-
/// Prints `-` only for the negative non-zero values.
403-
Minus, // -inf -1 0 0 1 inf nan
404-
/// Prints `-` only for any negative values (including the negative zero).
405-
MinusRaw, // -inf -1 -0 0 1 inf nan
406-
/// Prints `-` for the negative non-zero values, or `+` otherwise.
407-
MinusPlus, // -inf -1 +0 +0 +1 +inf nan
408-
/// Prints `-` for any negative values (including the negative zero), or `+` otherwise.
409-
MinusPlusRaw, // -inf -1 -0 +0 +1 +inf nan
402+
/// Prints `-` for any negative value.
403+
Minus, // -inf -1 -0 0 1 inf nan
404+
/// Prints `-` for any negative value, or `+` otherwise.
405+
MinusPlus, // -inf -1 -0 +0 +1 +inf nan
410406
}
411407

412408
/// Returns the static byte string corresponding to the sign to be formatted.
413409
/// It can be either `""`, `"+"` or `"-"`.
414410
fn determine_sign(sign: Sign, decoded: &FullDecoded, negative: bool) -> &'static str {
415411
match (*decoded, sign) {
416412
(FullDecoded::Nan, _) => "",
417-
(FullDecoded::Zero, Sign::Minus) => "",
418-
(FullDecoded::Zero, Sign::MinusRaw) => {
413+
(_, Sign::Minus) => {
419414
if negative {
420415
"-"
421416
} else {
422417
""
423418
}
424419
}
425-
(FullDecoded::Zero, Sign::MinusPlus) => "+",
426-
(FullDecoded::Zero, Sign::MinusPlusRaw) => {
427-
if negative {
428-
"-"
429-
} else {
430-
"+"
431-
}
432-
}
433-
(_, Sign::Minus | Sign::MinusRaw) => {
434-
if negative {
435-
"-"
436-
} else {
437-
""
438-
}
439-
}
440-
(_, Sign::MinusPlus | Sign::MinusPlusRaw) => {
420+
(_, Sign::MinusPlus) => {
441421
if negative {
442422
"-"
443423
} else {

0 commit comments

Comments
 (0)