mirror of https://github.com/rm-dr/daisy
rug types are only used if we're on unix
parent
e0f36ce112
commit
8982b3b118
|
@ -14,7 +14,6 @@ panic = "abort"
|
||||||
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rug = "1.19.2"
|
|
||||||
cfg-if = "1.0.0"
|
cfg-if = "1.0.0"
|
||||||
|
|
||||||
[target.'cfg(target_family = "unix")'.dependencies]
|
[target.'cfg(target_family = "unix")'.dependencies]
|
||||||
|
|
|
@ -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<F64Q> {
|
||||||
|
return Some(F64Q{ val: f });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_string(s: &str) -> Option<F64Q> {
|
||||||
|
let v = s.parse::<f64>();
|
||||||
|
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<F64Q> 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<Ordering> {
|
||||||
|
self.val.partial_cmp(&other.val)
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,7 +13,7 @@ use std::ops::{
|
||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
|
use crate::quantity::wrap_float;
|
||||||
use crate::quantity::Quantity;
|
use crate::quantity::Quantity;
|
||||||
use crate::quantity::QuantBase;
|
use crate::quantity::QuantBase;
|
||||||
use crate::quantity::FloatBase;
|
use crate::quantity::FloatBase;
|
||||||
|
@ -25,7 +25,7 @@ use super::FLOAT_PRECISION;
|
||||||
macro_rules! foward {
|
macro_rules! foward {
|
||||||
( $x:ident ) => {
|
( $x:ident ) => {
|
||||||
fn $x(&self) -> Quantity {
|
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);
|
foward!(log2);
|
||||||
|
|
||||||
fn log(&self, base: Quantity) -> Quantity {
|
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()
|
Quantity::float_from_rat(&base).log10()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pow(&self, base: Quantity) -> Quantity {
|
fn pow(&self, base: Quantity) -> Quantity {
|
||||||
match base {
|
match base {
|
||||||
Quantity::Rational { .. } => self.pow(Quantity::float_from_rat(&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)})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,12 +7,29 @@ use std::ops::{
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
mod rationalq;
|
|
||||||
mod floatq;
|
|
||||||
pub mod quantity;
|
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 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 FLOAT_PRECISION: u32 = 1024;
|
||||||
const PRINT_LEN: usize = 5; // How many significant digits we will show in output
|
const PRINT_LEN: usize = 5; // How many significant digits we will show in output
|
||||||
|
|
|
@ -7,58 +7,105 @@ use std::ops::{
|
||||||
};
|
};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use crate::quantity::rationalq::RationalQ;
|
use crate::quantity::wrap_rational;
|
||||||
use crate::quantity::floatq::FloatQ;
|
use crate::quantity::wrap_float;
|
||||||
|
|
||||||
|
|
||||||
use crate::quantity::QuantBase;
|
use crate::quantity::QuantBase;
|
||||||
use crate::quantity::RationalBase;
|
use crate::quantity::RationalBase;
|
||||||
use crate::quantity::FloatBase;
|
use crate::quantity::FloatBase;
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
#[derive(Clone)]
|
cfg_if::cfg_if! {
|
||||||
pub enum Quantity {
|
if #[cfg(target_arch = "unix")] {
|
||||||
Rational{ v: RationalQ },
|
use crate::quantity::rationalq::RationalQ;
|
||||||
Float{ v: FloatQ }
|
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 {
|
impl Quantity {
|
||||||
|
|
||||||
pub fn new_rational(top: i64, bottom: i64) -> Quantity {
|
cfg_if::cfg_if! {
|
||||||
return Quantity::Rational {
|
if #[cfg(target_arch = "unix")] {
|
||||||
v: RationalQ::from_frac(top, bottom)
|
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<Quantity> {
|
||||||
|
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<Quantity> {
|
||||||
|
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 {
|
pub fn float_from_rat(r: &Quantity) -> Quantity {
|
||||||
return Quantity::Float {
|
match &r {
|
||||||
v: FloatQ::from_f64(v).unwrap()
|
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<Quantity> {
|
||||||
|
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<Quantity> {
|
||||||
|
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<Quantity> {
|
pub fn float_from_rat(r: &Quantity) -> Quantity {
|
||||||
let r = RationalQ::from_string(s);
|
match &r {
|
||||||
if r.is_none() { return None; }
|
Quantity::Float { .. } => r.clone(),
|
||||||
return Some(Quantity::Rational{v: r.unwrap()})
|
Quantity::Rational { .. } => r.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_float_from_string(s: &str) -> Option<Quantity> {
|
|
||||||
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 is_nan(&self) -> bool {
|
pub fn is_nan(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Quantity::Float { v } => {v.val.is_nan()},
|
Quantity::Float { v } => {v.val.is_nan()},
|
||||||
|
@ -156,8 +203,8 @@ impl Neg for Quantity where {
|
||||||
|
|
||||||
fn neg(self) -> Self::Output {
|
fn neg(self) -> Self::Output {
|
||||||
match self {
|
match self {
|
||||||
Quantity::Float { v } => {Quantity::Float{ v: -v }},
|
Quantity::Float { v } => {wrap_float!(-v)},
|
||||||
Quantity::Rational { v } => {Quantity::Rational { v: -v }},
|
Quantity::Rational { v } => {wrap_rational!(-v)},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -167,10 +214,10 @@ impl Add for Quantity {
|
||||||
|
|
||||||
fn add(self, other: Self) -> Self::Output {
|
fn add(self, other: Self) -> Self::Output {
|
||||||
match (&self, &other) {
|
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::Float{ .. }, Quantity::Rational{ .. }) => {self + Quantity::float_from_rat(&other)},
|
||||||
(Quantity::Rational{ .. }, Quantity::Float{ .. }) => {Quantity::float_from_rat(&self) + 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 {
|
fn sub(self, other: Self) -> Self::Output {
|
||||||
match (&self, &other) {
|
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::Float{ .. }, Quantity::Rational{ .. }) => {self - Quantity::float_from_rat(&other)},
|
||||||
(Quantity::Rational{ .. }, Quantity::Float{ .. }) => {Quantity::float_from_rat(&self) - 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 {
|
fn mul(self, other: Self) -> Self::Output {
|
||||||
match (&self, &other) {
|
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::Float{ .. }, Quantity::Rational{ .. }) => {self * Quantity::float_from_rat(&other)},
|
||||||
(Quantity::Rational{ .. }, Quantity::Float{ .. }) => {Quantity::float_from_rat(&self) * self},
|
(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 {
|
fn div(self, other: Self) -> Self::Output {
|
||||||
match (&self, &other) {
|
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::Float{ .. }, Quantity::Rational{ .. }) => {self / Quantity::float_from_rat(&other)},
|
||||||
(Quantity::Rational{ .. }, Quantity::Float{ .. }) => {Quantity::float_from_rat(&self) / 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<Quantity> for Quantity {
|
||||||
|
|
||||||
fn rem(self, other: Quantity) -> Self::Output {
|
fn rem(self, other: Quantity) -> Self::Output {
|
||||||
match (&self, &other) {
|
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::Float{ .. }, Quantity::Rational{ .. }) => {self % Quantity::float_from_rat(&other)},
|
||||||
(Quantity::Rational{ .. }, Quantity::Float{ .. }) => {Quantity::float_from_rat(&self) % 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())},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,19 +11,16 @@ use std::ops::{
|
||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
|
use crate::quantity::wrap_rational;
|
||||||
use crate::quantity::Quantity;
|
use crate::quantity::Quantity;
|
||||||
use crate::quantity::QuantBase;
|
use crate::quantity::QuantBase;
|
||||||
use crate::quantity::RationalBase;
|
use crate::quantity::RationalBase;
|
||||||
|
|
||||||
macro_rules! wraprat {
|
|
||||||
( $x:expr ) => { Quantity::Rational{v: $x} }
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! float_foward {
|
macro_rules! float_foward {
|
||||||
( $x:ident ) => {
|
( $x:ident ) => {
|
||||||
fn $x(&self) -> Quantity {
|
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<usize>) -> (bool, St
|
||||||
|
|
||||||
impl ToString for RationalQ{
|
impl ToString for RationalQ{
|
||||||
fn to_string(&self) -> String {
|
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();
|
return v.to_string();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,17 +51,17 @@ impl ToString for RationalQ{
|
||||||
impl QuantBase for RationalQ {
|
impl QuantBase for RationalQ {
|
||||||
|
|
||||||
fn fract(&self) -> Quantity {
|
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_zero(&self) -> bool {self.val == Rational::from((0,1))}
|
||||||
fn is_negative(&self) -> bool { self.val.clone().signum() == -1 }
|
fn is_negative(&self) -> bool { self.val.clone().signum() == -1 }
|
||||||
fn is_positive(&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 abs(&self) -> Quantity {wrap_rational!(RationalQ{val: self.val.clone().abs()})}
|
||||||
fn floor(&self) -> Quantity {wraprat!(RationalQ{val: self.val.clone().floor()})}
|
fn floor(&self) -> Quantity {wrap_rational!(RationalQ{val: self.val.clone().floor()})}
|
||||||
fn ceil(&self) -> Quantity {wraprat!(RationalQ{val: self.val.clone().ceil()})}
|
fn ceil(&self) -> Quantity {wrap_rational!(RationalQ{val: self.val.clone().ceil()})}
|
||||||
fn round(&self) -> Quantity {wraprat!(RationalQ{val: self.val.clone().round()})}
|
fn round(&self) -> Quantity {wrap_rational!(RationalQ{val: self.val.clone().round()})}
|
||||||
|
|
||||||
float_foward!(sin);
|
float_foward!(sin);
|
||||||
float_foward!(cos);
|
float_foward!(cos);
|
||||||
|
@ -86,11 +83,11 @@ impl QuantBase for RationalQ {
|
||||||
float_foward!(log2);
|
float_foward!(log2);
|
||||||
|
|
||||||
fn log(&self, base: Quantity) -> Quantity {
|
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 {
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue