| 1 |
use minimal_lexical::extended_float::ExtendedFloat; |
| 2 |
use minimal_lexical::rounding; |
| 3 |
|
| 4 |
#[test] |
| 5 |
fn round_test() { |
| 6 |
let mut fp = ExtendedFloat { |
| 7 |
mant: 9223372036854776832, |
| 8 |
exp: -10, |
| 9 |
}; |
| 10 |
rounding::round::<f64, _>(&mut fp, |f, s| { |
| 11 |
f.mant >>= s; |
| 12 |
f.exp += s; |
| 13 |
}); |
| 14 |
assert_eq!(fp.mant, 0); |
| 15 |
assert_eq!(fp.exp, 1); |
| 16 |
|
| 17 |
let mut fp = ExtendedFloat { |
| 18 |
mant: 9223372036854776832, |
| 19 |
exp: -10, |
| 20 |
}; |
| 21 |
rounding::round::<f64, _>(&mut fp, |f, s| { |
| 22 |
f.mant >>= s; |
| 23 |
f.exp += s; |
| 24 |
// Round-up. |
| 25 |
f.mant += 1; |
| 26 |
}); |
| 27 |
assert_eq!(fp.mant, 1); |
| 28 |
assert_eq!(fp.exp, 1); |
| 29 |
|
| 30 |
// Round-down |
| 31 |
let mut fp = ExtendedFloat { |
| 32 |
mant: 9223372036854776832, |
| 33 |
exp: -10, |
| 34 |
}; |
| 35 |
rounding::round::<f64, _>(&mut fp, |f, s| { |
| 36 |
rounding::round_nearest_tie_even(f, s, |is_odd, is_halfway, is_above| { |
| 37 |
is_above || (is_odd && is_halfway) |
| 38 |
}); |
| 39 |
}); |
| 40 |
assert_eq!(fp.mant, 0); |
| 41 |
assert_eq!(fp.exp, 1); |
| 42 |
|
| 43 |
// Round up |
| 44 |
let mut fp = ExtendedFloat { |
| 45 |
mant: 9223372036854778880, |
| 46 |
exp: -10, |
| 47 |
}; |
| 48 |
rounding::round::<f64, _>(&mut fp, |f, s| { |
| 49 |
rounding::round_nearest_tie_even(f, s, |is_odd, is_halfway, is_above| { |
| 50 |
is_above || (is_odd && is_halfway) |
| 51 |
}); |
| 52 |
}); |
| 53 |
assert_eq!(fp.mant, 2); |
| 54 |
assert_eq!(fp.exp, 1); |
| 55 |
|
| 56 |
// Round down |
| 57 |
let mut fp = ExtendedFloat { |
| 58 |
mant: 9223372036854778880, |
| 59 |
exp: -10, |
| 60 |
}; |
| 61 |
rounding::round::<f64, _>(&mut fp, rounding::round_down); |
| 62 |
assert_eq!(fp.mant, 1); |
| 63 |
assert_eq!(fp.exp, 1); |
| 64 |
} |