2024-09-02 23:15:17 +00:00
|
|
|
use crate::fixed::Fixed;
|
|
|
|
|
|
|
|
use arrayvec::ArrayVec;
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
macro_rules! impl_zeroes{
|
|
|
|
($n:expr)=>{
|
2024-09-03 00:03:01 +00:00
|
|
|
impl Fixed<$n,{$n*32}>{
|
2024-09-02 23:15:17 +00:00
|
|
|
#[inline]
|
2024-09-10 18:26:46 +00:00
|
|
|
pub fn zeroes2(a0:Self,a1:Self,a2:Self)->ArrayVec<<Self as core::ops::Div>::Output,2>{
|
2024-09-02 23:15:17 +00:00
|
|
|
let a2pos=match a2.cmp(&Self::ZERO){
|
|
|
|
Ordering::Greater=>true,
|
2024-09-03 00:03:01 +00:00
|
|
|
Ordering::Equal=>return ArrayVec::from_iter(Self::zeroes1(a0,a1).into_iter()),
|
2024-09-26 22:26:18 +00:00
|
|
|
Ordering::Less=>false,
|
2024-09-02 23:15:17 +00:00
|
|
|
};
|
2024-09-10 18:26:46 +00:00
|
|
|
let radicand=a1*a1-a2*a0*4;
|
|
|
|
match radicand.cmp(&<Self as core::ops::Mul>::Output::ZERO){
|
2024-09-02 23:15:17 +00:00
|
|
|
Ordering::Greater=>{
|
2024-09-11 20:59:33 +00:00
|
|
|
paste::item!{
|
|
|
|
let planar_radicand=radicand.sqrt().[<fix_ $n>]();
|
|
|
|
}
|
2024-09-02 23:15:17 +00:00
|
|
|
//sort roots ascending and avoid taking the difference of large numbers
|
2024-09-10 18:26:46 +00:00
|
|
|
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)
|
2024-09-02 23:15:17 +00:00
|
|
|
},
|
2024-09-10 18:26:46 +00:00
|
|
|
Ordering::Equal=>ArrayVec::from_iter([(a1)/(a2*-2)]),
|
2024-09-02 23:15:17 +00:00
|
|
|
Ordering::Less=>ArrayVec::new_const(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[inline]
|
2024-09-10 18:26:46 +00:00
|
|
|
pub fn zeroes1(a0:Self,a1:Self)->ArrayVec<<Self as core::ops::Div>::Output,1>{
|
2024-09-02 23:15:17 +00:00
|
|
|
if a1==Self::ZERO{
|
|
|
|
ArrayVec::new_const()
|
|
|
|
}else{
|
2024-09-10 18:26:46 +00:00
|
|
|
ArrayVec::from_iter([(-a0)/(a1)])
|
2024-09-02 23:15:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
impl_zeroes!(1);
|
|
|
|
impl_zeroes!(2);
|
|
|
|
impl_zeroes!(3);
|
|
|
|
impl_zeroes!(4);
|
2024-09-03 00:03:01 +00:00
|
|
|
//sqrt doubles twice!
|
|
|
|
//impl_zeroes!(5);
|
|
|
|
//impl_zeroes!(6);
|
|
|
|
//impl_zeroes!(7);
|
|
|
|
//impl_zeroes!(8);
|