allow partial underflow

This commit is contained in:
Quaternions 2023-10-13 19:10:59 -07:00
parent 5f1d732b59
commit c88451c0f0

View File

@ -439,11 +439,19 @@ pub enum Planar64TryFromFloatError{
} }
#[inline] #[inline]
fn planar64_from_mes((m,e,s):(u64,i16,i8))->Result<Planar64,Planar64TryFromFloatError>{ fn planar64_from_mes((m,e,s):(u64,i16,i8))->Result<Planar64,Planar64TryFromFloatError>{
if e< -32{ let e32=e+32;
Err(Planar64TryFromFloatError::HighlyNegativeExponent(e)) if e32<0&&(m>>-e32)==0{//shifting m will underflow to 0
}else if e<32-52{ Ok(Planar64::ZERO)
Ok(Planar64((m as i64)*(s as i64)<<e)) // println!("m{} e{} s{}",m,e,s);
}else{ // println!("f={}",(m as f64)*(2.0f64.powf(e as f64))*(s as f64));
// Err(Planar64TryFromFloatError::HighlyNegativeExponent(e))
}else if (64-m.leading_zeros() as i16)+e32<64{//shifting m will not overflow
if e32<0{
Ok(Planar64((m as i64)*(s as i64)>>-e32))
}else{
Ok(Planar64((m as i64)*(s as i64)<<e32))
}
}else{//if shifting m will overflow (prev check failed)
Err(Planar64TryFromFloatError::HighlyPositiveExponent(e)) Err(Planar64TryFromFloatError::HighlyPositiveExponent(e))
} }
} }