Added basic units (incomplete)

pull/2/head
Mark 2023-04-08 16:47:47 -07:00
parent 10f706582e
commit 5b8dd2f703
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
8 changed files with 396 additions and 141 deletions

View File

@ -12,7 +12,7 @@ use crate::parser::groupify::groupify;
use crate::parser::treeify::treeify; use crate::parser::treeify::treeify;
use crate::parser::find_subs::find_subs; use crate::parser::find_subs::find_subs;
use crate::quantity::Quantity; use crate::quantity::{Quantity, BaseUnit};
use crate::tokens::Token; use crate::tokens::Token;
@ -96,6 +96,19 @@ impl PreToken {
"e" => { Token::Constant(Quantity::new_float_from_string("2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427").unwrap(), String::from("e")) }, "e" => { Token::Constant(Quantity::new_float_from_string("2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427").unwrap(), String::from("e")) },
"phi"|"φ" => { Token::Constant(Quantity::new_float_from_string("1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137").unwrap(), String::from("φ")) }, "phi"|"φ" => { Token::Constant(Quantity::new_float_from_string("1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137").unwrap(), String::from("φ")) },
// Units
"m" => {
let mut u = Quantity::new_rational(1, 1);
u.add_unit(BaseUnit::Meter, 1f64);
Token::Number(u)
},
"s" => {
let mut u = Quantity::new_rational(1, 1);
u.add_unit(BaseUnit::Second, 1f64);
Token::Number(u)
}
_ => { return Err((l, ParserError::Undefined(s))); } _ => { return Err((l, ParserError::Undefined(s))); }
}); });
} }

View File

