diff --git a/fixed_wide/src/fixed.rs b/fixed_wide/src/fixed.rs index fd49ace..1c1ca49 100644 --- a/fixed_wide/src/fixed.rs +++ b/fixed_wide/src/fixed.rs @@ -9,11 +9,15 @@ pub struct Fixed{ } impl Fixed{ - pub const ZERO:Self=Self{bits:BInt::::ZERO,frac:PhantomData}; - pub const ONE:Self=Self{bits:BInt::::ONE.shl(Frac::U32),frac:PhantomData}; - pub const NEG_ONE:Self=Self{bits:BInt::::NEG_ONE.shl(Frac::U32),frac:PhantomData}; 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 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}; + pub const NEG_ONE:Self=Self{bits:BInt::::NEG_ONE.shl(Frac::U32),frac:PhantomData}; + pub const NEG_TWO:Self=Self{bits:BInt::::NEG_TWO.shl(Frac::U32),frac:PhantomData}; + pub const NEG_HALF:Self=Self{bits:BInt::::NEG_ONE.shl(Frac::U32-1),frac:PhantomData}; } impl From for Fixed @@ -148,3 +152,50 @@ impl_shift_assign_operator!( Fixed, ShlAssign, shl_assign ); impl_shift_operator!( Fixed, Shl, shl, Self ); impl_shift_assign_operator!( Fixed, ShrAssign, shr_assign ); impl_shift_operator!( Fixed, Shr, shr, Self ); + +impl Fixed{ + pub fn sqrt_unchecked(self)->Self{ + let mut pow2=if self==Self::ZERO{ + return Self::ZERO; + }else if self>=1; + } + pow2 + }else if self==Self::ONE{ + return Self::ONE; + }else{ + //find pow2 more powerful than self + let mut pow2=Self::ONE; + while pow2<=self{ + pow2<<=1; + } + pow2 + }; + let mut result=pow2; + while pow2!=Self::ZERO{ + pow2>>=1; + let new_result=result+pow2; + if new_result*new_result<=self{ + result=new_result; + } + } + result + } + pub fn sqrt(self)->Self{ + if selfOption{ + if self