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

scientific notation incorrect decimal places #115737

Closed
ljdoig opened this issue Sep 10, 2023 · 2 comments · Fixed by #116301
Closed

scientific notation incorrect decimal places #115737

ljdoig opened this issue Sep 10, 2023 · 2 comments · Fixed by #116301
Assignees
Labels
A-fmt Area: `std::fmt` C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@ljdoig
Copy link

ljdoig commented Sep 10, 2023

I tried this code:

    assert_eq!("1.0e3", format!("{:.1e}", 1001));
    assert_eq!("1.0e3", format!("{:.1e}", 1000));
    assert_eq!("1.0e3", format!("{:.1e}", 999));

I expected this all to be fine.

Instead, the final assertion fails, and the formatting is done like this in the final case:

    assert_eq!("1.00e3", format!("{:.1e}", 999));

Not sure why 2 decimal places are returned, looks like a bug.
This bug also exists in the beta or nightly versions, running in Rust Playground.

Discussion: https://users.rust-lang.org/t/scientific-notation-decimal-places/99682

Edit: after looking around the compiler code for the first time I think the problem is somewhere in either format_exact or format_exact_opt.

@ljdoig ljdoig added the C-bug Category: This is a bug. label Sep 10, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 10, 2023
@saethlin saethlin added T-libs Relevant to the library team, which will review and decide on the PR/issue. A-fmt Area: `std::fmt` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 11, 2023
@steffahn
Copy link
Member

steffahn commented Sep 11, 2023

Edit: after looking around the compiler code for the first time I think the problem is somewhere in either format_exact or format_exact_opt.

No. The relevant code is actually the one for formatting integers in scientific notation, not floats. I. e. the relevant code is the code first introduced in #66721

As I mentioned in the linked URLO thread, I believe perhaps integer formatting in scientific notation should be fully identically behaving to the (seemingly "correct", and also historically older) formatting implementation for floats. This is a potential test we might want passing:

let [mut s1, mut s2]: [String; 2] = Default::default();

for x in -2000_i64..=2000 {
    let xf = x as f64;

    macro_rules! test {
        ($fmt:literal, $assert_fmt:literal) => {
            s1.clear();
            s2.clear();
            write!(&mut s1, $fmt, x).unwrap();
            write!(&mut s2, $fmt, xf).unwrap();
            assert_eq!(
                &s1,
                &s2,
                "exponential formatting of integers vs. integral floats\nformat!(\"{}\", {})",
                format_args!($assert_fmt),
                x
            );
        };
    }

    test!("{:e}", "{{:e}}");
    test!("{:E}", "{{:E}}");

    for n in 0..5 {
        test!("{:.n$e}", "{{:.{n}e}}");
        test!("{:.n$E}", "{{:.{n}E}}");
    }
}

but as of now, this kind of test fails in a lot of cases:

