test more

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

View File

@@ -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);
}
}