@ -13,8 +13,6 @@ use std::ops::{
use std::cmp::Ordering; use std::cmp::Ordering;
use crate::quantity::wrap_float;
use crate::quantity::Quantity;
use crate::quantity::QuantBase; use crate::quantity::QuantBase;
use crate::quantity::FloatBase; use crate::quantity::FloatBase;
use crate::quantity::PRINT_LEN; use crate::quantity::PRINT_LEN;
@ -24,8 +22,8 @@ use super::FLOAT_PRECISION;
macro_rules! foward { macro_rules! foward {
( $x:ident ) => { ( $x:ident ) => {
fn $x(&self) -> Quantity { fn $x(&self) -> Option<FloatQ> {
wrap_float!(FloatQ{ val: self.val.clone().$x()}) Some(FloatQ{ val: self.val.clone().$x()})
} }
} }
} }
@ -135,17 +133,12 @@ impl QuantBase for FloatQ {
foward!(log10); foward!(log10);
foward!(log2); foward!(log2);
fn log(&self, base: Quantity) -> Quantity { fn log(&self, base: FloatQ) -> Option<FloatQ> {
wrap_float!(FloatQ{ val: self.val.clone().log10() }) / Some(FloatQ{ val: self.val.clone().log10() } / base.log10().unwrap())
Quantity::float_from_rat(&base).log10()
} }
fn pow(&self, base: Quantity) -> Quantity { fn pow(&self, base: FloatQ) -> Option<FloatQ> {
match base { Some(FloatQ{ val: self.val.clone().pow(base.val)})
Quantity::Rational { .. } => self.pow(Quantity::float_from_rat(&base)),
Quantity::Float { v } => wrap_float!(FloatQ{ val: self.val.clone().pow(v.val)})
}
} }
} }
@ -242,8 +235,8 @@ impl Rem<FloatQ> for FloatQ {
fn rem(self, modulus: FloatQ) -> Self::Output { fn rem(self, modulus: FloatQ) -> Self::Output {
if { if {
(!self.fract().is_zero()) || (!self.fract().unwrap().is_zero()) ||
(!modulus.fract().is_zero()) (!modulus.fract().unwrap().is_zero())
} { panic!() } } { panic!() }
FloatQ{val : self.val.fract() % modulus.val.fract()} FloatQ{val : self.val.fract() % modulus.val.fract()}

View File

@ -25,6 +25,9 @@ cross-compilation to other systems. RUG does not work on all systems.
*/ */
pub mod quantity; pub mod quantity;
mod unit;
pub use crate::quantity::unit::Unit;
pub use crate::quantity::unit::BaseUnit;
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(target_family = "unix")] { if #[cfg(target_family = "unix")] {
@ -36,11 +39,11 @@ cfg_if::cfg_if! {
} }
macro_rules! wrap_rational { macro_rules! wrap_rational {
( $x:expr ) => { Quantity::Rational{v: $x} } ( $x:expr, $y:expr ) => { Quantity::Rational{v: $x, u: $y} }
} }
macro_rules! wrap_float { macro_rules! wrap_float {
( $x:expr ) => { Quantity::Float{v: $x} } ( $x:expr, $y:expr ) => { Quantity::Float{v: $x, u: $y} }
} }
pub use crate::quantity::quantity::Quantity; pub use crate::quantity::quantity::Quantity;
@ -72,31 +75,31 @@ pub trait QuantBase:
Neg + Rem + Neg + Rem +
PartialEq + PartialOrd PartialEq + PartialOrd
{ {
fn fract(&self) -> Quantity; fn fract(&self) -> Option<Self>;
fn is_zero(&self) -> bool; fn is_zero(&self) -> bool;
fn is_negative(&self) -> bool; fn is_negative(&self) -> bool;
fn is_positive(&self) -> bool; fn is_positive(&self) -> bool;
fn exp(&self) -> Quantity; fn exp(&self) -> Option<Self>;
fn abs(&self) -> Quantity; fn abs(&self) -> Option<Self>;
fn floor(&self) -> Quantity; fn floor(&self) -> Option<Self>;
fn ceil(&self) -> Quantity; fn ceil(&self) -> Option<Self>;
fn round(&self) -> Quantity; fn round(&self) -> Option<Self>;
fn sin(&self) -> Quantity; fn sin(&self) -> Option<Self>;
fn cos(&self) -> Quantity; fn cos(&self) -> Option<Self>;
fn tan(&self) -> Quantity; fn tan(&self) -> Option<Self>;
fn asin(&self) -> Quantity; fn asin(&self) -> Option<Self>;
fn acos(&self) -> Quantity; fn acos(&self) -> Option<Self>;
fn atan(&self) -> Quantity; fn atan(&self) -> Option<Self>;
fn sinh(&self) -> Quantity; fn sinh(&self) -> Option<Self>;
fn cosh(&self) -> Quantity; fn cosh(&self) -> Option<Self>;
fn tanh(&self) -> Quantity; fn tanh(&self) -> Option<Self>;
fn asinh(&self) -> Quantity; fn asinh(&self) -> Option<Self>;
fn acosh(&self) -> Quantity; fn acosh(&self) -> Option<Self>;
fn atanh(&self) -> Quantity; fn atanh(&self) -> Option<Self>;
fn ln(&self) -> Quantity; fn ln(&self) -> Option<Self>;
fn log10(&self) -> Quantity; fn log10(&self) -> Option<Self>;
fn log2(&self) -> Quantity; fn log2(&self) -> Option<Self>;
fn log(&self, base: Quantity) -> Quantity; fn log(&self, base: Self) -> Option<Self>;
fn pow(&self, exp: Quantity) -> Quantity; fn pow(&self, exp: Self) -> Option<Self>;
} }

View File

@ -14,6 +14,8 @@ use crate::quantity::QuantBase;
use crate::quantity::RationalBase; use crate::quantity::RationalBase;
use crate::quantity::FloatBase; use crate::quantity::FloatBase;
use crate::quantity::Unit;
use crate::quantity::BaseUnit;
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(target_family = "unix")] { if #[cfg(target_family = "unix")] {
@ -23,8 +25,8 @@ cfg_if::cfg_if! {
#[derive(Debug)] #[derive(Debug)]
#[derive(Clone)] #[derive(Clone)]
pub enum Quantity { pub enum Quantity {
Rational{ v: RationalQ }, Rational{ v: RationalQ, u: Unit },
Float{ v: FloatQ } Float{ v: FloatQ, u: Unit }
} }
} else { } else {
use crate::quantity::f64q::F64Q; use crate::quantity::f64q::F64Q;
@ -32,8 +34,8 @@ cfg_if::cfg_if! {
#[derive(Debug)] #[derive(Debug)]
#[derive(Clone)] #[derive(Clone)]
pub enum Quantity { pub enum Quantity {
Rational{ v: F64Q }, Rational{ v: F64Q, u: Unit },
Float{ v: F64Q } Float{ v: F64Q, u: Unit }
} }
} }
} }
@ -44,31 +46,44 @@ impl Quantity {
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(target_family = "unix")] { if #[cfg(target_family = "unix")] {
pub fn new_rational(top: i64, bottom: i64) -> Quantity { pub fn new_rational(top: i64, bottom: i64) -> Quantity {
return wrap_rational!(RationalQ::from_frac(top, bottom)); return wrap_rational!(
RationalQ::from_frac(top, bottom),
Unit::new()
);
} }
pub fn new_float(v: f64) -> Quantity { pub fn new_float(v: f64) -> Quantity {
return wrap_float!(FloatQ::from_f64(v).unwrap()) return wrap_float!(
FloatQ::from_f64(v).unwrap(),
Unit::new()
)
} }
pub fn new_rational_from_string(s: &str) -> Option<Quantity> { pub fn new_rational_from_string(s: &str) -> Option<Quantity> {
let r = RationalQ::from_string(s); let r = RationalQ::from_string(s);
if r.is_none() { return None; } if r.is_none() { return None; }
return Some(wrap_rational!(r.unwrap())); return Some(wrap_rational!(
r.unwrap(),
Unit::new()
));
} }
pub fn new_float_from_string(s: &str) -> Option<Quantity> { pub fn new_float_from_string(s: &str) -> Option<Quantity> {
let v = FloatQ::from_string(s); let v = FloatQ::from_string(s);
if v.is_none() { return None; } if v.is_none() { return None; }
return Some(wrap_float!(v.unwrap())) return Some(wrap_float!(
v.unwrap(),
Unit::new()
))
} }
pub fn float_from_rat(r: &Quantity) -> Quantity { pub fn float_from_rat(r: &Quantity) -> Quantity {
match &r { match &r {
Quantity::Float { .. } => r.clone(), Quantity::Float { .. } => r.clone(),
Quantity::Rational { v } => wrap_float!( Quantity::Rational { v, u } => wrap_float!(
FloatQ::from(v.val.numer()).unwrap() / FloatQ::from(v.val.numer()).unwrap() /
FloatQ::from(v.val.denom()).unwrap() FloatQ::from(v.val.denom()).unwrap(),
u.clone()
) )
} }
} }
@ -102,61 +117,93 @@ impl Quantity {
} }
} }
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()},
Quantity::Rational { .. } => {panic!()} Quantity::Rational { .. } => {panic!()}
} }
} }
pub fn add_unit(&mut self, ui: BaseUnit, pi: f64) {
match self {
Quantity::Float { u, .. } => {u.insert(ui, pi)},
Quantity::Rational { u, .. } => {u.insert(ui, pi)}
}
}
} }
impl ToString for Quantity { impl ToString for Quantity {
fn to_string(&self) -> String { fn to_string(&self) -> String {
let mut n: String;
let u: &Unit;
match self { match self {
Quantity::Rational{v} => v.to_string(), Quantity::Rational{u:un, ..} => {
Quantity::Float{v} => v.to_string(), n = Quantity::float_from_rat(self).to_string();
} u = un
},
Quantity::Float{v, u:un} => {
n = v.to_string();
u = un;
},
};
//n.push(' ');
//n.push_str(&u.to_string());
n
} }
} }
macro_rules! quant_foward { macro_rules! quant_foward {
( $x:ident ) => { ( $x:ident ) => {
fn $x(&self) -> Quantity { pub fn $x(&self) -> Quantity {
match self { match self {
Quantity::Rational{v} => v.$x(), Quantity::Rational{v, u} => {
Quantity::Float{v} => v.$x(), if !u.unitless() { panic!() }
let r = v.$x();
if r.is_none() {
let v = Quantity::float_from_rat(self);
return v.$x();
} else {wrap_rational!(r.unwrap(), u.clone())}
},
Quantity::Float{v, u} => {
if !u.unitless() { panic!() }
wrap_float!(v.$x().unwrap(), u.clone())
},
} }
} }
} }
} }
impl QuantBase for Quantity { impl Quantity {
fn is_zero(&self) -> bool { pub fn is_zero(&self) -> bool {
match self { match self {
Quantity::Rational{v} => v.is_zero(), Quantity::Rational{v, .. } => v.is_zero(),
Quantity::Float{v} => v.is_zero(), Quantity::Float{v, .. } => v.is_zero(),
} }
} }
fn is_negative(&self) -> bool { pub fn is_negative(&self) -> bool {
match self { match self {
Quantity::Rational{v} => v.is_negative(), Quantity::Rational{v, .. } => v.is_negative(),
Quantity::Float{v} => v.is_negative(), Quantity::Float{v, .. } => v.is_negative(),
} }
} }
fn is_positive(&self) -> bool { pub fn is_positive(&self) -> bool {
match self { match self {
Quantity::Rational{v} => v.is_positive(), Quantity::Rational{v, .. } => v.is_positive(),
Quantity::Float{v} => v.is_positive(), Quantity::Float{v, .. } => v.is_positive(),
}
}
pub fn unitless(&self) -> bool {
match self {
Quantity::Rational{ u, .. } => u.unitless(),
Quantity::Float{ u, .. } => u.unitless(),
} }
} }
@ -182,16 +229,55 @@ impl QuantBase for Quantity {
quant_foward!(log10); quant_foward!(log10);
quant_foward!(log2); quant_foward!(log2);
fn log(&self, base: Quantity) -> Quantity { pub fn log(&self, base: Quantity) -> Quantity {
if !self.unitless() { panic!() }
match self { match self {
Quantity::Rational{v} => v.log(base), Quantity::Rational{u, .. } => {
Quantity::Float{v} => v.log(base), if !u.unitless() { panic!() }
Quantity::float_from_rat(self).log(Quantity::float_from_rat(&base))
},
Quantity::Float{u, .. } => {
if !u.unitless() { panic!() }
Quantity::float_from_rat(self).log(base)
},
} }
} }
fn pow(&self, base: Quantity) -> Quantity { pub fn pow(&self, base: Quantity) -> Quantity {
match self { match self {
Quantity::Rational{v} => v.pow(base), Quantity::Rational{u, .. } => {
Quantity::Float{v} => v.pow(base), let a = match Quantity::float_from_rat(self) {
Quantity::Rational{ .. } => panic!(),
Quantity::Float{v, .. } => v,
};
let b = match Quantity::float_from_rat(&base) {
Quantity::Rational{ .. } => panic!(),
Quantity::Float{v, .. } => v,
};
let mut nu = u.clone();
nu.pow(2f64);
wrap_float!(a.pow(b).unwrap(), nu)
},
Quantity::Float{u, .. } => {
if !u.unitless() { panic!() }
let a = match Quantity::float_from_rat(self) {
Quantity::Rational{ .. } => panic!(),
Quantity::Float{v, .. } => v,
};
let b = match Quantity::float_from_rat(&base) {
Quantity::Rational{ .. } => panic!(),
Quantity::Float{v, .. } => v,
};
let mut nu = u.clone();
nu.pow(2f64);
wrap_float!(a.pow(b).unwrap(), nu)
},
} }
} }
} }
@ -203,8 +289,8 @@ impl Neg for Quantity where {
fn neg(self) -> Self::Output { fn neg(self) -> Self::Output {
match self { match self {
Quantity::Float { v } => {wrap_float!(-v)}, Quantity::Float { v, u } => {wrap_float!(-v, u)},
Quantity::Rational { v } => {wrap_rational!(-v)}, Quantity::Rational { v, u } => {wrap_rational!(-v, u)},
} }
} }
} }
@ -214,10 +300,16 @@ 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}) => {wrap_float!(a.clone()+b.clone())}, (Quantity::Float{v:va,u:ua}, Quantity::Float{v:vb,u:ub}) => {
if ua != ub { panic!() }
wrap_float!(va.clone()+vb.clone(), ua.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}) => {wrap_rational!(a.clone()+b.clone())}, (Quantity::Rational{v:va,u:ua}, Quantity::Rational{v:vb,u:ub}) => {
if ua != ub { panic!() }
wrap_rational!(va.clone()+vb.clone(), ua.clone())
},
} }
} }
} }
@ -225,10 +317,16 @@ impl Add for Quantity {
impl AddAssign for Quantity where { impl AddAssign for Quantity where {
fn add_assign(&mut self, other: Self) { fn add_assign(&mut self, other: Self) {
match (&mut *self, &other) { match (&mut *self, &other) {
(Quantity::Float{v: a}, Quantity::Float{v: ref b}) => {*a += b.clone()}, (Quantity::Float{v:va,u:ua}, Quantity::Float{v:vb,u:ub}) => {
if ua != ub { panic!() }
*va += vb.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{ .. }) => {*self = Quantity::float_from_rat(self) + other }, (Quantity::Rational{ .. }, Quantity::Float{ .. }) => {*self = Quantity::float_from_rat(self) + other },
(Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {*a += b.clone()}, (Quantity::Rational{v:va,u:ua}, Quantity::Rational{v:vb,u:ub}) => {
if ua != ub { panic!() }
*va += vb.clone()
},
} }
} }
} }
@ -238,10 +336,16 @@ 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}) => {wrap_float!(a.clone()-b.clone())}, (Quantity::Float{v:va,u:ua}, Quantity::Float{v:vb,u:ub}) => {
if ua != ub { panic!() }
wrap_float!(va.clone()-vb.clone(), ua.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}) => {wrap_rational!(a.clone()-b.clone())}, (Quantity::Rational{v:va,u:ua}, Quantity::Rational{v:vb,u:ub}) => {
if ua != ub { panic!() }
wrap_rational!(va.clone()-vb.clone(), ua.clone())
},
} }
} }
} }
@ -249,10 +353,16 @@ impl Sub for Quantity {
impl SubAssign for Quantity where { impl SubAssign for Quantity where {
fn sub_assign(&mut self, other: Self) { fn sub_assign(&mut self, other: Self) {
match (&mut *self, &other) { match (&mut *self, &other) {
(Quantity::Float{v: a}, Quantity::Float{v: ref b}) => {*a -= b.clone()}, (Quantity::Float{v:va,u:ua}, Quantity::Float{v:vb,u:ub}) => {
if ua != ub { panic!() }
*va -= vb.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{ .. }) => {*self = Quantity::float_from_rat(self) - other }, (Quantity::Rational{ .. }, Quantity::Float{ .. }) => {*self = Quantity::float_from_rat(self) - other },
(Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {*a -= b.clone()}, (Quantity::Rational{v:va,u:ua}, Quantity::Rational{v:vb,u:ub}) => {
if ua != ub { panic!() }
*va -= vb.clone()
},
} }
} }
} }
@ -262,10 +372,16 @@ 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}) => {wrap_float!(a.clone()*b.clone())}, (Quantity::Float{v:va,u:ua}, Quantity::Float{v:vb,u:ub}) => {
let u = ua.clone()*ub.clone();
wrap_float!(va.clone()*vb.clone(), u)
},
(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}) => {wrap_rational!(a.clone()*b.clone())}, (Quantity::Rational{v:va,u:ua}, Quantity::Rational{v:vb,u:ub}) => {
let u = ua.clone()*ub.clone();
wrap_rational!(va.clone()*vb.clone(), u)
},
} }
} }
} }
@ -273,10 +389,16 @@ impl Mul for Quantity {
impl MulAssign for Quantity where { impl MulAssign for Quantity where {
fn mul_assign(&mut self, other: Self) { fn mul_assign(&mut self, other: Self) {
match (&mut *self, &other) { match (&mut *self, &other) {
(Quantity::Float{v: a}, Quantity::Float{v:b}) => {*a *= b.clone()}, (Quantity::Float{v:va,u:ua}, Quantity::Float{v:vb,u:ub}) => {
*ua *= ub.clone();
*va *= vb.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{ .. }) => {*self = Quantity::float_from_rat(self) * other }, (Quantity::Rational{ .. }, Quantity::Float{ .. }) => {*self = Quantity::float_from_rat(self) * other },
(Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {*a *= b.clone()}, (Quantity::Rational{v:va,u:ua}, Quantity::Rational{v:vb,u:ub}) => {
*ua *= ub.clone();
*va *= vb.clone()
},
} }
} }
} }
@ -286,10 +408,16 @@ 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}) => {wrap_float!(a.clone()/b.clone())}, (Quantity::Float{v:va,u:ua}, Quantity::Float{v:vb,u:ub}) => {
let u = ua.clone()/ub.clone();
wrap_float!(va.clone()/vb.clone(), u)
},
(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}) => {wrap_rational!(a.clone()/b.clone())}, (Quantity::Rational{v:va,u:ua}, Quantity::Rational{v:vb,u:ub}) => {
let u = ua.clone()/ub.clone();
wrap_rational!(va.clone()/vb.clone(), u)
},
} }
} }
} }
@ -297,10 +425,16 @@ impl Div for Quantity {
impl DivAssign for Quantity where { impl DivAssign for Quantity where {
fn div_assign(&mut self, other: Self) { fn div_assign(&mut self, other: Self) {
match (&mut *self, &other) { match (&mut *self, &other) {
(Quantity::Float{v: a}, Quantity::Float{v: ref b}) => {*a /= b.clone()}, (Quantity::Float{v:va,u:ua}, Quantity::Float{v:vb,u:ub}) => {
*ua /= ub.clone();
*va /= vb.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{ .. }) => {*self = Quantity::float_from_rat(self) / other }, (Quantity::Rational{ .. }, Quantity::Float{ .. }) => {*self = Quantity::float_from_rat(self) / other },
(Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {*a /= b.clone()}, (Quantity::Rational{v:va,u:ua}, Quantity::Rational{v:vb,u:ub}) => {
*ua /= ub.clone();
*va /= vb.clone()
}
} }
} }
} }
@ -310,10 +444,16 @@ 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}) => {wrap_float!(a.clone()%b.clone())}, (Quantity::Float{v:va,u:ua}, Quantity::Float{v:vb,u:ub}) => {
if ua != ub { panic!() }
wrap_float!(va.clone()%vb.clone(), ua.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}) => {wrap_rational!(a.clone()%b.clone())}, (Quantity::Rational{v:va,u:ua}, Quantity::Rational{v:vb,u:ub}) => {
if ua != ub { panic!() }
wrap_rational!(va.clone()%vb.clone(), ua.clone())
},
} }
} }
} }
@ -321,10 +461,10 @@ impl Rem<Quantity> for Quantity {
impl PartialEq for Quantity { impl PartialEq for Quantity {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
match (self, other) { match (self, other) {
(Quantity::Float{v:a}, Quantity::Float{v:b}) => {a == b}, (Quantity::Float{v:va,u:ua}, Quantity::Float{v:vb,u:ub}) => { if ua!=ub {false} else {va == vb} },
(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}) => {a == b}, (Quantity::Rational{v:va,u:ua}, Quantity::Rational{v:vb,u:ub}) => { if ua!=ub {false} else {va == vb} },
} }
} }
} }
@ -332,10 +472,16 @@ impl PartialEq for Quantity {
impl PartialOrd for Quantity { impl PartialOrd for Quantity {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (self, other) { match (self, other) {
(Quantity::Float{v:a}, Quantity::Float{v:b}) => {a.partial_cmp(b)}, (Quantity::Float{v:va,u:ua}, Quantity::Float{v:vb,u:ub}) => {
if ua != ub { panic!() }
va.partial_cmp(vb)
},
(Quantity::Float{ .. }, Quantity::Rational{ .. }) => {(*self).partial_cmp(&Quantity::float_from_rat(other))}, (Quantity::Float{ .. }, Quantity::Rational{ .. }) => {(*self).partial_cmp(&Quantity::float_from_rat(other))},
(Quantity::Rational{ .. }, Quantity::Float{ .. }) => {Quantity::float_from_rat(self).partial_cmp(other)}, (Quantity::Rational{ .. }, Quantity::Float{ .. }) => {Quantity::float_from_rat(self).partial_cmp(other)},
(Quantity::Rational{v:a}, Quantity::Rational{v:b}) => {a.partial_cmp(b)}, (Quantity::Rational{v:va,u:ua}, Quantity::Rational{v:vb,u:ub}) => {
if ua != ub { panic!() }
va.partial_cmp(vb)
},
} }
} }
} }

