Skip to content

seq: fix default float precision (%f); fix 0 scientific printing #7384

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

Merged
merged 4 commits into from
Mar 14, 2025
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
26 changes: 12 additions & 14 deletions src/uucore/src/lib/features/format/num_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,7 @@ impl Formatter for Float {

let precision = match precision {
Some(CanAsterisk::Fixed(x)) => x,
None => {
if matches!(variant, FloatVariant::Shortest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm good catch. I was wondering how this came to be and it seems to be this: 4c5326f

Looks like that commit only looked at %g and check the other formats.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... Also... Default precision for %a is not 6, but... the hexadecimal printing function right now does not support any sort of precision setting (e.g. %.6a still prints all digits...).

I'm... slowly trying to untangle this, maybe better to fix #7364 first. Still filed #7429 to remember.

6
} else {
0
}
}
None => 6, // Default float precision (C standard)
Some(CanAsterisk::Asterisk) => return Err(FormatError::WrongSpecType),
};

Expand Down Expand Up @@ -350,11 +344,16 @@ fn format_float_scientific(
case: Case,
force_decimal: ForceDecimal,
) -> String {
let exp_char = match case {
Case::Lowercase => 'e',
Case::Uppercase => 'E',
};

if f == 0.0 {
return if force_decimal == ForceDecimal::Yes && precision == 0 {
"0.e+00".into()
format!("0.{exp_char}+00")
} else {
format!("{:.*}e+00", precision, 0.0)
format!("{:.*}{exp_char}+00", precision, 0.0)
};
}

Expand All @@ -375,11 +374,6 @@ fn format_float_scientific(
""
};

let exp_char = match case {
Case::Lowercase => 'e',
Case::Uppercase => 'E',
};

format!("{normalized:.precision$}{additional_dot}{exp_char}{exponent:+03}")
}

Expand Down Expand Up @@ -582,6 +576,10 @@ mod test {
assert_eq!(f(12.345_678_9), "1.234568e+01");
assert_eq!(f(1_000_000.0), "1.000000e+06");
assert_eq!(f(99_999_999.0), "1.000000e+08");

let f = |x| format_float_scientific(x, 6, Case::Uppercase, ForceDecimal::No);
assert_eq!(f(0.0), "0.000000E+00");
assert_eq!(f(123_456.789), "1.234568E+05");
}

#[test]
Expand Down
26 changes: 24 additions & 2 deletions tests/by-util/test_seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,30 @@ fn test_format_option() {
}

#[test]
#[ignore = "Need issue #2660 to be fixed"]
fn test_format_option_default_precision() {
new_ucmd!()
.args(&["-f", "%f", "0", "0.7", "2"])
.succeeds()
.stdout_only("0.000000\n0.700000\n1.400000\n");
}

#[test]
fn test_format_option_default_precision_short() {
new_ucmd!()
.args(&["-f", "%g", "0", "0.987654321", "2"])
.succeeds()
.stdout_only("0\n0.987654\n1.97531\n");
}

#[test]
fn test_format_option_default_precision_scientific() {
new_ucmd!()
.args(&["-f", "%E", "0", "0.7", "2"])
.succeeds()
.stdout_only("0.000000E+00\n7.000000E-01\n1.400000E+00\n");
}

#[test]
fn test_auto_precision() {
new_ucmd!()
.args(&["1", "0x1p-1", "2"])
Expand All @@ -718,7 +741,6 @@ fn test_auto_precision() {
}

#[test]
#[ignore = "Need issue #3318 to be fixed"]
fn test_undefined() {
new_ucmd!()
.args(&["1e-9223372036854775808"])
Expand Down
Loading