diff --git a/Cargo.toml b/Cargo.toml index 946f89a..094d7e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,6 @@ panic = "abort" [dependencies] -rug = "1.19.2" cfg-if = "1.0.0" [target.'cfg(target_family = "unix")'.dependencies] diff --git a/src/quantity/f64q.rs b/src/quantity/f64q.rs new file mode 100644 index 0000000..3511f00 --- /dev/null +++ b/src/quantity/f64q.rs @@ -0,0 +1,188 @@ +use std::ops::{ + Add, Sub, Mul, Div, + Neg, Rem, + + AddAssign, SubAssign, + MulAssign, DivAssign +}; + +use std::cmp::Ordering; + +use crate::quantity::wrap_float; +use crate::quantity::Quantity; +use crate::quantity::QuantBase; +use crate::quantity::FloatBase; + + +macro_rules! foward { + ( $x:ident ) => { + fn $x(&self) -> Quantity { + wrap_float!(F64Q{ val: self.val.clone().$x() }) + } + } +} + +#[derive(Debug)] +#[derive(Clone)] +pub struct F64Q where { + pub val: f64 +} + +impl ToString for F64Q { + fn to_string(&self) -> String { self.val.to_string() } +} + + +impl QuantBase for F64Q { + + foward!(fract); + + fn is_zero(&self) -> bool {self.val == 0f64} + fn is_negative(&self) -> bool { self.val.is_sign_negative() } + fn is_positive(&self) -> bool { self.val.is_sign_positive() } + + foward!(abs); + foward!(floor); + foward!(ceil); + foward!(round); + + foward!(sin); + foward!(cos); + foward!(tan); + foward!(asin); + foward!(acos); + foward!(atan); + + foward!(sinh); + foward!(cosh); + foward!(tanh); + foward!(asinh); + foward!(acosh); + foward!(atanh); + + foward!(exp); + foward!(ln); + foward!(log10); + foward!(log2); + + fn log(&self, base: Quantity) -> Quantity { + wrap_float!(F64Q{ val: self.val.clone().log10() }) / + Quantity::float_from_rat(&base).log10() + } + + fn pow(&self, base: Quantity) -> Quantity { + match base { + Quantity::Rational { .. } => self.pow(Quantity::float_from_rat(&base)), + Quantity::Float { v } => wrap_float!(F64Q{ val: self.val.clone().powf(v.val) }) + } + + } + +} + +impl FloatBase for F64Q { + fn from_f64(f: f64) -> Option { + return Some(F64Q{ val: f }); + } + + fn from_string(s: &str) -> Option { + let v = s.parse::(); + let v = match v { + Ok(x) => x, + Err(_) => return None + }; + + return Some(F64Q{ val: v }); + } +} + + + +impl Add for F64Q where { + type Output = Self; + + fn add(self, other: Self) -> Self::Output { + Self { val: self.val + other.val} + } +} + +impl AddAssign for F64Q where { + fn add_assign(&mut self, other: Self) { + self.val += other.val; + } +} + +impl Sub for F64Q { + type Output = Self; + + fn sub(self, other: Self) -> Self::Output { + Self {val: self.val - other.val} + } +} + +impl SubAssign for F64Q where { + fn sub_assign(&mut self, other: Self) { + self.val -= other.val; + } +} + +impl Mul for F64Q { + type Output = Self; + + fn mul(self, other: Self) -> Self::Output { + Self {val: self.val * other.val} + } +} + +impl MulAssign for F64Q where { + fn mul_assign(&mut self, other: Self) { + self.val *= other.val; + } +} + +impl Div for F64Q { + type Output = Self; + + fn div(self, other: Self) -> Self::Output { + Self {val: self.val / other.val} + } +} + +impl DivAssign for F64Q where { + fn div_assign(&mut self, other: Self) { + self.val /= other.val; + } +} + +impl Neg for F64Q where { + type Output = Self; + + fn neg(self) -> Self::Output { + Self {val: -self.val} + } +} + +impl Rem for F64Q { + type Output = Self; + + fn rem(self, modulus: F64Q) -> Self::Output { + if { + (!self.fract().is_zero()) || + (!modulus.fract().is_zero()) + } { panic!() } + + F64Q{val : self.val.fract() % modulus.val.fract()} + } +} + +impl PartialEq for F64Q { + fn eq(&self, other: &Self) -> bool { + self.val == other.val + } +} + +impl PartialOrd for F64Q { + fn partial_cmp(&self, other: &Self) -> Option { + self.val.partial_cmp(&other.val) + } +} \ No newline at end of file diff --git a/src/quantity/floatq.rs b/src/quantity/floatq.rs index ca93c2f..d50796d 100644 --- a/src/quantity/floatq.rs +++ b/src/quantity/floatq.rs @@ -13,7 +13,7 @@ use std::ops::{ use std::cmp::Ordering; - +use crate::quantity::wrap_float; use crate::quantity::Quantity; use crate::quantity::QuantBase; use crate::quantity::FloatBase; @@ -25,7 +25,7 @@ use super::FLOAT_PRECISION; macro_rules! foward { ( $x:ident ) => { fn $x(&self) -> Quantity { - Quantity::Float{v: FloatQ{ val: self.val.clone().$x() }} + wrap_float!(FloatQ{ val: self.val.clone().$x()}) } } } @@ -136,14 +136,14 @@ impl QuantBase for FloatQ { foward!(log2); fn log(&self, base: Quantity) -> Quantity { - Quantity::Float{v: FloatQ{ val: self.val.clone().log10() }} / + wrap_float!(FloatQ{ val: self.val.clone().log10() }) / Quantity::float_from_rat(&base).log10() } fn pow(&self, base: Quantity) -> Quantity { match base { Quantity::Rational { .. } => self.pow(Quantity::float_from_rat(&base)), - Quantity::Float { v } => Quantity::Float{v: FloatQ{ val: self.val.clone().pow(v.val) }} + Quantity::Float { v } => wrap_float!(FloatQ{ val: self.val.clone().pow(v.val)}) } } diff --git a/src/quantity/mod.rs b/src/quantity/mod.rs index e875f7c..446559d 100644 --- a/src/quantity/mod.rs +++ b/src/quantity/mod.rs @@ -7,12 +7,29 @@ use std::ops::{ }; - -mod rationalq; -mod floatq; pub mod quantity; +cfg_if::cfg_if! { + if #[cfg(target_arch = "unix")] { + mod rationalq; + mod floatq; + } else { + mod f64q; + } +} + +macro_rules! wrap_rational { + ( $x:expr ) => { Quantity::Rational{v: $x} } +} + +macro_rules! wrap_float { + ( $x:expr ) => { Quantity::Float{v: $x} } +} + pub use crate::quantity::quantity::Quantity; +pub(in crate::quantity) use wrap_rational; +pub(in crate::quantity) use wrap_float; + const FLOAT_PRECISION: u32 = 1024; const PRINT_LEN: usize = 5; // How many significant digits we will show in output diff --git a/src/quantity/quantity.rs b/src/quantity/quantity.rs index 17cdf80..c628361 100644 --- a/src/quantity/quantity.rs +++ b/src/quantity/quantity.rs @@ -7,58 +7,105 @@ use std::ops::{ }; use std::cmp::Ordering; -use crate::quantity::rationalq::RationalQ; -use crate::quantity::floatq::FloatQ; - +use crate::quantity::wrap_rational; +use crate::quantity::wrap_float; use crate::quantity::QuantBase; use crate::quantity::RationalBase; use crate::quantity::FloatBase; -#[derive(Debug)] -#[derive(Clone)] -pub enum Quantity { - Rational{ v: RationalQ }, - Float{ v: FloatQ } + +cfg_if::cfg_if! { + if #[cfg(target_arch = "unix")] { + use crate::quantity::rationalq::RationalQ; + use crate::quantity::floatq::FloatQ; + + #[derive(Debug)] + #[derive(Clone)] + pub enum Quantity { + Rational{ v: RationalQ }, + Float{ v: FloatQ } + } + } else { + use crate::quantity::f64q::F64Q; + + #[derive(Debug)] + #[derive(Clone)] + pub enum Quantity { + Rational{ v: F64Q }, + Float{ v: F64Q } + } + } } impl Quantity { - pub fn new_rational(top: i64, bottom: i64) -> Quantity { - return Quantity::Rational { - v: RationalQ::from_frac(top, bottom) - } - } + cfg_if::cfg_if! { + if #[cfg(target_arch = "unix")] { + pub fn new_rational(top: i64, bottom: i64) -> Quantity { + return wrap_rational!(RationalQ::from_frac(top, bottom)); + } + + pub fn new_float(v: f64) -> Quantity { + return wrap_float!(FloatQ::from_f64(v).unwrap()) + } + + pub fn new_rational_from_string(s: &str) -> Option { + let r = RationalQ::from_string(s); + if r.is_none() { return None; } + return Some(wrap_rational!(r.unwrap())); + } + + pub fn new_float_from_string(s: &str) -> Option { + let v = FloatQ::from_string(s); + if v.is_none() { return None; } + return Some(wrap_float!(v.unwrap())) + } - pub fn new_float(v: f64) -> Quantity { - return Quantity::Float { - v: FloatQ::from_f64(v).unwrap() - } - } + pub fn float_from_rat(r: &Quantity) -> Quantity { + match &r { + Quantity::Float { .. } => r.clone(), + Quantity::Rational { v } => wrap_float!( + FloatQ::from(v.val.numer()).unwrap() / + FloatQ::from(v.val.denom()).unwrap() + ) + } + } + } else { + pub fn new_rational(top: i64, bottom: i64) -> Quantity { + return wrap_float!(F64Q::from_f64( (top as f64) / (bottom as f64)).unwrap()) + } + + pub fn new_float(v: f64) -> Quantity { + return wrap_float!(F64Q::from_f64(v).unwrap()) + } + + pub fn new_rational_from_string(s: &str) -> Option { + let r = F64Q::from_string(s); + if r.is_none() { return None; } + return Some(wrap_rational!(r.unwrap())); + } + + pub fn new_float_from_string(s: &str) -> Option { + let v = F64Q::from_string(s); + if v.is_none() { return None; } + return Some(wrap_float!(v.unwrap())) + } - pub fn new_rational_from_string(s: &str) -> Option { - let r = RationalQ::from_string(s); - if r.is_none() { return None; } - return Some(Quantity::Rational{v: r.unwrap()}) - } - - pub fn new_float_from_string(s: &str) -> Option { - let v = FloatQ::from_string(s); - if v.is_none() { return None; } - return Some(Quantity::Float{v: v.unwrap()}) - } - - pub fn float_from_rat(r: &Quantity) -> Quantity { - match &r { - Quantity::Float { .. } => r.clone(), - Quantity::Rational { v } => Quantity::Float { v: - FloatQ::from(v.val.numer()).unwrap() / - FloatQ::from(v.val.denom()).unwrap() + pub fn float_from_rat(r: &Quantity) -> Quantity { + match &r { + Quantity::Float { .. } => r.clone(), + Quantity::Rational { .. } => r.clone() + } } } } + + + + pub fn is_nan(&self) -> bool { match self { Quantity::Float { v } => {v.val.is_nan()}, @@ -156,8 +203,8 @@ impl Neg for Quantity where { fn neg(self) -> Self::Output { match self { - Quantity::Float { v } => {Quantity::Float{ v: -v }}, - Quantity::Rational { v } => {Quantity::Rational { v: -v }}, + Quantity::Float { v } => {wrap_float!(-v)}, + Quantity::Rational { v } => {wrap_rational!(-v)}, } } } @@ -167,10 +214,10 @@ impl Add for Quantity { fn add(self, other: Self) -> Self::Output { match (&self, &other) { - (Quantity::Float{v:a}, Quantity::Float{v:b}) => {Quantity::Float{ v: a.clone()+b.clone() }}, + (Quantity::Float{v:a}, Quantity::Float{v:b}) => {wrap_float!(a.clone()+b.clone())}, (Quantity::Float{ .. }, Quantity::Rational{ .. }) => {self + Quantity::float_from_rat(&other)}, (Quantity::Rational{ .. }, Quantity::Float{ .. }) => {Quantity::float_from_rat(&self) + other}, - (Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {Quantity::Rational{ v: a.clone()+b.clone() }}, + (Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {wrap_rational!(a.clone()+b.clone())}, } } } @@ -191,10 +238,10 @@ impl Sub for Quantity { fn sub(self, other: Self) -> Self::Output { match (&self, &other) { - (Quantity::Float{v:a}, Quantity::Float{v:b}) => {Quantity::Float{ v: a.clone()-b.clone() }}, + (Quantity::Float{v:a}, Quantity::Float{v:b}) => {wrap_float!(a.clone()-b.clone())}, (Quantity::Float{ .. }, Quantity::Rational{ .. }) => {self - Quantity::float_from_rat(&other)}, (Quantity::Rational{ .. }, Quantity::Float{ .. }) => {Quantity::float_from_rat(&self) - other}, - (Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {Quantity::Rational{ v: a.clone()-b.clone() }}, + (Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {wrap_rational!(a.clone()-b.clone())}, } } } @@ -215,10 +262,10 @@ impl Mul for Quantity { fn mul(self, other: Self) -> Self::Output { match (&self, &other) { - (Quantity::Float{v:a}, Quantity::Float{v:b}) => {Quantity::Float{ v: a.clone()*b.clone() }}, + (Quantity::Float{v:a}, Quantity::Float{v:b}) => {wrap_float!(a.clone()*b.clone())}, (Quantity::Float{ .. }, Quantity::Rational{ .. }) => {self * Quantity::float_from_rat(&other)}, (Quantity::Rational{ .. }, Quantity::Float{ .. }) => {Quantity::float_from_rat(&self) * self}, - (Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {Quantity::Rational{ v: a.clone()*b.clone() }}, + (Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {wrap_rational!(a.clone()*b.clone())}, } } } @@ -239,10 +286,10 @@ impl Div for Quantity { fn div(self, other: Self) -> Self::Output { match (&self, &other) { - (Quantity::Float{v:a}, Quantity::Float{v:b}) => {Quantity::Float{ v: a.clone()/b.clone() }}, + (Quantity::Float{v:a}, Quantity::Float{v:b}) => {wrap_float!(a.clone()/b.clone())}, (Quantity::Float{ .. }, Quantity::Rational{ .. }) => {self / Quantity::float_from_rat(&other)}, (Quantity::Rational{ .. }, Quantity::Float{ .. }) => {Quantity::float_from_rat(&self) / other}, - (Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {Quantity::Rational{ v: a.clone()/b.clone() }}, + (Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {wrap_rational!(a.clone()/b.clone())}, } } } @@ -263,10 +310,10 @@ impl Rem for Quantity { fn rem(self, other: Quantity) -> Self::Output { match (&self, &other) { - (Quantity::Float{v:a}, Quantity::Float{v:b}) => {Quantity::Float{ v: a.clone()%b.clone() }}, + (Quantity::Float{v:a}, Quantity::Float{v:b}) => {wrap_float!(a.clone()%b.clone())}, (Quantity::Float{ .. }, Quantity::Rational{ .. }) => {self % Quantity::float_from_rat(&other)}, (Quantity::Rational{ .. }, Quantity::Float{ .. }) => {Quantity::float_from_rat(&self) % other}, - (Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {Quantity::Rational { v: a.clone()%b.clone() }}, + (Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {wrap_rational!(a.clone()%b.clone())}, } } } diff --git a/src/quantity/rationalq.rs b/src/quantity/rationalq.rs index ae3fcc3..7016f20 100644 --- a/src/quantity/rationalq.rs +++ b/src/quantity/rationalq.rs @@ -11,19 +11,16 @@ use std::ops::{ use std::cmp::Ordering; - +use crate::quantity::wrap_rational; use crate::quantity::Quantity; use crate::quantity::QuantBase; use crate::quantity::RationalBase; -macro_rules! wraprat { - ( $x:expr ) => { Quantity::Rational{v: $x} } -} macro_rules! float_foward { ( $x:ident ) => { fn $x(&self) -> Quantity { - Quantity::float_from_rat(&wraprat!(self.clone())).$x() + Quantity::float_from_rat(&wrap_rational!(self.clone())).$x() } } } @@ -46,7 +43,7 @@ fn to_sign_string_exp(&self, radix: i32, num_digits: Option) -> (bool, St impl ToString for RationalQ{ fn to_string(&self) -> String { - let v = Quantity::float_from_rat(&wraprat!(self.clone())); + let v = Quantity::float_from_rat(&wrap_rational!(self.clone())); return v.to_string(); } } @@ -54,17 +51,17 @@ impl ToString for RationalQ{ impl QuantBase for RationalQ { fn fract(&self) -> Quantity { - wraprat!(RationalQ{val: self.val.clone().fract_floor(Integer::new()).0}) + wrap_rational!(RationalQ{val: self.val.clone().fract_floor(Integer::new()).0}) } fn is_zero(&self) -> bool {self.val == Rational::from((0,1))} fn is_negative(&self) -> bool { self.val.clone().signum() == -1 } fn is_positive(&self) -> bool { self.val.clone().signum() == 1 } - fn abs(&self) -> Quantity {wraprat!(RationalQ{val: self.val.clone().abs()})} - fn floor(&self) -> Quantity {wraprat!(RationalQ{val: self.val.clone().floor()})} - fn ceil(&self) -> Quantity {wraprat!(RationalQ{val: self.val.clone().ceil()})} - fn round(&self) -> Quantity {wraprat!(RationalQ{val: self.val.clone().round()})} + fn abs(&self) -> Quantity {wrap_rational!(RationalQ{val: self.val.clone().abs()})} + fn floor(&self) -> Quantity {wrap_rational!(RationalQ{val: self.val.clone().floor()})} + fn ceil(&self) -> Quantity {wrap_rational!(RationalQ{val: self.val.clone().ceil()})} + fn round(&self) -> Quantity {wrap_rational!(RationalQ{val: self.val.clone().round()})} float_foward!(sin); float_foward!(cos); @@ -86,11 +83,11 @@ impl QuantBase for RationalQ { float_foward!(log2); fn log(&self, base: Quantity) -> Quantity { - Quantity::float_from_rat(&wraprat!(self.clone())).log10() / base.log10() + Quantity::float_from_rat(&wrap_rational!(self.clone())).log10() / base.log10() } fn pow(&self, base: Quantity) -> Quantity { - Quantity::float_from_rat(&wraprat!(self.clone())).pow(base) + Quantity::float_from_rat(&wrap_rational!(self.clone())).pow(base) } }