View File

@ -11,17 +11,13 @@ use std::ops::{
use std::cmp::Ordering; use std::cmp::Ordering;
use crate::quantity::wrap_rational;
use crate::quantity::Quantity;
use crate::quantity::QuantBase; use crate::quantity::QuantBase;
use crate::quantity::RationalBase; use crate::quantity::RationalBase;
macro_rules! float_foward { macro_rules! cant_do {
( $x:ident ) => { ( $x:ident ) => {
fn $x(&self) -> Quantity { fn $x(&self) -> Option<RationalQ> { None }
Quantity::float_from_rat(&wrap_rational!(self.clone())).$x()
}
} }
} }
@ -43,52 +39,46 @@ 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(&wrap_rational!(self.clone())); return self.val.to_string();
return v.to_string();
} }
} }
impl QuantBase for RationalQ { impl QuantBase for RationalQ {
fn fract(&self) -> Quantity { fn fract(&self) -> Option<RationalQ> {
wrap_rational!(RationalQ{val: self.val.clone().fract_floor(Integer::new()).0}) Some(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 {wrap_rational!(RationalQ{val: self.val.clone().abs()})} fn abs(&self) -> Option<RationalQ> {Some(RationalQ{val: self.val.clone().abs()})}
fn floor(&self) -> Quantity {wrap_rational!(RationalQ{val: self.val.clone().floor()})} fn floor(&self) -> Option<RationalQ> {Some(RationalQ{val: self.val.clone().floor()})}
fn ceil(&self) -> Quantity {wrap_rational!(RationalQ{val: self.val.clone().ceil()})} fn ceil(&self) -> Option<RationalQ> {Some(RationalQ{val: self.val.clone().ceil()})}
fn round(&self) -> Quantity {wrap_rational!(RationalQ{val: self.val.clone().round()})} fn round(&self) -> Option<RationalQ> {Some(RationalQ{val: self.val.clone().round()})}
float_foward!(sin); cant_do!(sin);
float_foward!(cos); cant_do!(cos);
float_foward!(tan); cant_do!(tan);
float_foward!(asin); cant_do!(asin);
float_foward!(acos); cant_do!(acos);
float_foward!(atan); cant_do!(atan);
float_foward!(sinh); cant_do!(sinh);
float_foward!(cosh); cant_do!(cosh);
float_foward!(tanh); cant_do!(tanh);
float_foward!(asinh); cant_do!(asinh);
float_foward!(acosh); cant_do!(acosh);
float_foward!(atanh); cant_do!(atanh);
float_foward!(exp); cant_do!(exp);
float_foward!(ln); cant_do!(ln);
float_foward!(log10); cant_do!(log10);
float_foward!(log2); cant_do!(log2);
fn log(&self, base: Quantity) -> Quantity { fn log(&self, _base: RationalQ) -> Option<RationalQ> { None }
Quantity::float_from_rat(&wrap_rational!(self.clone())).log10() / base.log10() fn pow(&self, _base: RationalQ) -> Option<RationalQ> { None }
}
fn pow(&self, base: Quantity) -> Quantity {
Quantity::float_from_rat(&wrap_rational!(self.clone())).pow(base)
}
} }

112
src/quantity/unit.rs Normal file
View File

@ -0,0 +1,112 @@
use std::{collections::HashMap, hash::Hash};
use std::ops::{
Mul, Div,
MulAssign, DivAssign
};
#[derive(Debug)]
#[derive(Hash)]
#[derive(Eq, PartialEq)]
#[derive(Copy, Clone)]
pub enum BaseUnit {
Second,
Meter,
Kilogram,
Ampere,
Kelvin,
Mole,
Candela
}
#[derive(Debug)]
#[derive(Clone)]
pub struct Unit {
// Unit, power.
pub val: HashMap<BaseUnit, f64>
}
impl ToString for Unit {
fn to_string(&self) -> String {
format!("{:?}", self)
}
}
impl Unit {
pub fn new() -> Unit {
return Unit{
val: HashMap::new()
}
}
pub fn unitless(&self) -> bool { self.val.len() == 0 }
pub fn insert(&mut self, u: BaseUnit, p: f64) {
match self.val.get_mut(&u) {
Some(i) => {
let n = *i + p;
if n == 0f64 {
self.val.remove(&u);
} else { *i = n; }
},
None => { self.val.insert(u, p); }
};
}
pub fn pow(&mut self, pwr: f64) {
for (_, p) in &mut self.val {
*p *= pwr;
}
}
}
impl PartialEq for Unit {
fn eq(&self, other: &Self) -> bool {
for (u, p) in &other.val {
match self.val.get(u) {
Some(i) => { if i != p { return false; } },
None => { return false; }
};
}
return true;
}
}
impl Mul for Unit {
type Output = Self;
fn mul(self, other: Self) -> Self::Output {
let mut o = self.clone();
for (u, p) in &other.val { o.insert(*u, *p); }
return o;
}
}
impl MulAssign for Unit where {
fn mul_assign(&mut self, other: Self) {
for (u, p) in &other.val { self.insert(*u, *p); }
}
}
impl Div for Unit {
type Output = Self;
fn div(self, other: Self) -> Self::Output {
let mut o = self.clone();
for (u, p) in &other.val { o.insert(*u, -*p); }
return o;
}
}
impl DivAssign for Unit where {
fn div_assign(&mut self, other: Self) {
for (u, p) in &other.val { self.insert(*u, -*p); }
}
}

View File

@ -2,7 +2,6 @@ use std::collections::VecDeque;
use crate::tokens::Token; use crate::tokens::Token;
use crate::tokens::Operator; use crate::tokens::Operator;
use crate::quantity::QuantBase;
#[derive(Debug)] #[derive(Debug)]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]

View File

@ -4,7 +4,6 @@ use std::cmp::Ordering;
use crate::tokens::Token; use crate::tokens::Token;
use crate::tokens::Function; use crate::tokens::Function;
use crate::quantity::Quantity; use crate::quantity::Quantity;
use crate::quantity::QuantBase;
/// Operator types, in order of increasing priority. /// Operator types, in order of increasing priority.
#[derive(Debug)] #[derive(Debug)]