From 8ba76c7a00467d7fbc22e0e0d34204a94cd4469f Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 29 Aug 2024 12:12:14 -0700 Subject: [PATCH] smarter sqrt --- fixed_wide/src/fixed.rs | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/fixed_wide/src/fixed.rs b/fixed_wide/src/fixed.rs index 83125b3..0030361 100644 --- a/fixed_wide/src/fixed.rs +++ b/fixed_wide/src/fixed.rs @@ -289,25 +289,17 @@ impl Fixed Fixed:::std::ops::Mul,Output=Fixed>, { pub fn sqrt_unchecked(self)->Self{ - //find pow2 more powerful than self - let mut pow2=if Self::ONE>=1; - } - pow2 - }else{//either 0==self or self is negative - return Self::ZERO; + //pow2 must be the minimum power of two which when squared is greater than self + //0001.0000 Fixed + //sqrt + //0110.0000 + //pow2 = 0100.0000 + let mut pow2=Self{ + bits:BInt::::ONE.shl((((CHUNKS as i32*64-Frac::I32-(self.bits.leading_zeros() as i32)+1)>>1)+Frac::I32) as u32), + frac:PhantomData, }; - let mut result=pow2; + let mut result=pow2>>1; + while pow2!=Self::ZERO{ pow2>>=1; let new_result=result+pow2;