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

Add IEEE 754 compliant fmt/parse of -0, infinity, NaN #78618

Merged
merged 5 commits into from
Mar 27, 2021
Merged
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
5 changes: 2 additions & 3 deletions library/alloc/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,8 @@
//!
//! * `+` - This is intended for numeric types and indicates that the sign
//! should always be printed. Positive signs are never printed by
//! default, and the negative sign is only printed by default for the
//! `Signed` trait. This flag indicates that the correct sign (`+` or `-`)
//! should always be printed.
//! default, and the negative sign is only printed by default for signed values.
//! This flag indicates that the correct sign (`+` or `-`) should always be printed.
//! * `-` - Currently not used
//! * `#` - This flag indicates that the "alternate" form of printing should
//! be used. The alternate forms are:
Expand Down
3 changes: 1 addition & 2 deletions library/alloc/tests/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ fn test_format_macro_interface() {
t!(format!("{:+10.3e}", -1.2345e6f64), " -1.234e6");

// Float edge cases
t!(format!("{}", -0.0), "0");
t!(format!("{:?}", -0.0), "-0.0");
workingjubilee marked this conversation as resolved.
Show resolved Hide resolved
t!(format!("{}", -0.0), "-0");
t!(format!("{:?}", 0.0), "0.0");

// sign aware zero padding
Expand Down
19 changes: 6 additions & 13 deletions library/core/src/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,14 @@ where
}

// Common code of floating point Debug and Display.
fn float_to_decimal_common<T>(
fmt: &mut Formatter<'_>,
num: &T,
negative_zero: bool,
min_precision: usize,
) -> Result
fn float_to_decimal_common<T>(fmt: &mut Formatter<'_>, num: &T, min_precision: usize) -> Result
where
T: flt2dec::DecodableFloat,
{
let force_sign = fmt.sign_plus();
let sign = match (force_sign, negative_zero) {
(false, false) => flt2dec::Sign::Minus,
(false, true) => flt2dec::Sign::MinusRaw,
(true, false) => flt2dec::Sign::MinusPlus,
(true, true) => flt2dec::Sign::MinusPlusRaw,
let sign = match force_sign {
false => flt2dec::Sign::Minus,
true => flt2dec::Sign::MinusPlus,
};

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

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

Expand Down
12 changes: 7 additions & 5 deletions library/core/src/num/dec2flt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,15 @@ fn dec2flt<T: RawFloat>(s: &str) -> Result<T, ParseFloatError> {
ParseResult::Valid(decimal) => convert(decimal)?,
ParseResult::ShortcutToInf => T::INFINITY,
ParseResult::ShortcutToZero => T::ZERO,
ParseResult::Invalid => match s {
"inf" => T::INFINITY,
"NaN" => T::NAN,
_ => {
ParseResult::Invalid => {
if s.eq_ignore_ascii_case("nan") {
T::NAN
} else if s.eq_ignore_ascii_case("inf") || s.eq_ignore_ascii_case("infinity") {
T::INFINITY
} else {
return Err(pfe_invalid());
}
},
}
};

match sign {
Expand Down
32 changes: 6 additions & 26 deletions library/core/src/num/flt2dec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,45 +399,25 @@ fn digits_to_exp_str<'a>(
/// Sign formatting options.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Sign {
/// Prints `-` only for the negative non-zero values.
Minus, // -inf -1 0 0 1 inf nan
/// Prints `-` only for any negative values (including the negative zero).
MinusRaw, // -inf -1 -0 0 1 inf nan
/// Prints `-` for the negative non-zero values, or `+` otherwise.
MinusPlus, // -inf -1 +0 +0 +1 +inf nan
/// Prints `-` for any negative values (including the negative zero), or `+` otherwise.
MinusPlusRaw, // -inf -1 -0 +0 +1 +inf nan
/// Prints `-` for any negative value.
Minus, // -inf -1 -0 0 1 inf nan
/// Prints `-` for any negative value, or `+` otherwise.
MinusPlus, // -inf -1 -0 +0 +1 +inf nan
}

/// Returns the static byte string corresponding to the sign to be formatted.
/// It can be either `""`, `"+"` or `"-"`.
fn determine_sign(sign: Sign, decoded: &FullDecoded, negative: bool) -> &'static str {
match (*decoded, sign) {
(FullDecoded::Nan, _) => "",
(FullDecoded::Zero, Sign::Minus) => "",
(FullDecoded::Zero, Sign::MinusRaw) => {
(_, Sign::Minus) => {
if negative {
"-"
} else {
""
}
}
(FullDecoded::Zero, Sign::MinusPlus) => "+",
(FullDecoded::Zero, Sign::MinusPlusRaw) => {
if negative {
"-"
} else {
"+"
}
}
(_, Sign::Minus | Sign::MinusRaw) => {
if negative {
"-"
} else {
""
}
}
(_, Sign::MinusPlus | Sign::MinusPlusRaw) => {
(_, Sign::MinusPlus) => {
if negative {
"-"
} else {
Expand Down
Loading