Skip to content

Commit

Permalink
Cut down number of digits printed without exponential notation
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Aug 7, 2018
1 parent 8b923a2 commit 8ee4570
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/pretty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub unsafe fn d2s_buffered_n(f: f64, result: *mut u8) -> usize {
let kk = length + k; // 10^(kk-1) <= v < 10^kk
debug_assert!(k >= -324);

if 0 <= k && kk <= 21 {
if 0 <= k && kk <= 16 {
// 1234e7 -> 12340000000.0
write_mantissa_long(v.mantissa, result.offset(index + length));
for i in length..kk {
Expand All @@ -49,7 +49,7 @@ pub unsafe fn d2s_buffered_n(f: f64, result: *mut u8) -> usize {
*result.offset(index + kk) = b'.';
*result.offset(index + kk + 1) = b'0';
index as usize + kk as usize + 2
} else if 0 < kk && kk <= 21 {
} else if 0 < kk && kk <= 16 {
// 1234e-2 -> 12.34
write_mantissa_long(v.mantissa, result.offset(index + length + 1));
ptr::copy(result.offset(index + 1), result.offset(index), kk as usize);
Expand Down Expand Up @@ -110,7 +110,7 @@ pub unsafe fn f2s_buffered_n(f: f32, result: *mut u8) -> usize {
let kk = length + k; // 10^(kk-1) <= v < 10^kk
debug_assert!(k >= -45);

if 0 <= k && kk <= 21 {
if 0 <= k && kk <= 13 {
// 1234e7 -> 12340000000.0
write_mantissa(v.mantissa, result.offset(index + length));
for i in length..kk {
Expand All @@ -119,7 +119,7 @@ pub unsafe fn f2s_buffered_n(f: f32, result: *mut u8) -> usize {
*result.offset(index + kk) = b'.';
*result.offset(index + kk + 1) = b'0';
index as usize + kk as usize + 2
} else if 0 < kk && kk <= 21 {
} else if 0 < kk && kk <= 13 {
// 1234e-2 -> 12.34
write_mantissa(v.mantissa, result.offset(index + length + 1));
ptr::copy(result.offset(index + 1), result.offset(index), kk as usize);
Expand Down
10 changes: 5 additions & 5 deletions tests/d2s_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ fn pretty(f: f64) -> String {
#[test]
fn test_ryu() {
check!(3E-1, 0.3);
check!(1.234E20, 123400000000000000000.0);
check!(1.234E21, 1.234e21);
check!(1.234E15, 1234000000000000.0);
check!(1.234E16, 1.234e16);
check!(2.71828E0, 2.71828);
check!(1.1E128, 1.1e128);
check!(1.1E-64, 1.1e-64);
Expand Down Expand Up @@ -102,13 +102,13 @@ fn test_lots_of_trailing_zeros() {

#[test]
fn test_regression() {
check!(-2.109808898695963E16, -21098088986959630.0);
check!(-2.109808898695963E16, -2.109808898695963e16);
check!(4.940656E-318, 4.940656e-318);
check!(1.18575755E-316, 1.18575755e-316);
check!(2.989102097996E-312, 2.989102097996e-312);
check!(9.0608011534336E15, 9060801153433600.0);
check!(4.708356024711512E18, 4708356024711512000.0);
check!(9.409340012568248E18, 9409340012568248000.0);
check!(4.708356024711512E18, 4.708356024711512e18);
check!(9.409340012568248E18, 9.409340012568248e18);
check!(1.2345678E0, 1.2345678);
}

Expand Down
27 changes: 14 additions & 13 deletions tests/f2s_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ fn pretty(f: f32) -> String {

#[test]
fn test_ryu() {
check!(1.234E20, 123400000000000000000.0);
check!(1.234E21, 1.234e21);
check!(3E-1, 0.3);
check!(1.234E12, 1234000000000.0);
check!(1.234E13, 1.234e13);
check!(2.71828E0, 2.71828);
check!(1.1E32, 1.1e32);
check!(1.1E-32, 1.1e-32);
Expand Down Expand Up @@ -136,20 +137,20 @@ fn test_regression() {
check!(5.3399997E9, 5339999700.0);
check!(6.0898E-39, 6.0898e-39);
check!(1.0310042E-3, 0.0010310042);
check!(2.882326E17, 288232600000000000.0);
check!(2.882326E17, 2.882326e17);
check!(7.038531E-26, 7.038531e-26);
check!(9.223404E17, 922340400000000000.0);
check!(9.223404E17, 9.223404e17);
check!(6.710887E7, 67108870.0);
check!(1E-44, 1e-44);
check!(2.816025E14, 281602500000000.0);
check!(9.223372E18, 9223372000000000000.0);
check!(2.816025E14, 2.816025e14);
check!(9.223372E18, 9.223372e18);
check!(1.5846086E29, 1.5846086e29);
check!(1.1811161E19, 11811161000000000000.0);
check!(5.368709E18, 5368709000000000000.0);
check!(4.6143166E18, 4614316600000000000.0);
check!(1.1811161E19, 1.1811161e19);
check!(5.368709E18, 5.368709e18);
check!(4.6143166E18, 4.6143166e18);
check!(7.812537E-3, 0.007812537);
check!(1E-45, 1e-45);
check!(1.18697725E20, 118697725000000000000.0);
check!(1.18697725E20, 1.18697725e20);
check!(1.00014165E-36, 1.00014165e-36);
check!(2E2, 200.0);
check!(3.3554432E7, 33554432.0);
Expand All @@ -161,11 +162,11 @@ fn test_looks_like_pow5() {
// and an exponent that causes the computation for q to result in 10, which
// is a corner case for Ryu.
assert_eq!(f32::from_bits(0x5D1502F9), 6.7108864e17);
check!(6.7108864E17, 671088640000000000.0);
check!(6.7108864E17, 6.7108864e17);
assert_eq!(f32::from_bits(0x5D9502F9), 1.3421773e18);
check!(1.3421773E18, 1342177300000000000.0);
check!(1.3421773E18, 1.3421773e18);
assert_eq!(f32::from_bits(0x5E1502F9), 2.6843546e18);
check!(2.6843546E18, 2684354600000000000.0);
check!(2.6843546E18, 2.6843546e18);
}

#[test]
Expand Down

0 comments on commit 8ee4570

Please sign in to comment.