@@ -105,11 +105,17 @@ fn check_exact<F, T>(mut f: F, v: T, vstr: &str, expected: &[u8], expectedk: i16
105
105
bytes:: copy_memory ( & expected[ ..i] , & mut expected_) ;
106
106
let mut expectedk_ = expectedk;
107
107
if expected[ i] >= b'5' {
108
- // if this returns true, expected_[..i] is all `9`s and being rounded up.
109
- // we should always return `100..00` (`i` digits) instead, since that's
110
- // what we can came up with `i` digits anyway. `round_up` assumes that
111
- // the adjustment to the length is done by caller, which we simply ignore.
112
- if let Some ( _) = round_up ( & mut expected_, i) { expectedk_ += 1 ; }
108
+ // check if this is a rounding-to-even case.
109
+ // we avoid rounding ...x5000... (with infinite zeroes) to ...(x+1) when x is even.
110
+ if !( i+1 < expected. len ( ) && expected[ i-1 ] & 1 == 0 &&
111
+ expected[ i] == b'5' &&
112
+ expected[ i+1 ] == b' ' ) {
113
+ // if this returns true, expected_[..i] is all `9`s and being rounded up.
114
+ // we should always return `100..00` (`i` digits) instead, since that's
115
+ // what we can came up with `i` digits anyway. `round_up` assumes that
116
+ // the adjustment to the length is done by caller, which we simply ignore.
117
+ if let Some ( _) = round_up ( & mut expected_, i) { expectedk_ += 1 ; }
118
+ }
113
119
}
114
120
115
121
try_exact ! ( f( & decoded) => & mut buf, & expected_[ ..i] , expectedk_;
@@ -243,6 +249,7 @@ pub fn f32_exact_sanity_test<F>(mut f: F)
243
249
let minf32 = f32:: ldexp ( 1.0 , -149 ) ;
244
250
245
251
check_exact ! ( f( 0.1f32 ) => b"100000001490116119384765625 " , 0 ) ;
252
+ check_exact ! ( f( 0.5f32 ) => b"5 " , 0 ) ;
246
253
check_exact ! ( f( 1.0f32 /3.0 ) => b"3333333432674407958984375 " , 0 ) ;
247
254
check_exact ! ( f( 3.141592f32 ) => b"31415920257568359375 " , 1 ) ;
248
255
check_exact ! ( f( 3.141592e17f32 ) => b"314159196796878848 " , 18 ) ;
@@ -348,6 +355,7 @@ pub fn f64_exact_sanity_test<F>(mut f: F)
348
355
349
356
check_exact ! ( f( 0.1f64 ) => b"1000000000000000055511151231257827021181" , 0 ) ;
350
357
check_exact ! ( f( 0.45f64 ) => b"4500000000000000111022302462515654042363" , 0 ) ;
358
+ check_exact ! ( f( 0.5f64 ) => b"5 " , 0 ) ;
351
359
check_exact ! ( f( 0.95f64 ) => b"9499999999999999555910790149937383830547" , 0 ) ;
352
360
check_exact ! ( f( 100.0f64 ) => b"1 " , 3 ) ;
353
361
check_exact ! ( f( 999.5f64 ) => b"9995000000000000000000000000000000000000" , 3 ) ;
@@ -1032,6 +1040,11 @@ pub fn to_exact_fixed_str_test<F>(mut f_: F)
1032
1040
assert_eq ! ( to_string( f, 999.5 , Minus , 3 , false ) , "999.500" ) ;
1033
1041
assert_eq ! ( to_string( f, 999.5 , Minus , 30 , false ) , "999.500000000000000000000000000000" ) ;
1034
1042
1043
+ assert_eq ! ( to_string( f, 0.5 , Minus , 0 , false ) , "1" ) ;
1044
+ assert_eq ! ( to_string( f, 0.5 , Minus , 1 , false ) , "0.5" ) ;
1045
+ assert_eq ! ( to_string( f, 0.5 , Minus , 2 , false ) , "0.50" ) ;
1046
+ assert_eq ! ( to_string( f, 0.5 , Minus , 3 , false ) , "0.500" ) ;
1047
+
1035
1048
assert_eq ! ( to_string( f, 0.95 , Minus , 0 , false ) , "1" ) ;
1036
1049
assert_eq ! ( to_string( f, 0.95 , Minus , 1 , false ) , "0.9" ) ; // because it really is less than 0.95
1037
1050
assert_eq ! ( to_string( f, 0.95 , Minus , 2 , false ) , "0.95" ) ;
0 commit comments