zeroes function uses type transformation, drops direct ratio dep from zeroes

This commit is contained in:
Quaternions 2024-09-10 11:26:46 -07:00
parent c20a0a4a89
commit fa8614891d
3 changed files with 20 additions and 18 deletions

View File

@ -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"

View File

@ -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.[<wide_mul_ $n _ $n>](Self::ONE);
//TODO: use resize method
let wide_self:<Self as core::ops::Mul>::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.[<wide_mul_ $n _ $n>](new_result)<=wide_self{
if new_result*new_result<=wide_self{
result=new_result;
}
}

View File

@ -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<Ratio<Self,Self>,2>{
pub fn zeroes2(a0:Self,a1:Self,a2:Self)->ArrayVec<<Self as core::ops::Div>::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.[<wide_mul_ $n _ $n>](a1)-a2.[<wide_mul_ $n _ $n>](a0)*4;
let radicand=a1*a1-a2*a0*4;
}
match radicand.cmp(&Fixed::<{$n*2},{$n*2*32}>::ZERO){
match radicand.cmp(&<Self as core::ops::Mul>::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<a1){
(true, true )=>[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){
(true, true )=>[(-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<Ratio<Self,Self>,1>{
pub fn zeroes1(a0:Self,a1:Self)->ArrayVec<<Self as core::ops::Div>::Output,1>{
if a1==Self::ZERO{
ArrayVec::new_const()
}else{
ArrayVec::from_iter([Ratio::new(-a0,a1)])
ArrayVec::from_iter([(-a0)/(a1)])
}
}
}