From 8ee457029ec8494fb417f2ea0ea9f3a7c17cca30 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 7 Aug 2018 11:09:48 -0700 Subject: [PATCH] Cut down number of digits printed without exponential notation --- src/pretty/mod.rs | 8 ++++---- tests/d2s_test.rs | 10 +++++----- tests/f2s_test.rs | 27 ++++++++++++++------------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/pretty/mod.rs b/src/pretty/mod.rs index 5361b0d..cf1dfa5 100644 --- a/src/pretty/mod.rs +++ b/src/pretty/mod.rs @@ -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 { @@ -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); @@ -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 { @@ -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); diff --git a/tests/d2s_test.rs b/tests/d2s_test.rs index b566b14..4586c2a 100644 --- a/tests/d2s_test.rs +++ b/tests/d2s_test.rs @@ -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); @@ -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); } diff --git a/tests/f2s_test.rs b/tests/f2s_test.rs index 62b6c55..24cb814 100644 --- a/tests/f2s_test.rs +++ b/tests/f2s_test.rs @@ -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); @@ -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); @@ -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]