From e81964e6f900fbe725ce0fad3c7a29d6536146e9 Mon Sep 17 00:00:00 2001 From: James Dietz Date: Sat, 30 Sep 2023 16:01:01 -0400 Subject: [PATCH 1/2] fix rounding issue with exponents in fmt --- library/core/src/fmt/num.rs | 8 + library/core/tests/fmt/num.rs | 353 +++++++++++++++++++++++++++++++++- 2 files changed, 357 insertions(+), 4 deletions(-) diff --git a/library/core/src/fmt/num.rs b/library/core/src/fmt/num.rs index 4f42f73ebbaff..a1467384c9c02 100644 --- a/library/core/src/fmt/num.rs +++ b/library/core/src/fmt/num.rs @@ -334,8 +334,16 @@ macro_rules! impl_Exp { // round up last digit if rem >= 5 { n += 1; + // if the digit is rounded to the next power + // instead adjust the exponent + if n % 10 == 0 { + n /= 10; + exponent += 1; + } } + // n = 100 } + // assert!(n == 666, "{}\n{}\n{}\n",n, exponent, added_precision); (n, exponent, exponent, added_precision) }; diff --git a/library/core/tests/fmt/num.rs b/library/core/tests/fmt/num.rs index b9ede65c9ff09..540f306e8f6dd 100644 --- a/library/core/tests/fmt/num.rs +++ b/library/core/tests/fmt/num.rs @@ -128,30 +128,375 @@ fn test_format_int_exp_precision() { let big_int: u32 = 314_159_265; assert_eq!(format!("{big_int:.1e}"), format!("{:.1e}", f64::from(big_int))); - //test adding precision + // test adding precision assert_eq!(format!("{:.10e}", i8::MIN), "-1.2800000000e2"); assert_eq!(format!("{:.10e}", i16::MIN), "-3.2768000000e4"); assert_eq!(format!("{:.10e}", i32::MIN), "-2.1474836480e9"); assert_eq!(format!("{:.20e}", i64::MIN), "-9.22337203685477580800e18"); assert_eq!(format!("{:.40e}", i128::MIN), "-1.7014118346046923173168730371588410572800e38"); - //test rounding + // test rounding assert_eq!(format!("{:.1e}", i8::MIN), "-1.3e2"); assert_eq!(format!("{:.1e}", i16::MIN), "-3.3e4"); assert_eq!(format!("{:.1e}", i32::MIN), "-2.1e9"); assert_eq!(format!("{:.1e}", i64::MIN), "-9.2e18"); assert_eq!(format!("{:.1e}", i128::MIN), "-1.7e38"); - //test huge precision + // test huge precision assert_eq!(format!("{:.1000e}", 1), format!("1.{}e0", "0".repeat(1000))); //test zero precision assert_eq!(format!("{:.0e}", 1), format!("1e0",)); assert_eq!(format!("{:.0e}", 35), format!("4e1",)); - //test padding with precision (and sign) + // test padding with precision (and sign) assert_eq!(format!("{:+10.3e}", 1), " +1.000e0"); } +#[test] +fn new_test() { + // test precision remains correct when rounding to next power + assert_eq!(format!("{:.1e}", 999), "1.0e3"); + assert_eq!(format!("{:.1e}", 1001), "1.0e3"); + assert_eq!(format!("{:.1e}", 1000), "1.0e3"); + assert_eq!(format!("{:.0e}", -999), "-1e3"); + assert_eq!(format!("{:.0e}", -998), "-1e3"); + assert_eq!(format!("{:.0e}", -997), "-1e3"); + assert_eq!(format!("{:.0e}", -996), "-1e3"); + assert_eq!(format!("{:.0e}", -995), "-1e3"); + assert_eq!(format!("{:.0e}", -994), "-1e3"); + assert_eq!(format!("{:.0e}", -993), "-1e3"); + assert_eq!(format!("{:.0e}", -992), "-1e3"); + assert_eq!(format!("{:.0e}", -991), "-1e3"); + assert_eq!(format!("{:.0e}", -990), "-1e3"); + assert_eq!(format!("{:.0e}", -989), "-1e3"); + assert_eq!(format!("{:.0e}", -988), "-1e3"); + assert_eq!(format!("{:.0e}", -987), "-1e3"); + assert_eq!(format!("{:.0e}", -986), "-1e3"); + assert_eq!(format!("{:.0e}", -985), "-1e3"); + assert_eq!(format!("{:.0e}", -984), "-1e3"); + assert_eq!(format!("{:.0e}", -983), "-1e3"); + assert_eq!(format!("{:.0e}", -982), "-1e3"); + assert_eq!(format!("{:.0e}", -981), "-1e3"); + assert_eq!(format!("{:.0e}", -980), "-1e3"); + assert_eq!(format!("{:.0e}", -979), "-1e3"); + assert_eq!(format!("{:.0e}", -978), "-1e3"); + assert_eq!(format!("{:.0e}", -977), "-1e3"); + assert_eq!(format!("{:.0e}", -976), "-1e3"); + assert_eq!(format!("{:.0e}", -975), "-1e3"); + assert_eq!(format!("{:.0e}", -974), "-1e3"); + assert_eq!(format!("{:.0e}", -973), "-1e3"); + assert_eq!(format!("{:.0e}", -972), "-1e3"); + assert_eq!(format!("{:.0e}", -971), "-1e3"); + assert_eq!(format!("{:.0e}", -970), "-1e3"); + assert_eq!(format!("{:.0e}", -969), "-1e3"); + assert_eq!(format!("{:.0e}", -968), "-1e3"); + assert_eq!(format!("{:.0e}", -967), "-1e3"); + assert_eq!(format!("{:.0e}", -966), "-1e3"); + assert_eq!(format!("{:.0e}", -965), "-1e3"); + assert_eq!(format!("{:.0e}", -964), "-1e3"); + assert_eq!(format!("{:.0e}", -963), "-1e3"); + assert_eq!(format!("{:.0e}", -962), "-1e3"); + assert_eq!(format!("{:.0e}", -961), "-1e3"); + assert_eq!(format!("{:.0e}", -960), "-1e3"); + assert_eq!(format!("{:.0e}", -959), "-1e3"); + assert_eq!(format!("{:.0e}", -958), "-1e3"); + assert_eq!(format!("{:.0e}", -957), "-1e3"); + assert_eq!(format!("{:.0e}", -956), "-1e3"); + assert_eq!(format!("{:.0e}", -955), "-1e3"); + assert_eq!(format!("{:.0e}", -954), "-1e3"); + assert_eq!(format!("{:.0e}", -953), "-1e3"); + assert_eq!(format!("{:.0e}", -952), "-1e3"); + assert_eq!(format!("{:.0e}", -951), "-1e3"); + assert_eq!(format!("{:.0e}", -950), "-1e3"); + assert_eq!(format!("{:.0e}", -850), "-9e2"); + assert_eq!(format!("{:.0e}", -650), "-7e2"); + assert_eq!(format!("{:.0e}", -450), "-5e2"); + assert_eq!(format!("{:.0e}", -250), "-3e2"); + assert_eq!(format!("{:.0e}", -99), "-1e2"); + assert_eq!(format!("{:.0e}", -98), "-1e2"); + assert_eq!(format!("{:.0e}", -97), "-1e2"); + assert_eq!(format!("{:.0e}", -96), "-1e2"); + assert_eq!(format!("{:.0e}", -95), "-1e2"); + assert_eq!(format!("{:.0e}", -85), "-9e1"); + assert_eq!(format!("{:.0e}", -65), "-7e1"); + assert_eq!(format!("{:.0e}", -45), "-5e1"); + assert_eq!(format!("{:.0e}", -25), "-3e1"); + assert_eq!(format!("{:.0e}", 25), "3e1"); + assert_eq!(format!("{:.0e}", 45), "5e1"); + assert_eq!(format!("{:.0e}", 65), "7e1"); + assert_eq!(format!("{:.0e}", 85), "9e1"); + assert_eq!(format!("{:.0e}", 94), "9e1"); + assert_eq!(format!("{:.0e}", 95), "1e2"); + assert_eq!(format!("{:.0e}", 96), "1e2"); + assert_eq!(format!("{:.0e}", 97), "1e2"); + assert_eq!(format!("{:.0e}", 98), "1e2"); + assert_eq!(format!("{:.0e}", 99), "1e2"); + assert_eq!(format!("{:.0e}", 250), "3e2"); + assert_eq!(format!("{:.0e}", 450), "5e2"); + assert_eq!(format!("{:.0e}", 650), "7e2"); + assert_eq!(format!("{:.0e}", 850), "9e2"); + assert_eq!(format!("{:.0e}", 950), "1e3"); + assert_eq!(format!("{:.0e}", 951), "1e3"); + assert_eq!(format!("{:.0e}", 952), "1e3"); + assert_eq!(format!("{:.0e}", 953), "1e3"); + assert_eq!(format!("{:.0e}", 954), "1e3"); + assert_eq!(format!("{:.0e}", 955), "1e3"); + assert_eq!(format!("{:.0e}", 956), "1e3"); + assert_eq!(format!("{:.0e}", 957), "1e3"); + assert_eq!(format!("{:.0e}", 958), "1e3"); + assert_eq!(format!("{:.0e}", 959), "1e3"); + assert_eq!(format!("{:.0e}", 960), "1e3"); + assert_eq!(format!("{:.0e}", 961), "1e3"); + assert_eq!(format!("{:.0e}", 962), "1e3"); + assert_eq!(format!("{:.0e}", 963), "1e3"); + assert_eq!(format!("{:.0e}", 964), "1e3"); + assert_eq!(format!("{:.0e}", 965), "1e3"); + assert_eq!(format!("{:.0e}", 966), "1e3"); + assert_eq!(format!("{:.0e}", 967), "1e3"); + assert_eq!(format!("{:.0e}", 968), "1e3"); + assert_eq!(format!("{:.0e}", 969), "1e3"); + assert_eq!(format!("{:.0e}", 970), "1e3"); + assert_eq!(format!("{:.0e}", 971), "1e3"); + assert_eq!(format!("{:.0e}", 972), "1e3"); + assert_eq!(format!("{:.0e}", 973), "1e3"); + assert_eq!(format!("{:.0e}", 974), "1e3"); + assert_eq!(format!("{:.0e}", 975), "1e3"); + assert_eq!(format!("{:.0e}", 976), "1e3"); + assert_eq!(format!("{:.0e}", 977), "1e3"); + assert_eq!(format!("{:.0e}", 978), "1e3"); + assert_eq!(format!("{:.0e}", 979), "1e3"); + assert_eq!(format!("{:.0e}", 980), "1e3"); + assert_eq!(format!("{:.0e}", 981), "1e3"); + assert_eq!(format!("{:.0e}", 982), "1e3"); + assert_eq!(format!("{:.0e}", 983), "1e3"); + assert_eq!(format!("{:.0e}", 984), "1e3"); + assert_eq!(format!("{:.0e}", 985), "1e3"); + assert_eq!(format!("{:.0e}", 986), "1e3"); + assert_eq!(format!("{:.0e}", 987), "1e3"); + assert_eq!(format!("{:.0e}", 988), "1e3"); + assert_eq!(format!("{:.0e}", 989), "1e3"); + assert_eq!(format!("{:.0e}", 990), "1e3"); + assert_eq!(format!("{:.0e}", 991), "1e3"); + assert_eq!(format!("{:.0e}", 992), "1e3"); + assert_eq!(format!("{:.0e}", 993), "1e3"); + assert_eq!(format!("{:.0e}", 994), "1e3"); + assert_eq!(format!("{:.0e}", 995), "1e3"); + assert_eq!(format!("{:.0e}", 996), "1e3"); + assert_eq!(format!("{:.0e}", 997), "1e3"); + assert_eq!(format!("{:.0e}", 998), "1e3"); + assert_eq!(format!("{:.0e}", 999), "1e3"); + assert_eq!(format!("{:.1e}", -1850), "-1.9e3"); + assert_eq!(format!("{:.1e}", -1650), "-1.7e3"); + assert_eq!(format!("{:.1e}", -1450), "-1.5e3"); + assert_eq!(format!("{:.1e}", -1250), "-1.3e3"); + assert_eq!(format!("{:.1e}", -1050), "-1.1e3"); + assert_eq!(format!("{:.1e}", -999), "-1.0e3"); + assert_eq!(format!("{:.1e}", -998), "-1.0e3"); + assert_eq!(format!("{:.1e}", -997), "-1.0e3"); + assert_eq!(format!("{:.1e}", -996), "-1.0e3"); + assert_eq!(format!("{:.1e}", -995), "-1.0e3"); + assert_eq!(format!("{:.1e}", -985), "-9.9e2"); + assert_eq!(format!("{:.1e}", -965), "-9.7e2"); + assert_eq!(format!("{:.1e}", -945), "-9.5e2"); + assert_eq!(format!("{:.1e}", -925), "-9.3e2"); + assert_eq!(format!("{:.1e}", -905), "-9.1e2"); + assert_eq!(format!("{:.1e}", -885), "-8.9e2"); + assert_eq!(format!("{:.1e}", -865), "-8.7e2"); + assert_eq!(format!("{:.1e}", -845), "-8.5e2"); + assert_eq!(format!("{:.1e}", -825), "-8.3e2"); + assert_eq!(format!("{:.1e}", -805), "-8.1e2"); + assert_eq!(format!("{:.1e}", -785), "-7.9e2"); + assert_eq!(format!("{:.1e}", -765), "-7.7e2"); + assert_eq!(format!("{:.1e}", -745), "-7.5e2"); + assert_eq!(format!("{:.1e}", -725), "-7.3e2"); + assert_eq!(format!("{:.1e}", -705), "-7.1e2"); + assert_eq!(format!("{:.1e}", -685), "-6.9e2"); + assert_eq!(format!("{:.1e}", -665), "-6.7e2"); + assert_eq!(format!("{:.1e}", -645), "-6.5e2"); + assert_eq!(format!("{:.1e}", -625), "-6.3e2"); + assert_eq!(format!("{:.1e}", -605), "-6.1e2"); + assert_eq!(format!("{:.1e}", -585), "-5.9e2"); + assert_eq!(format!("{:.1e}", -565), "-5.7e2"); + assert_eq!(format!("{:.1e}", -545), "-5.5e2"); + assert_eq!(format!("{:.1e}", -525), "-5.3e2"); + assert_eq!(format!("{:.1e}", -505), "-5.1e2"); + assert_eq!(format!("{:.1e}", -485), "-4.9e2"); + assert_eq!(format!("{:.1e}", -465), "-4.7e2"); + assert_eq!(format!("{:.1e}", -445), "-4.5e2"); + assert_eq!(format!("{:.1e}", -425), "-4.3e2"); + assert_eq!(format!("{:.1e}", -405), "-4.1e2"); + assert_eq!(format!("{:.1e}", -385), "-3.9e2"); + assert_eq!(format!("{:.1e}", -365), "-3.7e2"); + assert_eq!(format!("{:.1e}", -345), "-3.5e2"); + assert_eq!(format!("{:.1e}", -325), "-3.3e2"); + assert_eq!(format!("{:.1e}", -305), "-3.1e2"); + assert_eq!(format!("{:.1e}", -285), "-2.9e2"); + assert_eq!(format!("{:.1e}", -265), "-2.7e2"); + assert_eq!(format!("{:.1e}", -245), "-2.5e2"); + assert_eq!(format!("{:.1e}", -225), "-2.3e2"); + assert_eq!(format!("{:.1e}", -205), "-2.1e2"); + assert_eq!(format!("{:.1e}", -185), "-1.9e2"); + assert_eq!(format!("{:.1e}", -165), "-1.7e2"); + assert_eq!(format!("{:.1e}", -145), "-1.5e2"); + assert_eq!(format!("{:.1e}", -125), "-1.3e2"); + assert_eq!(format!("{:.1e}", -105), "-1.1e2"); + assert_eq!(format!("{:.1e}", 105), "1.1e2"); + assert_eq!(format!("{:.1e}", 125), "1.3e2"); + assert_eq!(format!("{:.1e}", 145), "1.5e2"); + assert_eq!(format!("{:.1e}", 165), "1.7e2"); + assert_eq!(format!("{:.1e}", 185), "1.9e2"); + assert_eq!(format!("{:.1e}", 205), "2.1e2"); + assert_eq!(format!("{:.1e}", 225), "2.3e2"); + assert_eq!(format!("{:.1e}", 245), "2.5e2"); + assert_eq!(format!("{:.1e}", 265), "2.7e2"); + assert_eq!(format!("{:.1e}", 285), "2.9e2"); + assert_eq!(format!("{:.1e}", 305), "3.1e2"); + assert_eq!(format!("{:.1e}", 325), "3.3e2"); + assert_eq!(format!("{:.1e}", 345), "3.5e2"); + assert_eq!(format!("{:.1e}", 365), "3.7e2"); + assert_eq!(format!("{:.1e}", 385), "3.9e2"); + assert_eq!(format!("{:.1e}", 405), "4.1e2"); + assert_eq!(format!("{:.1e}", 425), "4.3e2"); + assert_eq!(format!("{:.1e}", 445), "4.5e2"); + assert_eq!(format!("{:.1e}", 465), "4.7e2"); + assert_eq!(format!("{:.1e}", 485), "4.9e2"); + assert_eq!(format!("{:.1e}", 505), "5.1e2"); + assert_eq!(format!("{:.1e}", 525), "5.3e2"); + assert_eq!(format!("{:.1e}", 545), "5.5e2"); + assert_eq!(format!("{:.1e}", 565), "5.7e2"); + assert_eq!(format!("{:.1e}", 585), "5.9e2"); + assert_eq!(format!("{:.1e}", 605), "6.1e2"); + assert_eq!(format!("{:.1e}", 625), "6.3e2"); + assert_eq!(format!("{:.1e}", 645), "6.5e2"); + assert_eq!(format!("{:.1e}", 665), "6.7e2"); + assert_eq!(format!("{:.1e}", 685), "6.9e2"); + assert_eq!(format!("{:.1e}", 705), "7.1e2"); + assert_eq!(format!("{:.1e}", 725), "7.3e2"); + assert_eq!(format!("{:.1e}", 745), "7.5e2"); + assert_eq!(format!("{:.1e}", 765), "7.7e2"); + assert_eq!(format!("{:.1e}", 785), "7.9e2"); + assert_eq!(format!("{:.1e}", 805), "8.1e2"); + assert_eq!(format!("{:.1e}", 825), "8.3e2"); + assert_eq!(format!("{:.1e}", 845), "8.5e2"); + assert_eq!(format!("{:.1e}", 865), "8.7e2"); + assert_eq!(format!("{:.1e}", 885), "8.9e2"); + assert_eq!(format!("{:.1e}", 905), "9.1e2"); + assert_eq!(format!("{:.1e}", 925), "9.3e2"); + assert_eq!(format!("{:.1e}", 945), "9.5e2"); + assert_eq!(format!("{:.1e}", 965), "9.7e2"); + assert_eq!(format!("{:.1e}", 985), "9.9e2"); + assert_eq!(format!("{:.1e}", 995), "1.0e3"); + assert_eq!(format!("{:.1e}", 996), "1.0e3"); + assert_eq!(format!("{:.1e}", 997), "1.0e3"); + assert_eq!(format!("{:.1e}", 998), "1.0e3"); + assert_eq!(format!("{:.1e}", 999), "1.0e3"); + assert_eq!(format!("{:.1e}", 1050), "1.1e3"); + assert_eq!(format!("{:.1e}", 1250), "1.3e3"); + assert_eq!(format!("{:.1e}", 1450), "1.5e3"); + assert_eq!(format!("{:.1e}", 1650), "1.7e3"); + assert_eq!(format!("{:.1e}", 1850), "1.9e3"); + assert_eq!(format!("{:.2e}", -1985), "-1.99e3"); + assert_eq!(format!("{:.2e}", -1965), "-1.97e3"); + assert_eq!(format!("{:.2e}", -1945), "-1.95e3"); + assert_eq!(format!("{:.2e}", -1925), "-1.93e3"); + assert_eq!(format!("{:.2e}", -1905), "-1.91e3"); + assert_eq!(format!("{:.2e}", -1885), "-1.89e3"); + assert_eq!(format!("{:.2e}", -1865), "-1.87e3"); + assert_eq!(format!("{:.2e}", -1845), "-1.85e3"); + assert_eq!(format!("{:.2e}", -1825), "-1.83e3"); + assert_eq!(format!("{:.2e}", -1805), "-1.81e3"); + assert_eq!(format!("{:.2e}", -1785), "-1.79e3"); + assert_eq!(format!("{:.2e}", -1765), "-1.77e3"); + assert_eq!(format!("{:.2e}", -1745), "-1.75e3"); + assert_eq!(format!("{:.2e}", -1725), "-1.73e3"); + assert_eq!(format!("{:.2e}", -1705), "-1.71e3"); + assert_eq!(format!("{:.2e}", -1685), "-1.69e3"); + assert_eq!(format!("{:.2e}", -1665), "-1.67e3"); + assert_eq!(format!("{:.2e}", -1645), "-1.65e3"); + assert_eq!(format!("{:.2e}", -1625), "-1.63e3"); + assert_eq!(format!("{:.2e}", -1605), "-1.61e3"); + assert_eq!(format!("{:.2e}", -1585), "-1.59e3"); + assert_eq!(format!("{:.2e}", -1565), "-1.57e3"); + assert_eq!(format!("{:.2e}", -1545), "-1.55e3"); + assert_eq!(format!("{:.2e}", -1525), "-1.53e3"); + assert_eq!(format!("{:.2e}", -1505), "-1.51e3"); + assert_eq!(format!("{:.2e}", -1485), "-1.49e3"); + assert_eq!(format!("{:.2e}", -1465), "-1.47e3"); + assert_eq!(format!("{:.2e}", -1445), "-1.45e3"); + assert_eq!(format!("{:.2e}", -1425), "-1.43e3"); + assert_eq!(format!("{:.2e}", -1405), "-1.41e3"); + assert_eq!(format!("{:.2e}", -1385), "-1.39e3"); + assert_eq!(format!("{:.2e}", -1365), "-1.37e3"); + assert_eq!(format!("{:.2e}", -1345), "-1.35e3"); + assert_eq!(format!("{:.2e}", -1325), "-1.33e3"); + assert_eq!(format!("{:.2e}", -1305), "-1.31e3"); + assert_eq!(format!("{:.2e}", -1285), "-1.29e3"); + assert_eq!(format!("{:.2e}", -1265), "-1.27e3"); + assert_eq!(format!("{:.2e}", -1245), "-1.25e3"); + assert_eq!(format!("{:.2e}", -1225), "-1.23e3"); + assert_eq!(format!("{:.2e}", -1205), "-1.21e3"); + assert_eq!(format!("{:.2e}", -1185), "-1.19e3"); + assert_eq!(format!("{:.2e}", -1165), "-1.17e3"); + assert_eq!(format!("{:.2e}", -1145), "-1.15e3"); + assert_eq!(format!("{:.2e}", -1125), "-1.13e3"); + assert_eq!(format!("{:.2e}", -1105), "-1.11e3"); + assert_eq!(format!("{:.2e}", -1085), "-1.09e3"); + assert_eq!(format!("{:.2e}", -1065), "-1.07e3"); + assert_eq!(format!("{:.2e}", -1045), "-1.05e3"); + assert_eq!(format!("{:.2e}", -1025), "-1.03e3"); + assert_eq!(format!("{:.2e}", -1005), "-1.01e3"); + assert_eq!(format!("{:.2e}", 1005), "1.01e3" ); + assert_eq!(format!("{:.2e}", 1025), "1.03e3" ); + assert_eq!(format!("{:.2e}", 1045), "1.05e3" ); + assert_eq!(format!("{:.2e}", 1065), "1.07e3" ); + assert_eq!(format!("{:.2e}", 1085), "1.09e3" ); + assert_eq!(format!("{:.2e}", 1105), "1.11e3" ); + assert_eq!(format!("{:.2e}", 1125), "1.13e3" ); + assert_eq!(format!("{:.2e}", 1145), "1.15e3" ); + assert_eq!(format!("{:.2e}", 1165), "1.17e3" ); + assert_eq!(format!("{:.2e}", 1185), "1.19e3" ); + assert_eq!(format!("{:.2e}", 1205), "1.21e3" ); + assert_eq!(format!("{:.2e}", 1225), "1.23e3" ); + assert_eq!(format!("{:.2e}", 1245), "1.25e3" ); + assert_eq!(format!("{:.2e}", 1265), "1.27e3" ); + assert_eq!(format!("{:.2e}", 1285), "1.29e3" ); + assert_eq!(format!("{:.2e}", 1305), "1.31e3" ); + assert_eq!(format!("{:.2e}", 1325), "1.33e3" ); + assert_eq!(format!("{:.2e}", 1345), "1.35e3" ); + assert_eq!(format!("{:.2e}", 1365), "1.37e3" ); + assert_eq!(format!("{:.2e}", 1385), "1.39e3" ); + assert_eq!(format!("{:.2e}", 1405), "1.41e3" ); + assert_eq!(format!("{:.2e}", 1425), "1.43e3" ); + assert_eq!(format!("{:.2e}", 1445), "1.45e3" ); + assert_eq!(format!("{:.2e}", 1465), "1.47e3" ); + assert_eq!(format!("{:.2e}", 1485), "1.49e3" ); + assert_eq!(format!("{:.2e}", 1505), "1.51e3" ); + assert_eq!(format!("{:.2e}", 1525), "1.53e3" ); + assert_eq!(format!("{:.2e}", 1545), "1.55e3" ); + assert_eq!(format!("{:.2e}", 1565), "1.57e3" ); + assert_eq!(format!("{:.2e}", 1585), "1.59e3" ); + assert_eq!(format!("{:.2e}", 1605), "1.61e3" ); + assert_eq!(format!("{:.2e}", 1625), "1.63e3" ); + assert_eq!(format!("{:.2e}", 1645), "1.65e3" ); + assert_eq!(format!("{:.2e}", 1665), "1.67e3" ); + assert_eq!(format!("{:.2e}", 1685), "1.69e3" ); + assert_eq!(format!("{:.2e}", 1705), "1.71e3" ); + assert_eq!(format!("{:.2e}", 1725), "1.73e3" ); + assert_eq!(format!("{:.2e}", 1745), "1.75e3" ); + assert_eq!(format!("{:.2e}", 1765), "1.77e3" ); + assert_eq!(format!("{:.2e}", 1785), "1.79e3" ); + assert_eq!(format!("{:.2e}", 1805), "1.81e3" ); + assert_eq!(format!("{:.2e}", 1825), "1.83e3" ); + assert_eq!(format!("{:.2e}", 1845), "1.85e3" ); + assert_eq!(format!("{:.2e}", 1865), "1.87e3" ); + assert_eq!(format!("{:.2e}", 1885), "1.89e3" ); + assert_eq!(format!("{:.2e}", 1905), "1.91e3" ); + assert_eq!(format!("{:.2e}", 1925), "1.93e3" ); + assert_eq!(format!("{:.2e}", 1945), "1.95e3" ); + assert_eq!(format!("{:.2e}", 1965), "1.97e3" ); + assert_eq!(format!("{:.2e}", 1985), "1.99e3" ); +} + #[test] fn test_format_int_zero() { assert_eq!(format!("{}", 0), "0"); From 3f0908f47cff0f80bf4da69e845b6efdd333f6ea Mon Sep 17 00:00:00 2001 From: James Dietz Date: Fri, 27 Oct 2023 17:14:40 -0400 Subject: [PATCH 2/2] round to even --- library/core/src/fmt/num.rs | 9 +- library/core/tests/fmt/num.rs | 353 +--------------------------------- 2 files changed, 13 insertions(+), 349 deletions(-) diff --git a/library/core/src/fmt/num.rs b/library/core/src/fmt/num.rs index a1467384c9c02..778515f8616c4 100644 --- a/library/core/src/fmt/num.rs +++ b/library/core/src/fmt/num.rs @@ -309,7 +309,6 @@ macro_rules! impl_Exp { n /= 10; exponent += 1; } - let (added_precision, subtracted_precision) = match f.precision() { Some(fmt_prec) => { // number of decimal digits minus 1 @@ -331,19 +330,17 @@ macro_rules! impl_Exp { let rem = n % 10; n /= 10; exponent += 1; - // round up last digit - if rem >= 5 { + // round up last digit, round to even on a tie + if rem > 5 || (rem == 5 && (n % 2 != 0 || subtracted_precision > 1 )) { n += 1; // if the digit is rounded to the next power // instead adjust the exponent - if n % 10 == 0 { + if n.ilog10() > (n - 1).ilog10() { n /= 10; exponent += 1; } } - // n = 100 } - // assert!(n == 666, "{}\n{}\n{}\n",n, exponent, added_precision); (n, exponent, exponent, added_precision) }; diff --git a/library/core/tests/fmt/num.rs b/library/core/tests/fmt/num.rs index 540f306e8f6dd..1ddcd5ab7955f 100644 --- a/library/core/tests/fmt/num.rs +++ b/library/core/tests/fmt/num.rs @@ -150,351 +150,18 @@ fn test_format_int_exp_precision() { // test padding with precision (and sign) assert_eq!(format!("{:+10.3e}", 1), " +1.000e0"); -} -#[test] -fn new_test() { // test precision remains correct when rounding to next power - assert_eq!(format!("{:.1e}", 999), "1.0e3"); - assert_eq!(format!("{:.1e}", 1001), "1.0e3"); - assert_eq!(format!("{:.1e}", 1000), "1.0e3"); - assert_eq!(format!("{:.0e}", -999), "-1e3"); - assert_eq!(format!("{:.0e}", -998), "-1e3"); - assert_eq!(format!("{:.0e}", -997), "-1e3"); - assert_eq!(format!("{:.0e}", -996), "-1e3"); - assert_eq!(format!("{:.0e}", -995), "-1e3"); - assert_eq!(format!("{:.0e}", -994), "-1e3"); - assert_eq!(format!("{:.0e}", -993), "-1e3"); - assert_eq!(format!("{:.0e}", -992), "-1e3"); - assert_eq!(format!("{:.0e}", -991), "-1e3"); - assert_eq!(format!("{:.0e}", -990), "-1e3"); - assert_eq!(format!("{:.0e}", -989), "-1e3"); - assert_eq!(format!("{:.0e}", -988), "-1e3"); - assert_eq!(format!("{:.0e}", -987), "-1e3"); - assert_eq!(format!("{:.0e}", -986), "-1e3"); - assert_eq!(format!("{:.0e}", -985), "-1e3"); - assert_eq!(format!("{:.0e}", -984), "-1e3"); - assert_eq!(format!("{:.0e}", -983), "-1e3"); - assert_eq!(format!("{:.0e}", -982), "-1e3"); - assert_eq!(format!("{:.0e}", -981), "-1e3"); - assert_eq!(format!("{:.0e}", -980), "-1e3"); - assert_eq!(format!("{:.0e}", -979), "-1e3"); - assert_eq!(format!("{:.0e}", -978), "-1e3"); - assert_eq!(format!("{:.0e}", -977), "-1e3"); - assert_eq!(format!("{:.0e}", -976), "-1e3"); - assert_eq!(format!("{:.0e}", -975), "-1e3"); - assert_eq!(format!("{:.0e}", -974), "-1e3"); - assert_eq!(format!("{:.0e}", -973), "-1e3"); - assert_eq!(format!("{:.0e}", -972), "-1e3"); - assert_eq!(format!("{:.0e}", -971), "-1e3"); - assert_eq!(format!("{:.0e}", -970), "-1e3"); - assert_eq!(format!("{:.0e}", -969), "-1e3"); - assert_eq!(format!("{:.0e}", -968), "-1e3"); - assert_eq!(format!("{:.0e}", -967), "-1e3"); - assert_eq!(format!("{:.0e}", -966), "-1e3"); - assert_eq!(format!("{:.0e}", -965), "-1e3"); - assert_eq!(format!("{:.0e}", -964), "-1e3"); - assert_eq!(format!("{:.0e}", -963), "-1e3"); - assert_eq!(format!("{:.0e}", -962), "-1e3"); - assert_eq!(format!("{:.0e}", -961), "-1e3"); - assert_eq!(format!("{:.0e}", -960), "-1e3"); - assert_eq!(format!("{:.0e}", -959), "-1e3"); - assert_eq!(format!("{:.0e}", -958), "-1e3"); - assert_eq!(format!("{:.0e}", -957), "-1e3"); - assert_eq!(format!("{:.0e}", -956), "-1e3"); - assert_eq!(format!("{:.0e}", -955), "-1e3"); - assert_eq!(format!("{:.0e}", -954), "-1e3"); - assert_eq!(format!("{:.0e}", -953), "-1e3"); - assert_eq!(format!("{:.0e}", -952), "-1e3"); - assert_eq!(format!("{:.0e}", -951), "-1e3"); - assert_eq!(format!("{:.0e}", -950), "-1e3"); - assert_eq!(format!("{:.0e}", -850), "-9e2"); - assert_eq!(format!("{:.0e}", -650), "-7e2"); - assert_eq!(format!("{:.0e}", -450), "-5e2"); - assert_eq!(format!("{:.0e}", -250), "-3e2"); - assert_eq!(format!("{:.0e}", -99), "-1e2"); - assert_eq!(format!("{:.0e}", -98), "-1e2"); - assert_eq!(format!("{:.0e}", -97), "-1e2"); - assert_eq!(format!("{:.0e}", -96), "-1e2"); - assert_eq!(format!("{:.0e}", -95), "-1e2"); - assert_eq!(format!("{:.0e}", -85), "-9e1"); - assert_eq!(format!("{:.0e}", -65), "-7e1"); - assert_eq!(format!("{:.0e}", -45), "-5e1"); - assert_eq!(format!("{:.0e}", -25), "-3e1"); - assert_eq!(format!("{:.0e}", 25), "3e1"); - assert_eq!(format!("{:.0e}", 45), "5e1"); - assert_eq!(format!("{:.0e}", 65), "7e1"); - assert_eq!(format!("{:.0e}", 85), "9e1"); - assert_eq!(format!("{:.0e}", 94), "9e1"); - assert_eq!(format!("{:.0e}", 95), "1e2"); - assert_eq!(format!("{:.0e}", 96), "1e2"); - assert_eq!(format!("{:.0e}", 97), "1e2"); - assert_eq!(format!("{:.0e}", 98), "1e2"); - assert_eq!(format!("{:.0e}", 99), "1e2"); - assert_eq!(format!("{:.0e}", 250), "3e2"); - assert_eq!(format!("{:.0e}", 450), "5e2"); - assert_eq!(format!("{:.0e}", 650), "7e2"); - assert_eq!(format!("{:.0e}", 850), "9e2"); - assert_eq!(format!("{:.0e}", 950), "1e3"); - assert_eq!(format!("{:.0e}", 951), "1e3"); - assert_eq!(format!("{:.0e}", 952), "1e3"); - assert_eq!(format!("{:.0e}", 953), "1e3"); - assert_eq!(format!("{:.0e}", 954), "1e3"); - assert_eq!(format!("{:.0e}", 955), "1e3"); - assert_eq!(format!("{:.0e}", 956), "1e3"); - assert_eq!(format!("{:.0e}", 957), "1e3"); - assert_eq!(format!("{:.0e}", 958), "1e3"); - assert_eq!(format!("{:.0e}", 959), "1e3"); - assert_eq!(format!("{:.0e}", 960), "1e3"); - assert_eq!(format!("{:.0e}", 961), "1e3"); - assert_eq!(format!("{:.0e}", 962), "1e3"); - assert_eq!(format!("{:.0e}", 963), "1e3"); - assert_eq!(format!("{:.0e}", 964), "1e3"); - assert_eq!(format!("{:.0e}", 965), "1e3"); - assert_eq!(format!("{:.0e}", 966), "1e3"); - assert_eq!(format!("{:.0e}", 967), "1e3"); - assert_eq!(format!("{:.0e}", 968), "1e3"); - assert_eq!(format!("{:.0e}", 969), "1e3"); - assert_eq!(format!("{:.0e}", 970), "1e3"); - assert_eq!(format!("{:.0e}", 971), "1e3"); - assert_eq!(format!("{:.0e}", 972), "1e3"); - assert_eq!(format!("{:.0e}", 973), "1e3"); - assert_eq!(format!("{:.0e}", 974), "1e3"); - assert_eq!(format!("{:.0e}", 975), "1e3"); - assert_eq!(format!("{:.0e}", 976), "1e3"); - assert_eq!(format!("{:.0e}", 977), "1e3"); - assert_eq!(format!("{:.0e}", 978), "1e3"); - assert_eq!(format!("{:.0e}", 979), "1e3"); - assert_eq!(format!("{:.0e}", 980), "1e3"); - assert_eq!(format!("{:.0e}", 981), "1e3"); - assert_eq!(format!("{:.0e}", 982), "1e3"); - assert_eq!(format!("{:.0e}", 983), "1e3"); - assert_eq!(format!("{:.0e}", 984), "1e3"); - assert_eq!(format!("{:.0e}", 985), "1e3"); - assert_eq!(format!("{:.0e}", 986), "1e3"); - assert_eq!(format!("{:.0e}", 987), "1e3"); - assert_eq!(format!("{:.0e}", 988), "1e3"); - assert_eq!(format!("{:.0e}", 989), "1e3"); - assert_eq!(format!("{:.0e}", 990), "1e3"); - assert_eq!(format!("{:.0e}", 991), "1e3"); - assert_eq!(format!("{:.0e}", 992), "1e3"); - assert_eq!(format!("{:.0e}", 993), "1e3"); - assert_eq!(format!("{:.0e}", 994), "1e3"); - assert_eq!(format!("{:.0e}", 995), "1e3"); - assert_eq!(format!("{:.0e}", 996), "1e3"); - assert_eq!(format!("{:.0e}", 997), "1e3"); - assert_eq!(format!("{:.0e}", 998), "1e3"); - assert_eq!(format!("{:.0e}", 999), "1e3"); - assert_eq!(format!("{:.1e}", -1850), "-1.9e3"); - assert_eq!(format!("{:.1e}", -1650), "-1.7e3"); - assert_eq!(format!("{:.1e}", -1450), "-1.5e3"); - assert_eq!(format!("{:.1e}", -1250), "-1.3e3"); - assert_eq!(format!("{:.1e}", -1050), "-1.1e3"); - assert_eq!(format!("{:.1e}", -999), "-1.0e3"); - assert_eq!(format!("{:.1e}", -998), "-1.0e3"); - assert_eq!(format!("{:.1e}", -997), "-1.0e3"); - assert_eq!(format!("{:.1e}", -996), "-1.0e3"); - assert_eq!(format!("{:.1e}", -995), "-1.0e3"); - assert_eq!(format!("{:.1e}", -985), "-9.9e2"); - assert_eq!(format!("{:.1e}", -965), "-9.7e2"); - assert_eq!(format!("{:.1e}", -945), "-9.5e2"); - assert_eq!(format!("{:.1e}", -925), "-9.3e2"); - assert_eq!(format!("{:.1e}", -905), "-9.1e2"); - assert_eq!(format!("{:.1e}", -885), "-8.9e2"); - assert_eq!(format!("{:.1e}", -865), "-8.7e2"); - assert_eq!(format!("{:.1e}", -845), "-8.5e2"); - assert_eq!(format!("{:.1e}", -825), "-8.3e2"); - assert_eq!(format!("{:.1e}", -805), "-8.1e2"); - assert_eq!(format!("{:.1e}", -785), "-7.9e2"); - assert_eq!(format!("{:.1e}", -765), "-7.7e2"); - assert_eq!(format!("{:.1e}", -745), "-7.5e2"); - assert_eq!(format!("{:.1e}", -725), "-7.3e2"); - assert_eq!(format!("{:.1e}", -705), "-7.1e2"); - assert_eq!(format!("{:.1e}", -685), "-6.9e2"); - assert_eq!(format!("{:.1e}", -665), "-6.7e2"); - assert_eq!(format!("{:.1e}", -645), "-6.5e2"); - assert_eq!(format!("{:.1e}", -625), "-6.3e2"); - assert_eq!(format!("{:.1e}", -605), "-6.1e2"); - assert_eq!(format!("{:.1e}", -585), "-5.9e2"); - assert_eq!(format!("{:.1e}", -565), "-5.7e2"); - assert_eq!(format!("{:.1e}", -545), "-5.5e2"); - assert_eq!(format!("{:.1e}", -525), "-5.3e2"); - assert_eq!(format!("{:.1e}", -505), "-5.1e2"); - assert_eq!(format!("{:.1e}", -485), "-4.9e2"); - assert_eq!(format!("{:.1e}", -465), "-4.7e2"); - assert_eq!(format!("{:.1e}", -445), "-4.5e2"); - assert_eq!(format!("{:.1e}", -425), "-4.3e2"); - assert_eq!(format!("{:.1e}", -405), "-4.1e2"); - assert_eq!(format!("{:.1e}", -385), "-3.9e2"); - assert_eq!(format!("{:.1e}", -365), "-3.7e2"); - assert_eq!(format!("{:.1e}", -345), "-3.5e2"); - assert_eq!(format!("{:.1e}", -325), "-3.3e2"); - assert_eq!(format!("{:.1e}", -305), "-3.1e2"); - assert_eq!(format!("{:.1e}", -285), "-2.9e2"); - assert_eq!(format!("{:.1e}", -265), "-2.7e2"); - assert_eq!(format!("{:.1e}", -245), "-2.5e2"); - assert_eq!(format!("{:.1e}", -225), "-2.3e2"); - assert_eq!(format!("{:.1e}", -205), "-2.1e2"); - assert_eq!(format!("{:.1e}", -185), "-1.9e2"); - assert_eq!(format!("{:.1e}", -165), "-1.7e2"); - assert_eq!(format!("{:.1e}", -145), "-1.5e2"); - assert_eq!(format!("{:.1e}", -125), "-1.3e2"); - assert_eq!(format!("{:.1e}", -105), "-1.1e2"); - assert_eq!(format!("{:.1e}", 105), "1.1e2"); - assert_eq!(format!("{:.1e}", 125), "1.3e2"); - assert_eq!(format!("{:.1e}", 145), "1.5e2"); - assert_eq!(format!("{:.1e}", 165), "1.7e2"); - assert_eq!(format!("{:.1e}", 185), "1.9e2"); - assert_eq!(format!("{:.1e}", 205), "2.1e2"); - assert_eq!(format!("{:.1e}", 225), "2.3e2"); - assert_eq!(format!("{:.1e}", 245), "2.5e2"); - assert_eq!(format!("{:.1e}", 265), "2.7e2"); - assert_eq!(format!("{:.1e}", 285), "2.9e2"); - assert_eq!(format!("{:.1e}", 305), "3.1e2"); - assert_eq!(format!("{:.1e}", 325), "3.3e2"); - assert_eq!(format!("{:.1e}", 345), "3.5e2"); - assert_eq!(format!("{:.1e}", 365), "3.7e2"); - assert_eq!(format!("{:.1e}", 385), "3.9e2"); - assert_eq!(format!("{:.1e}", 405), "4.1e2"); - assert_eq!(format!("{:.1e}", 425), "4.3e2"); - assert_eq!(format!("{:.1e}", 445), "4.5e2"); - assert_eq!(format!("{:.1e}", 465), "4.7e2"); - assert_eq!(format!("{:.1e}", 485), "4.9e2"); - assert_eq!(format!("{:.1e}", 505), "5.1e2"); - assert_eq!(format!("{:.1e}", 525), "5.3e2"); - assert_eq!(format!("{:.1e}", 545), "5.5e2"); - assert_eq!(format!("{:.1e}", 565), "5.7e2"); - assert_eq!(format!("{:.1e}", 585), "5.9e2"); - assert_eq!(format!("{:.1e}", 605), "6.1e2"); - assert_eq!(format!("{:.1e}", 625), "6.3e2"); - assert_eq!(format!("{:.1e}", 645), "6.5e2"); - assert_eq!(format!("{:.1e}", 665), "6.7e2"); - assert_eq!(format!("{:.1e}", 685), "6.9e2"); - assert_eq!(format!("{:.1e}", 705), "7.1e2"); - assert_eq!(format!("{:.1e}", 725), "7.3e2"); - assert_eq!(format!("{:.1e}", 745), "7.5e2"); - assert_eq!(format!("{:.1e}", 765), "7.7e2"); - assert_eq!(format!("{:.1e}", 785), "7.9e2"); - assert_eq!(format!("{:.1e}", 805), "8.1e2"); - assert_eq!(format!("{:.1e}", 825), "8.3e2"); - assert_eq!(format!("{:.1e}", 845), "8.5e2"); - assert_eq!(format!("{:.1e}", 865), "8.7e2"); - assert_eq!(format!("{:.1e}", 885), "8.9e2"); - assert_eq!(format!("{:.1e}", 905), "9.1e2"); - assert_eq!(format!("{:.1e}", 925), "9.3e2"); - assert_eq!(format!("{:.1e}", 945), "9.5e2"); - assert_eq!(format!("{:.1e}", 965), "9.7e2"); - assert_eq!(format!("{:.1e}", 985), "9.9e2"); - assert_eq!(format!("{:.1e}", 995), "1.0e3"); - assert_eq!(format!("{:.1e}", 996), "1.0e3"); - assert_eq!(format!("{:.1e}", 997), "1.0e3"); - assert_eq!(format!("{:.1e}", 998), "1.0e3"); - assert_eq!(format!("{:.1e}", 999), "1.0e3"); - assert_eq!(format!("{:.1e}", 1050), "1.1e3"); - assert_eq!(format!("{:.1e}", 1250), "1.3e3"); - assert_eq!(format!("{:.1e}", 1450), "1.5e3"); - assert_eq!(format!("{:.1e}", 1650), "1.7e3"); - assert_eq!(format!("{:.1e}", 1850), "1.9e3"); - assert_eq!(format!("{:.2e}", -1985), "-1.99e3"); - assert_eq!(format!("{:.2e}", -1965), "-1.97e3"); - assert_eq!(format!("{:.2e}", -1945), "-1.95e3"); - assert_eq!(format!("{:.2e}", -1925), "-1.93e3"); - assert_eq!(format!("{:.2e}", -1905), "-1.91e3"); - assert_eq!(format!("{:.2e}", -1885), "-1.89e3"); - assert_eq!(format!("{:.2e}", -1865), "-1.87e3"); - assert_eq!(format!("{:.2e}", -1845), "-1.85e3"); - assert_eq!(format!("{:.2e}", -1825), "-1.83e3"); - assert_eq!(format!("{:.2e}", -1805), "-1.81e3"); - assert_eq!(format!("{:.2e}", -1785), "-1.79e3"); - assert_eq!(format!("{:.2e}", -1765), "-1.77e3"); - assert_eq!(format!("{:.2e}", -1745), "-1.75e3"); - assert_eq!(format!("{:.2e}", -1725), "-1.73e3"); - assert_eq!(format!("{:.2e}", -1705), "-1.71e3"); - assert_eq!(format!("{:.2e}", -1685), "-1.69e3"); - assert_eq!(format!("{:.2e}", -1665), "-1.67e3"); - assert_eq!(format!("{:.2e}", -1645), "-1.65e3"); - assert_eq!(format!("{:.2e}", -1625), "-1.63e3"); - assert_eq!(format!("{:.2e}", -1605), "-1.61e3"); - assert_eq!(format!("{:.2e}", -1585), "-1.59e3"); - assert_eq!(format!("{:.2e}", -1565), "-1.57e3"); - assert_eq!(format!("{:.2e}", -1545), "-1.55e3"); - assert_eq!(format!("{:.2e}", -1525), "-1.53e3"); - assert_eq!(format!("{:.2e}", -1505), "-1.51e3"); - assert_eq!(format!("{:.2e}", -1485), "-1.49e3"); - assert_eq!(format!("{:.2e}", -1465), "-1.47e3"); - assert_eq!(format!("{:.2e}", -1445), "-1.45e3"); - assert_eq!(format!("{:.2e}", -1425), "-1.43e3"); - assert_eq!(format!("{:.2e}", -1405), "-1.41e3"); - assert_eq!(format!("{:.2e}", -1385), "-1.39e3"); - assert_eq!(format!("{:.2e}", -1365), "-1.37e3"); - assert_eq!(format!("{:.2e}", -1345), "-1.35e3"); - assert_eq!(format!("{:.2e}", -1325), "-1.33e3"); - assert_eq!(format!("{:.2e}", -1305), "-1.31e3"); - assert_eq!(format!("{:.2e}", -1285), "-1.29e3"); - assert_eq!(format!("{:.2e}", -1265), "-1.27e3"); - assert_eq!(format!("{:.2e}", -1245), "-1.25e3"); - assert_eq!(format!("{:.2e}", -1225), "-1.23e3"); - assert_eq!(format!("{:.2e}", -1205), "-1.21e3"); - assert_eq!(format!("{:.2e}", -1185), "-1.19e3"); - assert_eq!(format!("{:.2e}", -1165), "-1.17e3"); - assert_eq!(format!("{:.2e}", -1145), "-1.15e3"); - assert_eq!(format!("{:.2e}", -1125), "-1.13e3"); - assert_eq!(format!("{:.2e}", -1105), "-1.11e3"); - assert_eq!(format!("{:.2e}", -1085), "-1.09e3"); - assert_eq!(format!("{:.2e}", -1065), "-1.07e3"); - assert_eq!(format!("{:.2e}", -1045), "-1.05e3"); - assert_eq!(format!("{:.2e}", -1025), "-1.03e3"); - assert_eq!(format!("{:.2e}", -1005), "-1.01e3"); - assert_eq!(format!("{:.2e}", 1005), "1.01e3" ); - assert_eq!(format!("{:.2e}", 1025), "1.03e3" ); - assert_eq!(format!("{:.2e}", 1045), "1.05e3" ); - assert_eq!(format!("{:.2e}", 1065), "1.07e3" ); - assert_eq!(format!("{:.2e}", 1085), "1.09e3" ); - assert_eq!(format!("{:.2e}", 1105), "1.11e3" ); - assert_eq!(format!("{:.2e}", 1125), "1.13e3" ); - assert_eq!(format!("{:.2e}", 1145), "1.15e3" ); - assert_eq!(format!("{:.2e}", 1165), "1.17e3" ); - assert_eq!(format!("{:.2e}", 1185), "1.19e3" ); - assert_eq!(format!("{:.2e}", 1205), "1.21e3" ); - assert_eq!(format!("{:.2e}", 1225), "1.23e3" ); - assert_eq!(format!("{:.2e}", 1245), "1.25e3" ); - assert_eq!(format!("{:.2e}", 1265), "1.27e3" ); - assert_eq!(format!("{:.2e}", 1285), "1.29e3" ); - assert_eq!(format!("{:.2e}", 1305), "1.31e3" ); - assert_eq!(format!("{:.2e}", 1325), "1.33e3" ); - assert_eq!(format!("{:.2e}", 1345), "1.35e3" ); - assert_eq!(format!("{:.2e}", 1365), "1.37e3" ); - assert_eq!(format!("{:.2e}", 1385), "1.39e3" ); - assert_eq!(format!("{:.2e}", 1405), "1.41e3" ); - assert_eq!(format!("{:.2e}", 1425), "1.43e3" ); - assert_eq!(format!("{:.2e}", 1445), "1.45e3" ); - assert_eq!(format!("{:.2e}", 1465), "1.47e3" ); - assert_eq!(format!("{:.2e}", 1485), "1.49e3" ); - assert_eq!(format!("{:.2e}", 1505), "1.51e3" ); - assert_eq!(format!("{:.2e}", 1525), "1.53e3" ); - assert_eq!(format!("{:.2e}", 1545), "1.55e3" ); - assert_eq!(format!("{:.2e}", 1565), "1.57e3" ); - assert_eq!(format!("{:.2e}", 1585), "1.59e3" ); - assert_eq!(format!("{:.2e}", 1605), "1.61e3" ); - assert_eq!(format!("{:.2e}", 1625), "1.63e3" ); - assert_eq!(format!("{:.2e}", 1645), "1.65e3" ); - assert_eq!(format!("{:.2e}", 1665), "1.67e3" ); - assert_eq!(format!("{:.2e}", 1685), "1.69e3" ); - assert_eq!(format!("{:.2e}", 1705), "1.71e3" ); - assert_eq!(format!("{:.2e}", 1725), "1.73e3" ); - assert_eq!(format!("{:.2e}", 1745), "1.75e3" ); - assert_eq!(format!("{:.2e}", 1765), "1.77e3" ); - assert_eq!(format!("{:.2e}", 1785), "1.79e3" ); - assert_eq!(format!("{:.2e}", 1805), "1.81e3" ); - assert_eq!(format!("{:.2e}", 1825), "1.83e3" ); - assert_eq!(format!("{:.2e}", 1845), "1.85e3" ); - assert_eq!(format!("{:.2e}", 1865), "1.87e3" ); - assert_eq!(format!("{:.2e}", 1885), "1.89e3" ); - assert_eq!(format!("{:.2e}", 1905), "1.91e3" ); - assert_eq!(format!("{:.2e}", 1925), "1.93e3" ); - assert_eq!(format!("{:.2e}", 1945), "1.95e3" ); - assert_eq!(format!("{:.2e}", 1965), "1.97e3" ); - assert_eq!(format!("{:.2e}", 1985), "1.99e3" ); + + for i in i16::MIN..=i16::MAX { + for p in 0..=5 { + assert_eq!( + format!("{i:.p$e}"), + format!("{:.p$e}", f32::from(i)), + "integer {i} at precision {p}" + ); + } + } } #[test]