1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#![cfg(feature = "power-of-two")]
use lexical_parse_float::binary::{binary, slow_binary};
use lexical_parse_float::number::Number;
use lexical_util::format::NumberFormatBuilder;
const BINARY: u128 = NumberFormatBuilder::from_radix(2);
const BASE4: u128 = NumberFormatBuilder::from_radix(4);
const OCTAL: u128 = NumberFormatBuilder::from_radix(8);
const HEX: u128 = NumberFormatBuilder::from_radix(16);
const BASE32: u128 = NumberFormatBuilder::from_radix(32);
fn compute_float32<const FORMAT: u128>(q: i64, w: u64, many_digits: bool) -> (i32, u64) {
let num = Number {
exponent: q,
mantissa: w,
is_negative: false,
many_digits,
integer: &[],
fraction: None,
};
let fp = binary::<f32, FORMAT>(&num, false);
(fp.exp, fp.mant)
}
fn compute_float64<const FORMAT: u128>(q: i64, w: u64, many_digits: bool) -> (i32, u64) {
let num = Number {
exponent: q,
mantissa: w,
is_negative: false,
many_digits,
integer: &[],
fraction: None,
};
let fp = binary::<f64, FORMAT>(&num, false);
(fp.exp, fp.mant)
}
#[test]
fn computef32_test() {
// Halfway, round-down tests
assert_eq!(compute_float32::<BINARY>(0, 16777216, false), (151, 0));
assert_eq!(compute_float32::<BINARY>(0, 16777217, false), (151, 0));
assert_eq!(compute_float32::<BINARY>(0, 16777218, false), (151, 1));
assert_eq!(compute_float32::<BINARY>(0, 33554432, false), (152, 0));
assert_eq!(compute_float32::<BINARY>(0, 33554434, false), (152, 0));
assert_eq!(compute_float32::<BINARY>(0, 33554436, false), (152, 1));
}
#[test]
fn halfway_round_down_test() {
// Halfway, round-down tests
assert_eq!(compute_float64::<BINARY>(0, 9007199254740992, false), (1076, 0));
assert_eq!(compute_float64::<BINARY>(0, 9007199254740993, false), (1076, 0));
assert_eq!(compute_float64::<BINARY>(0, 9007199254740994, false), (1076, 1));
assert_eq!(compute_float64::<BINARY>(0, 18014398509481984, false), (1077, 0));
assert_eq!(compute_float64::<BINARY>(0, 18014398509481986, false), (1077, 0));
assert_eq!(compute_float64::<BINARY>(0, 18014398509481988, false), (1077, 1));
assert_eq!(compute_float64::<BINARY>(0, 9223372036854775808, false), (1086, 0));
assert_eq!(compute_float64::<BINARY>(0, 9223372036854776832, false), (1086, 0));
assert_eq!(compute_float64::<BINARY>(0, 9223372036854777856, false), (1086, 1));
// Add a 0 but say we're truncated.
assert_eq!(compute_float64::<BINARY>(-10, 9223372036854775808, true), (1076, 0));
assert_eq!(
compute_float64::<BINARY>(-10, 9223372036854776832, true),
(-31703, 9223372036854776832)
);
assert_eq!(compute_float64::<BINARY>(-10, 9223372036854777856, true), (1076, 1));
// Check other bases.
assert_eq!(compute_float64::<BASE4>(-2, 144115188075855872, false), (1076, 0));
assert_eq!(compute_float64::<BASE4>(-2, 144115188075855888, false), (1076, 0));
assert_eq!(compute_float64::<BASE4>(-2, 144115188075855904, false), (1076, 1));
assert_eq!(compute_float64::<OCTAL>(-2, 576460752303423488, false), (1076, 0));
assert_eq!(compute_float64::<OCTAL>(-2, 576460752303423552, false), (1076, 0));
assert_eq!(compute_float64::<OCTAL>(-2, 576460752303423616, false), (1076, 1));
assert_eq!(compute_float64::<HEX>(-1, 144115188075855872, false), (1076, 0));
assert_eq!(compute_float64::<HEX>(-1, 144115188075855888, false), (1076, 0));
assert_eq!(compute_float64::<HEX>(-1, 144115188075855904, false), (1076, 1));
assert_eq!(compute_float64::<BASE32>(-1, 288230376151711744, false), (1076, 0));
assert_eq!(compute_float64::<BASE32>(-1, 288230376151711776, false), (1076, 0));
assert_eq!(compute_float64::<BASE32>(-1, 288230376151711808, false), (1076, 1));
}
#[test]
fn test_halfway_round_up() {
// Halfway, round-up tests
assert_eq!(compute_float64::<BINARY>(0, 9007199254740994, false), (1076, 1));
assert_eq!(compute_float64::<BINARY>(0, 9007199254740995, false), (1076, 2));
assert_eq!(compute_float64::<BINARY>(0, 9007199254740996, false), (1076, 2));
assert_eq!(compute_float64::<BINARY>(0, 18014398509481988, false), (1077, 1));
assert_eq!(compute_float64::<BINARY>(0, 18014398509481990, false), (1077, 2));
assert_eq!(compute_float64::<BINARY>(0, 18014398509481992, false), (1077, 2));
assert_eq!(compute_float64::<BINARY>(0, 9223372036854777856, false), (1086, 1));
assert_eq!(compute_float64::<BINARY>(0, 9223372036854778880, false), (1086, 2));
assert_eq!(compute_float64::<BINARY>(0, 9223372036854779904, false), (1086, 2));
// Add a 0 but say we're truncated.
assert_eq!(compute_float64::<BINARY>(-10, 9223372036854777856, true), (1076, 1));
assert_eq!(compute_float64::<BINARY>(-10, 9223372036854778879, true), (1076, 1));
assert_eq!(compute_float64::<BINARY>(-10, 9223372036854778880, true), (1076, 2));
assert_eq!(compute_float64::<BINARY>(-10, 9223372036854779904, true), (1076, 2));
// Check other bases.
assert_eq!(compute_float64::<BASE4>(-2, 144115188075855904, false), (1076, 1));
assert_eq!(compute_float64::<BASE4>(-2, 144115188075855920, false), (1076, 2));
assert_eq!(compute_float64::<BASE4>(-2, 144115188075855936, false), (1076, 2));
assert_eq!(compute_float64::<OCTAL>(-2, 576460752303423616, false), (1076, 1));
assert_eq!(compute_float64::<OCTAL>(-2, 576460752303423680, false), (1076, 2));
assert_eq!(compute_float64::<OCTAL>(-2, 576460752303423744, false), (1076, 2));
assert_eq!(compute_float64::<HEX>(-1, 144115188075855904, false), (1076, 1));
assert_eq!(compute_float64::<HEX>(-1, 144115188075855920, false), (1076, 2));
assert_eq!(compute_float64::<HEX>(-1, 144115188075855936, false), (1076, 2));
assert_eq!(compute_float64::<BASE32>(-1, 288230376151711808, false), (1076, 1));
assert_eq!(compute_float64::<BASE32>(-1, 288230376151711840, false), (1076, 2));
assert_eq!(compute_float64::<BASE32>(-1, 288230376151711872, false), (1076, 2));
}
fn compute_float64_slow<const FORMAT: u128>(
integer: &[u8],
fraction: Option<&[u8]>,
exponent: i64,
) -> (i32, u64) {
let num = Number {
exponent,
mantissa: 0,
is_negative: false,
many_digits: false,
integer,
fraction,
};
let fp = slow_binary::<f64, FORMAT>(num);
(fp.exp, fp.mant)
}
#[test]
fn test_slow() {
let i = b"100000000000000000000000000000000000000000000000000001";
let f = b"0000000000000";
assert_eq!(compute_float64_slow::<BINARY>(i, Some(f), -10), (1076, 0));
let i = b"100000000000000000000000000000000000000000000000000001";
let f = b"000000000000000000001";
assert_eq!(compute_float64_slow::<BINARY>(i, Some(f), -10), (1076, 1));
let i = b"100000000000000000000000000000000000000000000000000001";
let f = b"000000000000010000000";
assert_eq!(compute_float64_slow::<BINARY>(i, Some(f), -10), (1076, 1));
}
|