format!("{:.0e}",  -999) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -998) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -997) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -996) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -995) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -994) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -993) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -992) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -991) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -990) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -989) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -988) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -987) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -986) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -985) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -984) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -983) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -982) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -981) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -980) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -979) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -978) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -977) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -976) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -975) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -974) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -973) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -972) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -971) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -970) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -969) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -968) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -967) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -966) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -965) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -964) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -963) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -962) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -961) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -960) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -959) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -958) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -957) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -956) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -955) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -954) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -953) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -952) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -951) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -950) --> i64: "-1.0e3"  f64: "-1e3"
format!("{:.0e}",  -850) --> i64: "-9e2"    f64: "-8e2"
format!("{:.0e}",  -650) --> i64: "-7e2"    f64: "-6e2"
format!("{:.0e}",  -450) --> i64: "-5e2"    f64: "-4e2"
format!("{:.0e}",  -250) --> i64: "-3e2"    f64: "-2e2"
format!("{:.0e}",   -99) --> i64: "-1.0e2"  f64: "-1e2"
format!("{:.0e}",   -98) --> i64: "-1.0e2"  f64: "-1e2"
format!("{:.0e}",   -97) --> i64: "-1.0e2"  f64: "-1e2"
format!("{:.0e}",   -96) --> i64: "-1.0e2"  f64: "-1e2"
format!("{:.0e}",   -95) --> i64: "-1.0e2"  f64: "-1e2"
format!("{:.0e}",   -85) --> i64: "-9e1"    f64: "-8e1"
format!("{:.0e}",   -65) --> i64: "-7e1"    f64: "-6e1"
format!("{:.0e}",   -45) --> i64: "-5e1"    f64: "-4e1"
format!("{:.0e}",   -25) --> i64: "-3e1"    f64: "-2e1"
format!("{:.0e}",    25) --> i64: "3e1"     f64: "2e1"
format!("{:.0e}",    45) --> i64: "5e1"     f64: "4e1"
format!("{:.0e}",    65) --> i64: "7e1"     f64: "6e1"
format!("{:.0e}",    85) --> i64: "9e1"     f64: "8e1"
format!("{:.0e}",    95) --> i64: "1.0e2"   f64: "1e2"
format!("{:.0e}",    96) --> i64: "1.0e2"   f64: "1e2"
format!("{:.0e}",    97) --> i64: "1.0e2"   f64: "1e2"
format!("{:.0e}",    98) --> i64: "1.0e2"   f64: "1e2"
format!("{:.0e}",    99) --> i64: "1.0e2"   f64: "1e2"
format!("{:.0e}",   250) --> i64: "3e2"     f64: "2e2"
format!("{:.0e}",   450) --> i64: "5e2"     f64: "4e2"
format!("{:.0e}",   650) --> i64: "7e2"     f64: "6e2"
format!("{:.0e}",   850) --> i64: "9e2"     f64: "8e2"
format!("{:.0e}",   950) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   951) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   952) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   953) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   954) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   955) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   956) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   957) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   958) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   959) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   960) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   961) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   962) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   963) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   964) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   965) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   966) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   967) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   968) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   969) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   970) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   971) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   972) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   973) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   974) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   975) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   976) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   977) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   978) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   979) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   980) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   981) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   982) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   983) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   984) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   985) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   986) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   987) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   988) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   989) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   990) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   991) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   992) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   993) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   994) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   995) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   996) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   997) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   998) --> i64: "1.0e3"   f64: "1e3"
format!("{:.0e}",   999) --> i64: "1.0e3"   f64: "1e3"
format!("{:.1e}", -1850) --> i64: "-1.9e3"  f64: "-1.8e3"
format!("{:.1e}", -1650) --> i64: "-1.7e3"  f64: "-1.6e3"
format!("{:.1e}", -1450) --> i64: "-1.5e3"  f64: "-1.4e3"
format!("{:.1e}", -1250) --> i64: "-1.3e3"  f64: "-1.2e3"
format!("{:.1e}", -1050) --> i64: "-1.1e3"  f64: "-1.0e3"
format!("{:.1e}",  -999) --> i64: "-1.00e3" f64: "-1.0e3"
format!("{:.1e}",  -998) --> i64: "-1.00e3" f64: "-1.0e3"
format!("{:.1e}",  -997) --> i64: "-1.00e3" f64: "-1.0e3"
format!("{:.1e}",  -996) --> i64: "-1.00e3" f64: "-1.0e3"
format!("{:.1e}",  -995) --> i64: "-1.00e3" f64: "-1.0e3"
format!("{:.1e}",  -985) --> i64: "-9.9e2"  f64: "-9.8e2"
format!("{:.1e}",  -965) --> i64: "-9.7e2"  f64: "-9.6e2"
format!("{:.1e}",  -945) --> i64: "-9.5e2"  f64: "-9.4e2"
format!("{:.1e}",  -925) --> i64: "-9.3e2"  f64: "-9.2e2"
format!("{:.1e}",  -905) --> i64: "-9.1e2"  f64: "-9.0e2"
format!("{:.1e}",  -885) --> i64: "-8.9e2"  f64: "-8.8e2"
format!("{:.1e}",  -865) --> i64: "-8.7e2"  f64: "-8.6e2"
format!("{:.1e}",  -845) --> i64: "-8.5e2"  f64: "-8.4e2"
format!("{:.1e}",  -825) --> i64: "-8.3e2"  f64: "-8.2e2"
format!("{:.1e}",  -805) --> i64: "-8.1e2"  f64: "-8.0e2"
format!("{:.1e}",  -785) --> i64: "-7.9e2"  f64: "-7.8e2"
format!("{:.1e}",  -765) --> i64: "-7.7e2"  f64: "-7.6e2"
format!("{:.1e}",  -745) --> i64: "-7.5e2"  f64: "-7.4e2"
format!("{:.1e}",  -725) --> i64: "-7.3e2"  f64: "-7.2e2"
format!("{:.1e}",  -705) --> i64: "-7.1e2"  f64: "-7.0e2"
format!("{:.1e}",  -685) --> i64: "-6.9e2"  f64: "-6.8e2"
format!("{:.1e}",  -665) --> i64: "-6.7e2"  f64: "-6.6e2"
format!("{:.1e}",  -645) --> i64: "-6.5e2"  f64: "-6.4e2"
format!("{:.1e}",  -625) --> i64: "-6.3e2"  f64: "-6.2e2"
format!("{:.1e}",  -605) --> i64: "-6.1e2"  f64: "-6.0e2"
format!("{:.1e}",  -585) --> i64: "-5.9e2"  f64: "-5.8e2"
format!("{:.1e}",  -565) --> i64: "-5.7e2"  f64: "-5.6e2"
format!("{:.1e}",  -545) --> i64: "-5.5e2"  f64: "-5.4e2"
format!("{:.1e}",  -525) --> i64: "-5.3e2"  f64: "-5.2e2"
format!("{:.1e}",  -505) --> i64: "-5.1e2"  f64: "-5.0e2"
format!("{:.1e}",  -485) --> i64: "-4.9e2"  f64: "-4.8e2"
format!("{:.1e}",  -465) --> i64: "-4.7e2"  f64: "-4.6e2"
format!("{:.1e}",  -445) --> i64: "-4.5e2"  f64: "-4.4e2"
format!("{:.1e}",  -425) --> i64: "-4.3e2"  f64: "-4.2e2"
format!("{:.1e}",  -405) --> i64: "-4.1e2"  f64: "-4.0e2"
format!("{:.1e}",  -385) --> i64: "-3.9e2"  f64: "-3.8e2"
format!("{:.1e}",  -365) --> i64: "-3.7e2"  f64: "-3.6e2"
format!("{:.1e}",  -345) --> i64: "-3.5e2"  f64: "-3.4e2"
format!("{:.1e}",  -325) --> i64: "-3.3e2"  f64: "-3.2e2"
format!("{:.1e}",  -305) --> i64: "-3.1e2"  f64: "-3.0e2"
format!("{:.1e}",  -285) --> i64: "-2.9e2"  f64: "-2.8e2"
format!("{:.1e}",  -265) --> i64: "-2.7e2"  f64: "-2.6e2"
format!("{:.1e}",  -245) --> i64: "-2.5e2"  f64: "-2.4e2"
format!("{:.1e}",  -225) --> i64: "-2.3e2"  f64: "-2.2e2"
format!("{:.1e}",  -205) --> i64: "-2.1e2"  f64: "-2.0e2"
format!("{:.1e}",  -185) --> i64: "-1.9e2"  f64: "-1.8e2"
format!("{:.1e}",  -165) --> i64: "-1.7e2"  f64: "-1.6e2"
format!("{:.1e}",  -145) --> i64: "-1.5e2"  f64: "-1.4e2"
format!("{:.1e}",  -125) --> i64: "-1.3e2"  f64: "-1.2e2"
format!("{:.1e}",  -105) --> i64: "-1.1e2"  f64: "-1.0e2"
format!("{:.1e}",   105) --> i64: "1.1e2"   f64: "1.0e2"
format!("{:.1e}",   125) --> i64: "1.3e2"   f64: "1.2e2"
format!("{:.1e}",   145) --> i64: "1.5e2"   f64: "1.4e2"
format!("{:.1e}",   165) --> i64: "1.7e2"   f64: "1.6e2"
format!("{:.1e}",   185) --> i64: "1.9e2"   f64: "1.8e2"
format!("{:.1e}",   205) --> i64: "2.1e2"   f64: "2.0e2"
format!("{:.1e}",   225) --> i64: "2.3e2"   f64: "2.2e2"
format!("{:.1e}",   245) --> i64: "2.5e2"   f64: "2.4e2"
format!("{:.1e}",   265) --> i64: "2.7e2"   f64: "2.6e2"
format!("{:.1e}",   285) --> i64: "2.9e2"   f64: "2.8e2"
format!("{:.1e}",   305) --> i64: "3.1e2"   f64: "3.0e2"
format!("{:.1e}",   325) --> i64: "3.3e2"   f64: "3.2e2"
format!("{:.1e}",   345) --> i64: "3.5e2"   f64: "3.4e2"
format!("{:.1e}",   365) --> i64: "3.7e2"   f64: "3.6e2"
format!("{:.1e}",   385) --> i64: "3.9e2"   f64: "3.8e2"
format!("{:.1e}",   405) --> i64: "4.1e2"   f64: "4.0e2"
format!("{:.1e}",   425) --> i64: "4.3e2"   f64: "4.2e2"
format!("{:.1e}",   445) --> i64: "4.5e2"   f64: "4.4e2"
format!("{:.1e}",   465) --> i64: "4.7e2"   f64: "4.6e2"
format!("{:.1e}",   485) --> i64: "4.9e2"   f64: "4.8e2"
format!("{:.1e}",   505) --> i64: "5.1e2"   f64: "5.0e2"
format!("{:.1e}",   525) --> i64: "5.3e2"   f64: "5.2e2"
format!("{:.1e}",   545) --> i64: "5.5e2"   f64: "5.4e2"
format!("{:.1e}",   565) --> i64: "5.7e2"   f64: "5.6e2"
format!("{:.1e}",   585) --> i64: "5.9e2"   f64: "5.8e2"
format!("{:.1e}",   605) --> i64: "6.1e2"   f64: "6.0e2"
format!("{:.1e}",   625) --> i64: "6.3e2"   f64: "6.2e2"
format!("{:.1e}",   645) --> i64: "6.5e2"   f64: "6.4e2"
format!("{:.1e}",   665) --> i64: "6.7e2"   f64: "6.6e2"
format!("{:.1e}",   685) --> i64: "6.9e2"   f64: "6.8e2"
format!("{:.1e}",   705) --> i64: "7.1e2"   f64: "7.0e2"
format!("{:.1e}",   725) --> i64: "7.3e2"   f64: "7.2e2"
format!("{:.1e}",   745) --> i64: "7.5e2"   f64: "7.4e2"
format!("{:.1e}",   765) --> i64: "7.7e2"   f64: "7.6e2"
format!("{:.1e}",   785) --> i64: "7.9e2"   f64: "7.8e2"
format!("{:.1e}",   805) --> i64: "8.1e2"   f64: "8.0e2"
format!("{:.1e}",   825) --> i64: "8.3e2"   f64: "8.2e2"
format!("{:.1e}",   845) --> i64: "8.5e2"   f64: "8.4e2"
format!("{:.1e}",   865) --> i64: "8.7e2"   f64: "8.6e2"
format!("{:.1e}",   885) --> i64: "8.9e2"   f64: "8.8e2"
format!("{:.1e}",   905) --> i64: "9.1e2"   f64: "9.0e2"
format!("{:.1e}",   925) --> i64: "9.3e2"   f64: "9.2e2"
format!("{:.1e}",   945) --> i64: "9.5e2"   f64: "9.4e2"
format!("{:.1e}",   965) --> i64: "9.7e2"   f64: "9.6e2"
format!("{:.1e}",   985) --> i64: "9.9e2"   f64: "9.8e2"
format!("{:.1e}",   995) --> i64: "1.00e3"  f64: "1.0e3"
format!("{:.1e}",   996) --> i64: "1.00e3"  f64: "1.0e3"
format!("{:.1e}",   997) --> i64: "1.00e3"  f64: "1.0e3"
format!("{:.1e}",   998) --> i64: "1.00e3"  f64: "1.0e3"
format!("{:.1e}",   999) --> i64: "1.00e3"  f64: "1.0e3"
format!("{:.1e}",  1050) --> i64: "1.1e3"   f64: "1.0e3"
format!("{:.1e}",  1250) --> i64: "1.3e3"   f64: "1.2e3"
format!("{:.1e}",  1450) --> i64: "1.5e3"   f64: "1.4e3"
format!("{:.1e}",  1650) --> i64: "1.7e3"   f64: "1.6e3"
format!("{:.1e}",  1850) --> i64: "1.9e3"   f64: "1.8e3"
format!("{:.2e}", -1985) --> i64: "-1.99e3" f64: "-1.98e3"
format!("{:.2e}", -1965) --> i64: "-1.97e3" f64: "-1.96e3"
format!("{:.2e}", -1945) --> i64: "-1.95e3" f64: "-1.94e3"
format!("{:.2e}", -1925) --> i64: "-1.93e3" f64: "-1.92e3"
format!("{:.2e}", -1905) --> i64: "-1.91e3" f64: "-1.90e3"
format!("{:.2e}", -1885) --> i64: "-1.89e3" f64: "-1.88e3"
format!("{:.2e}", -1865) --> i64: "-1.87e3" f64: "-1.86e3"
format!("{:.2e}", -1845) --> i64: "-1.85e3" f64: "-1.84e3"
format!("{:.2e}", -1825) --> i64: "-1.83e3" f64: "-1.82e3"
format!("{:.2e}", -1805) --> i64: "-1.81e3" f64: "-1.80e3"
format!("{:.2e}", -1785) --> i64: "-1.79e3" f64: "-1.78e3"
format!("{:.2e}", -1765) --> i64: "-1.77e3" f64: "-1.76e3"
format!("{:.2e}", -1745) --> i64: "-1.75e3" f64: "-1.74e3"
format!("{:.2e}", -1725) --> i64: "-1.73e3" f64: "-1.72e3"
format!("{:.2e}", -1705) --> i64: "-1.71e3" f64: "-1.70e3"
format!("{:.2e}", -1685) --> i64: "-1.69e3" f64: "-1.68e3"
format!("{:.2e}", -1665) --> i64: "-1.67e3" f64: "-1.66e3"
format!("{:.2e}", -1645) --> i64: "-1.65e3" f64: "-1.64e3"
format!("{:.2e}", -1625) --> i64: "-1.63e3" f64: "-1.62e3"
format!("{:.2e}", -1605) --> i64: "-1.61e3" f64: "-1.60e3"
format!("{:.2e}", -1585) --> i64: "-1.59e3" f64: "-1.58e3"
format!("{:.2e}", -1565) --> i64: "-1.57e3" f64: "-1.56e3"
format!("{:.2e}", -1545) --> i64: "-1.55e3" f64: "-1.54e3"
format!("{:.2e}", -1525) --> i64: "-1.53e3" f64: "-1.52e3"
format!("{:.2e}", -1505) --> i64: "-1.51e3" f64: "-1.50e3"
format!("{:.2e}", -1485) --> i64: "-1.49e3" f64: "-1.48e3"
format!("{:.2e}", -1465) --> i64: "-1.47e3" f64: "-1.46e3"
format!("{:.2e}", -1445) --> i64: "-1.45e3" f64: "-1.44e3"
format!("{:.2e}", -1425) --> i64: "-1.43e3" f64: "-1.42e3"
format!("{:.2e}", -1405) --> i64: "-1.41e3" f64: "-1.40e3"
format!("{:.2e}", -1385) --> i64: "-1.39e3" f64: "-1.38e3"
format!("{:.2e}", -1365) --> i64: "-1.37e3" f64: "-1.36e3"
format!("{:.2e}", -1345) --> i64: "-1.35e3" f64: "-1.34e3"
format!("{:.2e}", -1325) --> i64: "-1.33e3" f64: "-1.32e3"
format!("{:.2e}", -1305) --> i64: "-1.31e3" f64: "-1.30e3"
format!("{:.2e}", -1285) --> i64: "-1.29e3" f64: "-1.28e3"
format!("{:.2e}", -1265) --> i64: "-1.27e3" f64: "-1.26e3"
format!("{:.2e}", -1245) --> i64: "-1.25e3" f64: "-1.24e3"
format!("{:.2e}", -1225) --> i64: "-1.23e3" f64: "-1.22e3"
format!("{:.2e}", -1205) --> i64: "-1.21e3" f64: "-1.20e3"
format!("{:.2e}", -1185) --> i64: "-1.19e3" f64: "-1.18e3"
format!("{:.2e}", -1165) --> i64: "-1.17e3" f64: "-1.16e3"
format!("{:.2e}", -1145) --> i64: "-1.15e3" f64: "-1.14e3"
format!("{:.2e}", -1125) --> i64: "-1.13e3" f64: "-1.12e3"
format!("{:.2e}", -1105) --> i64: "-1.11e3" f64: "-1.10e3"
format!("{:.2e}", -1085) --> i64: "-1.09e3" f64: "-1.08e3"
format!("{:.2e}", -1065) --> i64: "-1.07e3" f64: "-1.06e3"
format!("{:.2e}", -1045) --> i64: "-1.05e3" f64: "-1.04e3"
format!("{:.2e}", -1025) --> i64: "-1.03e3" f64: "-1.02e3"
format!("{:.2e}", -1005) --> i64: "-1.01e3" f64: "-1.00e3"
format!("{:.2e}",  1005) --> i64: "1.01e3"  f64: "1.00e3"
format!("{:.2e}",  1025) --> i64: "1.03e3"  f64: "1.02e3"
format!("{:.2e}",  1045) --> i64: "1.05e3"  f64: "1.04e3"
format!("{:.2e}",  1065) --> i64: "1.07e3"  f64: "1.06e3"
format!("{:.2e}",  1085) --> i64: "1.09e3"  f64: "1.08e3"
format!("{:.2e}",  1105) --> i64: "1.11e3"  f64: "1.10e3"
format!("{:.2e}",  1125) --> i64: "1.13e3"  f64: "1.12e3"
format!("{:.2e}",  1145) --> i64: "1.15e3"  f64: "1.14e3"
format!("{:.2e}",  1165) --> i64: "1.17e3"  f64: "1.16e3"
format!("{:.2e}",  1185) --> i64: "1.19e3"  f64: "1.18e3"
format!("{:.2e}",  1205) --> i64: "1.21e3"  f64: "1.20e3"
format!("{:.2e}",  1225) --> i64: "1.23e3"  f64: "1.22e3"
format!("{:.2e}",  1245) --> i64: "1.25e3"  f64: "1.24e3"
format!("{:.2e}",  1265) --> i64: "1.27e3"  f64: "1.26e3"
format!("{:.2e}",  1285) --> i64: "1.29e3"  f64: "1.28e3"
format!("{:.2e}",  1305) --> i64: "1.31e3"  f64: "1.30e3"
format!("{:.2e}",  1325) --> i64: "1.33e3"  f64: "1.32e3"
format!("{:.2e}",  1345) --> i64: "1.35e3"  f64: "1.34e3"
format!("{:.2e}",  1365) --> i64: "1.37e3"  f64: "1.36e3"
format!("{:.2e}",  1385) --> i64: "1.39e3"  f64: "1.38e3"
format!("{:.2e}",  1405) --> i64: "1.41e3"  f64: "1.40e3"
format!("{:.2e}",  1425) --> i64: "1.43e3"  f64: "1.42e3"
format!("{:.2e}",  1445) --> i64: "1.45e3"  f64: "1.44e3"
format!("{:.2e}",  1465) --> i64: "1.47e3"  f64: "1.46e3"
format!("{:.2e}",  1485) --> i64: "1.49e3"  f64: "1.48e3"
format!("{:.2e}",  1505) --> i64: "1.51e3"  f64: "1.50e3"
format!("{:.2e}",  1525) --> i64: "1.53e3"  f64: "1.52e3"
format!("{:.2e}",  1545) --> i64: "1.55e3"  f64: "1.54e3"
format!("{:.2e}",  1565) --> i64: "1.57e3"  f64: "1.56e3"
format!("{:.2e}",  1585) --> i64: "1.59e3"  f64: "1.58e3"
format!("{:.2e}",  1605) --> i64: "1.61e3"  f64: "1.60e3"
format!("{:.2e}",  1625) --> i64: "1.63e3"  f64: "1.62e3"
format!("{:.2e}",  1645) --> i64: "1.65e3"  f64: "1.64e3"
format!("{:.2e}",  1665) --> i64: "1.67e3"  f64: "1.66e3"
format!("{:.2e}",  1685) --> i64: "1.69e3"  f64: "1.68e3"
format!("{:.2e}",  1705) --> i64: "1.71e3"  f64: "1.70e3"
format!("{:.2e}",  1725) --> i64: "1.73e3"  f64: "1.72e3"
format!("{:.2e}",  1745) --> i64: "1.75e3"  f64: "1.74e3"
format!("{:.2e}",  1765) --> i64: "1.77e3"  f64: "1.76e3"
format!("{:.2e}",  1785) --> i64: "1.79e3"  f64: "1.78e3"
format!("{:.2e}",  1805) --> i64: "1.81e3"  f64: "1.80e3"
format!("{:.2e}",  1825) --> i64: "1.83e3"  f64: "1.82e3"
format!("{:.2e}",  1845) --> i64: "1.85e3"  f64: "1.84e3"
format!("{:.2e}",  1865) --> i64: "1.87e3"  f64: "1.86e3"
format!("{:.2e}",  1885) --> i64: "1.89e3"  f64: "1.88e3"
format!("{:.2e}",  1905) --> i64: "1.91e3"  f64: "1.90e3"
format!("{:.2e}",  1925) --> i64: "1.93e3"  f64: "1.92e3"
format!("{:.2e}",  1945) --> i64: "1.95e3"  f64: "1.94e3"
format!("{:.2e}",  1965) --> i64: "1.97e3"  f64: "1.96e3"
format!("{:.2e}",  1985) --> i64: "1.99e3"  f64: "1.98e3"

In this test result, besides the behavior described in this issue, resulting in the wrong number of digits, there’s also the rounding behavior that is different between displaying floats and ints here. I’m not sure whether or not this difference is wanted. If it is wanted, it should be properly documented (on that note, I couldn’t easily find any documentation on the rounding used for floats, either), if it isn’t, it would be yet another issue or bug.

@mj10021
Copy link
Contributor

mj10021 commented Sep 19, 2023

I would like to try and fix this bug
@rustbot claim

@cuviper cuviper linked a pull request Nov 13, 2023 that will close this issue
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 14, 2023
fix rounding issue with exponents in fmt

fixes issue rust-lang#115737 , where the decimal places are rounded incorrectly when formatting scientific notation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-fmt Area: `std::fmt` C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants