improve failure mode

This commit is contained in:
Quaternions 2024-08-29 10:42:50 -07:00
parent 69da2c52a4
commit 491de52f17

View File

@ -155,24 +155,23 @@ impl_shift_operator!( Fixed, Shr, shr, Self );
impl<const CHUNKS:usize,Frac:Unsigned> Fixed<CHUNKS,Frac>{ impl<const CHUNKS:usize,Frac:Unsigned> Fixed<CHUNKS,Frac>{
pub fn sqrt_unchecked(self)->Self{ pub fn sqrt_unchecked(self)->Self{
let mut pow2=if self==Self::ZERO{
return Self::ZERO;
}else if self<Self::ONE{
//find pow2 more powerful than self //find pow2 more powerful than self
let mut pow2=if Self::ONE<self{
let mut pow2=Self::TWO;//1<self is so bake one iteration
while pow2<=self{
pow2<<=1;
}
pow2
}else if Self::ONE==self{
return Self::ONE;
}else if Self::ZERO<self{
let mut pow2=Self::ONE; let mut pow2=Self::ONE;
while self<=pow2{ while self<=pow2{
pow2>>=1; pow2>>=1;
} }
pow2 pow2
}else if self==Self::ONE{ }else{//either 0==self or self is negative
return Self::ONE; return Self::ZERO;
}else{
//find pow2 more powerful than self
let mut pow2=Self::ONE;
while pow2<=self{
pow2<<=1;
}
pow2
}; };
let mut result=pow2; let mut result=pow2;
while pow2!=Self::ZERO{ while pow2!=Self::ZERO{