This commit is contained in:
Trevor Spiteri 2019-01-26 03:52:41 +01:00
parent ac245367dd
commit 56b3176deb
4 changed files with 15 additions and 13 deletions

View File

@ -891,7 +891,7 @@ mod tests {
fn fixed_u128() { fn fixed_u128() {
use frac::U7 as Frac; use frac::U7 as Frac;
let frac = Frac::to_u32(); let frac = Frac::to_u32();
let a = 0x0003456789abcdef_0123456789abcdef_u128; let a = 0x0003_4567_89ab_cdef_0123_4567_89ab_cdef_u128;
let b = 5; let b = 5;
for &(a, b) in &[(a, b), (b, a)] { for &(a, b) in &[(a, b), (b, a)] {
let af = FixedU128::<Frac>::from_bits(a << frac); let af = FixedU128::<Frac>::from_bits(a << frac);
@ -913,7 +913,7 @@ mod tests {
fn fixed_i128() { fn fixed_i128() {
use frac::U7 as Frac; use frac::U7 as Frac;
let frac = Frac::to_u32(); let frac = Frac::to_u32();
let a = 0x0003456789abcdef_0123456789abcdef_i128; let a = 0x0003_4567_89ab_cdef_0123_4567_89ab_cdef_i128;
let b = 5; let b = 5;
for &(a, b) in &[ for &(a, b) in &[
(a, b), (a, b),

View File

@ -472,7 +472,7 @@ mod tests {
let frac = Frac::to_u32(); let frac = Frac::to_u32();
for i in 0..(1 << frac) { for i in 0..(1 << frac) {
let p = 0x1234_5678_9abc_def0u64 ^ i as u64; let p = 0x1234_5678_9abc_def0u64 ^ i as u64;
let n = -0x1234_5678_9abc_def0i64 ^ i as i64; let n = -0x1234_5678_9abc_def0i64 ^ i64::from(i);
let f_p = FixedU64::<Frac>::from_bits(p); let f_p = FixedU64::<Frac>::from_bits(p);
let f_n = FixedI64::<Frac>::from_bits(n); let f_n = FixedI64::<Frac>::from_bits(n);
assert_eq_fmt!(("{:x}", f_p), ("{:x}.{:02x}", p >> frac, (p & 0x7f) << 1)); assert_eq_fmt!(("{:x}", f_p), ("{:x}.{:02x}", p >> frac, (p & 0x7f) << 1));
@ -489,7 +489,7 @@ mod tests {
let frac = Frac::to_u32(); let frac = Frac::to_u32();
for i in 0..(1 << frac) { for i in 0..(1 << frac) {
let bits = !0u32 ^ i; let bits = !0u32 ^ i;
let flt = bits as f64 / (frac as f64).exp2(); let flt = f64::from(bits) / f64::from(frac).exp2();
let fix = FixedU32::<Frac>::from_bits(bits); let fix = FixedU32::<Frac>::from_bits(bits);
assert_eq_fmt!(("{}", fix), ("{:.2}", flt)); assert_eq_fmt!(("{}", fix), ("{:.2}", flt));
} }

View File

@ -206,6 +206,7 @@ lossless_from_fixed! { FixedU8(U8)::to_f64 -> f64 }
lossless_from_fixed! { FixedU16(U16)::to_f64 -> f64 } lossless_from_fixed! { FixedU16(U16)::to_f64 -> f64 }
lossless_from_fixed! { FixedU32(U32)::to_f64 -> f64 } lossless_from_fixed! { FixedU32(U32)::to_f64 -> f64 }
#[cfg_attr(feature = "cargo-clippy", allow(clippy::float_cmp))]
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use *; use *;
@ -343,10 +344,10 @@ mod tests {
fn to_f32() { fn to_f32() {
for u in 0x00..=0xff { for u in 0x00..=0xff {
let fu = FixedU8::<frac::U7>::from_bits(u); let fu = FixedU8::<frac::U7>::from_bits(u);
assert_eq!(fu.to_f32(), u as f32 / 128.0); assert_eq!(fu.to_f32(), f32::from(u) / 128.0);
let i = u as i8; let i = u as i8;
let fi = FixedI8::<frac::U7>::from_bits(i); let fi = FixedI8::<frac::U7>::from_bits(i);
assert_eq!(fi.to_f32(), i as f32 / 128.0); assert_eq!(fi.to_f32(), f32::from(i) / 128.0);
for hi in &[ for hi in &[
0u32, 0u32,
@ -357,7 +358,7 @@ mod tests {
0xffff_fe00, 0xffff_fe00,
0xffff_ff00, 0xffff_ff00,
] { ] {
let uu = *hi | u as u32; let uu = *hi | u32::from(u);
let fuu = FixedU32::<frac::U7>::from_bits(uu); let fuu = FixedU32::<frac::U7>::from_bits(uu);
assert_eq!(fuu.to_f32(), uu as f32 / 128.0); assert_eq!(fuu.to_f32(), uu as f32 / 128.0);
let ii = uu as i32; let ii = uu as i32;
@ -374,7 +375,7 @@ mod tests {
0xffff_ffff_ffff_ffff_ffff_ffff_ffff_fe00, 0xffff_ffff_ffff_ffff_ffff_ffff_ffff_fe00,
0xffff_ffff_ffff_ffff_ffff_ffff_ffff_ff00, 0xffff_ffff_ffff_ffff_ffff_ffff_ffff_ff00,
] { ] {
let uu = *hi | u as u128; let uu = *hi | u128::from(u);
let fuu = FixedU128::<frac::U7>::from_bits(uu); let fuu = FixedU128::<frac::U7>::from_bits(uu);
assert_eq!(fuu.to_f32(), (uu as f64 / 128.0) as f32); assert_eq!(fuu.to_f32(), (uu as f64 / 128.0) as f32);
let ii = uu as i128; let ii = uu as i128;
@ -409,17 +410,17 @@ mod tests {
// min_128 is -1.0 << 127. // min_128 is -1.0 << 127.
let min_i128 = FixedI128::<frac::U0>::min_value(); let min_i128 = FixedI128::<frac::U0>::min_value();
assert_eq!(min_i128.count_ones(), 1); assert_eq!(min_i128.count_ones(), 1);
assert_eq!(min_i128.to_f32(), -127f32.exp2()); assert_eq!(min_i128.to_f32(), -(127f32.exp2()));
} }
#[test] #[test]
fn to_f64() { fn to_f64() {
for u in 0x00..=0xff { for u in 0x00..=0xff {
let fu = FixedU8::<frac::U7>::from_bits(u); let fu = FixedU8::<frac::U7>::from_bits(u);
assert_eq!(fu.to_f32(), u as f32 / 128.0); assert_eq!(fu.to_f32(), f32::from(u) / 128.0);
let i = u as i8; let i = u as i8;
let fi = FixedI8::<frac::U7>::from_bits(i); let fi = FixedI8::<frac::U7>::from_bits(i);
assert_eq!(fi.to_f32(), i as f32 / 128.0); assert_eq!(fi.to_f32(), f32::from(i) / 128.0);
for hi in &[ for hi in &[
0u64, 0u64,
@ -430,7 +431,7 @@ mod tests {
0xffff_ffff_ffff_fe00, 0xffff_ffff_ffff_fe00,
0xffff_ffff_ffff_ff00, 0xffff_ffff_ffff_ff00,
] { ] {
let uu = *hi | u as u64; let uu = *hi | u64::from(u);
let fuu = FixedU64::<frac::U7>::from_bits(uu); let fuu = FixedU64::<frac::U7>::from_bits(uu);
assert_eq!(fuu.to_f64(), uu as f64 / 128.0); assert_eq!(fuu.to_f64(), uu as f64 / 128.0);
let ii = uu as i64; let ii = uu as i64;
@ -447,7 +448,7 @@ mod tests {
0xffff_ffff_ffff_ffff_ffff_ffff_ffff_fe00, 0xffff_ffff_ffff_ffff_ffff_ffff_ffff_fe00,
0xffff_ffff_ffff_ffff_ffff_ffff_ffff_ff00, 0xffff_ffff_ffff_ffff_ffff_ffff_ffff_ff00,
] { ] {
let uu = *hi | u as u128; let uu = *hi | u128::from(u);
let fuu = FixedU128::<frac::U7>::from_bits(uu); let fuu = FixedU128::<frac::U7>::from_bits(uu);
assert_eq!(fuu.to_f64(), uu as f64 / 128.0); assert_eq!(fuu.to_f64(), uu as f64 / 128.0);
let ii = uu as i128; let ii = uu as i128;

View File

@ -1684,6 +1684,7 @@ fixed! { "A 128-bit fixed-point signed integer", FixedI128(i128, U128, 128), Sig
mod tests { mod tests {
use *; use *;
#[cfg_attr(feature = "cargo-clippy", allow(clippy::cyclomatic_complexity))]
#[test] #[test]
fn rounding() { fn rounding() {
use frac::{U16, U32}; use frac::{U16, U32};