diff --git a/fixed_wide/Cargo.toml b/fixed_wide/Cargo.toml index ab20a56..f24c634 100644 --- a/fixed_wide/Cargo.toml +++ b/fixed_wide/Cargo.toml @@ -4,10 +4,10 @@ version = "0.1.0" edition = "2021" [features] -default=["zeroes","wide-mul"] +default=["ratio","zeroes","wide-mul"] ratio=[] wide-mul=[] -zeroes=["ratio","dep:arrayvec"] +zeroes=["dep:arrayvec"] [dependencies] bnum = "0.11.0" diff --git a/fixed_wide/src/fixed.rs b/fixed_wide/src/fixed.rs index df6b957..2e75e08 100644 --- a/fixed_wide/src/fixed.rs +++ b/fixed_wide/src/fixed.rs @@ -408,11 +408,12 @@ macro_rules! impl_not_const_generic{ let mut result=Self::ZERO; //multiply by one to make the types match (hack) - let wide_self=self.[](Self::ONE); + //TODO: use resize method + let wide_self:::Output=self*Self::ONE; //descend down the bits and check if flipping each bit would push the square over the input value for shift in (0..=max_shift).rev(){ let new_result=result|Self::from_bits(BInt::from_bits(bnum::BUint::power_of_two(shift))); - if new_result.[](new_result)<=wide_self{ + if new_result*new_result<=wide_self{ result=new_result; } } diff --git a/fixed_wide/src/zeroes.rs b/fixed_wide/src/zeroes.rs index 5966615..3634a02 100644 --- a/fixed_wide/src/zeroes.rs +++ b/fixed_wide/src/zeroes.rs @@ -1,5 +1,4 @@ use crate::fixed::Fixed; -use crate::ratio::Ratio; use arrayvec::ArrayVec; use std::cmp::Ordering; @@ -7,36 +6,38 @@ macro_rules! impl_zeroes{ ($n:expr)=>{ impl Fixed<$n,{$n*32}>{ #[inline] - pub fn zeroes2(a0:Self,a1:Self,a2:Self)->ArrayVec,2>{ + pub fn zeroes2(a0:Self,a1:Self,a2:Self)->ArrayVec<::Output,2>{ let a2pos=match a2.cmp(&Self::ZERO){ Ordering::Greater=>true, Ordering::Equal=>return ArrayVec::from_iter(Self::zeroes1(a0,a1).into_iter()), Ordering::Less=>true, }; paste::item!{ - let radicand=a1.[](a1)-a2.[](a0)*4; + let radicand=a1*a1-a2*a0*4; } - match radicand.cmp(&Fixed::<{$n*2},{$n*2*32}>::ZERO){ + match radicand.cmp(&::Output::ZERO){ Ordering::Greater=>{ - let planar_radicand=radicand.sqrt().halve_precision(); + //TODO: use resize method + let planar_radicand:Self=radicand.sqrt().halve_precision(); //sort roots ascending and avoid taking the difference of large numbers - match (a2pos,Self::ZERO[Ratio::new(-a1-planar_radicand,a2*2),Ratio::new(a0*2,-a1-planar_radicand)].into(), - (true, false)=>[Ratio::new(a0*2,-a1+planar_radicand),Ratio::new(-a1+planar_radicand,a2*2)].into(), - (false,true )=>[Ratio::new(a0*2,-a1-planar_radicand),Ratio::new(-a1-planar_radicand,a2*2)].into(), - (false,false)=>[Ratio::new(-a1+planar_radicand,a2*2),Ratio::new(a0*2,-a1+planar_radicand)].into(), - } + let zeroes=match (a2pos,Self::ZERO[(-a1-planar_radicand)/(a2*2),(a0*2)/(-a1-planar_radicand)], + (true, false)=>[(a0*2)/(-a1+planar_radicand),(-a1+planar_radicand)/(a2*2)], + (false,true )=>[(a0*2)/(-a1-planar_radicand),(-a1-planar_radicand)/(a2*2)], + (false,false)=>[(-a1+planar_radicand)/(a2*2),(a0*2)/(-a1+planar_radicand)], + }; + ArrayVec::from_iter(zeroes) }, - Ordering::Equal=>ArrayVec::from_iter([Ratio::new(a1,a2*-2)]), + Ordering::Equal=>ArrayVec::from_iter([(a1)/(a2*-2)]), Ordering::Less=>ArrayVec::new_const(), } } #[inline] - pub fn zeroes1(a0:Self,a1:Self)->ArrayVec,1>{ + pub fn zeroes1(a0:Self,a1:Self)->ArrayVec<::Output,1>{ if a1==Self::ZERO{ ArrayVec::new_const() }else{ - ArrayVec::from_iter([Ratio::new(-a0,a1)]) + ArrayVec::from_iter([(-a0)/(a1)]) } } }