test more

This commit is contained in:
Quaternions 2024-08-29 13:30:48 -07:00
parent b45d93a7dc
commit ac7d9f5c3b
2 changed files with 20 additions and 8 deletions

View File

@ -12,6 +12,8 @@ impl<const CHUNKS:usize,Frac:Unsigned> Fixed<CHUNKS,Frac>{
pub const MAX:Self=Self{bits:BInt::<CHUNKS>::MAX,frac:PhantomData}; pub const MAX:Self=Self{bits:BInt::<CHUNKS>::MAX,frac:PhantomData};
pub const MIN:Self=Self{bits:BInt::<CHUNKS>::MIN,frac:PhantomData}; pub const MIN:Self=Self{bits:BInt::<CHUNKS>::MIN,frac:PhantomData};
pub const ZERO:Self=Self{bits:BInt::<CHUNKS>::ZERO,frac:PhantomData}; pub const ZERO:Self=Self{bits:BInt::<CHUNKS>::ZERO,frac:PhantomData};
pub const EPSILON:Self=Self{bits:BInt::<CHUNKS>::ONE,frac:PhantomData};
pub const NEG_EPSILON:Self=Self{bits:BInt::<CHUNKS>::NEG_ONE,frac:PhantomData};
pub const ONE:Self=Self{bits:BInt::<CHUNKS>::ONE.shl(Frac::U32),frac:PhantomData}; pub const ONE:Self=Self{bits:BInt::<CHUNKS>::ONE.shl(Frac::U32),frac:PhantomData};
pub const TWO:Self=Self{bits:BInt::<CHUNKS>::TWO.shl(Frac::U32),frac:PhantomData}; pub const TWO:Self=Self{bits:BInt::<CHUNKS>::TWO.shl(Frac::U32),frac:PhantomData};
pub const HALF:Self=Self{bits:BInt::<CHUNKS>::ONE.shl(Frac::U32-1),frac:PhantomData}; pub const HALF:Self=Self{bits:BInt::<CHUNKS>::ONE.shl(Frac::U32-1),frac:PhantomData};

View File

@ -31,16 +31,26 @@ fn find_equiv_sqrt_via_f64(n:I32F32)->I32F32{
let ibits=bits as i64; let ibits=bits as i64;
let f=(ibits as f64)/((1u64<<32) as f64); let f=(ibits as f64)/((1u64<<32) as f64);
let f_ans=f.sqrt(); let f_ans=f.sqrt();
let mut i=(f_ans*((1u64<<32) as f64)) as i64; let i=(f_ans*((1u64<<32) as f64)) as i64;
let s=(i as i128)*(i as i128); let r=I32F32::from_bits(bnum::BInt::<1>::from(i));
if s<((ibits as i128)<<32){ //mimic the behaviour of the algorithm,
i+=1; //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] #[test]
fn test_sqrt_exact(){ fn test_sqrt_exact(){
let a=I32F32::ONE*2; //43
let b=find_equiv_sqrt_via_f64(a); for i in 0..((i64::MAX as f32).ln() as u32){
assert_eq!(a.sqrt(),b); let n=I32F32::from_bits(bnum::BInt::<1>::from((i as f32).exp() as i64));
test_exact(n);
}
} }