From ac7d9f5c3b04d938fb25a750497b3e9d32541734 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 29 Aug 2024 13:30:48 -0700 Subject: [PATCH] test more --- fixed_wide/src/fixed.rs | 2 ++ fixed_wide/src/tests.rs | 26 ++++++++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/fixed_wide/src/fixed.rs b/fixed_wide/src/fixed.rs index 4fa36fa..c39e31d 100644 --- a/fixed_wide/src/fixed.rs +++ b/fixed_wide/src/fixed.rs @@ -12,6 +12,8 @@ impl Fixed{ pub const MAX:Self=Self{bits:BInt::::MAX,frac:PhantomData}; pub const MIN:Self=Self{bits:BInt::::MIN,frac:PhantomData}; pub const ZERO:Self=Self{bits:BInt::::ZERO,frac:PhantomData}; + pub const EPSILON:Self=Self{bits:BInt::::ONE,frac:PhantomData}; + pub const NEG_EPSILON:Self=Self{bits:BInt::::NEG_ONE,frac:PhantomData}; pub const ONE:Self=Self{bits:BInt::::ONE.shl(Frac::U32),frac:PhantomData}; pub const TWO:Self=Self{bits:BInt::::TWO.shl(Frac::U32),frac:PhantomData}; pub const HALF:Self=Self{bits:BInt::::ONE.shl(Frac::U32-1),frac:PhantomData}; diff --git a/fixed_wide/src/tests.rs b/fixed_wide/src/tests.rs index 05c911a..a46ef87 100644 --- a/fixed_wide/src/tests.rs +++ b/fixed_wide/src/tests.rs @@ -31,16 +31,26 @@ fn find_equiv_sqrt_via_f64(n:I32F32)->I32F32{ let ibits=bits as i64; let f=(ibits as f64)/((1u64<<32) as f64); let f_ans=f.sqrt(); - let mut i=(f_ans*((1u64<<32) as f64)) as i64; - let s=(i as i128)*(i as i128); - if s<((ibits as i128)<<32){ - i+=1; + let i=(f_ans*((1u64<<32) as f64)) as i64; + let r=I32F32::from_bits(bnum::BInt::<1>::from(i)); + //mimic the behaviour of the algorithm, + //return the result if it truncates to the exact answer + if (r+I32F32::EPSILON)*(r+I32F32::EPSILON)==n{ + return r+I32F32::EPSILON; } - I32F32::from_bits(bnum::BInt::<1>::from(i)) + if (r-I32F32::EPSILON)*(r-I32F32::EPSILON)==n{ + return r-I32F32::EPSILON; + } + return r; +} +fn test_exact(n:I32F32){ + assert_eq!(n.sqrt(),find_equiv_sqrt_via_f64(n)); } #[test] fn test_sqrt_exact(){ - let a=I32F32::ONE*2; - let b=find_equiv_sqrt_via_f64(a); - assert_eq!(a.sqrt(),b); + //43 + for i in 0..((i64::MAX as f32).ln() as u32){ + let n=I32F32::from_bits(bnum::BInt::<1>::from((i as f32).exp() as i64)); + test_exact(n); + } }