pull/2/head
Mark 2023-04-08 20:40:35 -07:00
parent fb9cc03bb9
commit e9d4ec0b12
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 23 additions and 14 deletions

View File

@ -98,13 +98,13 @@ impl PreToken {
// Units // Units
"m" => { "m" => {
let mut u = Quantity::new_rational_from_string("1").unwrap(); let mut u = Quantity::new_rational(1f64).unwrap();
u.add_unit(BaseUnit::Meter, 1f64); u.add_unit(BaseUnit::Meter, 1f64);
Token::Number(u) Token::Number(u)
}, },
"s" => { "s" => {
let mut u = Quantity::new_rational_from_string("1").unwrap(); let mut u = Quantity::new_rational(1f64).unwrap();
u.add_unit(BaseUnit::Second, 1f64); u.add_unit(BaseUnit::Second, 1f64);
Token::Number(u) Token::Number(u)
} }

View File

@ -25,9 +25,9 @@ pub struct Quantity {
impl ToString for Quantity { impl ToString for Quantity {
fn to_string(&self) -> String { fn to_string(&self) -> String {
let n = self.v.to_string(); let mut n = self.v.to_string();
//n.push(' '); n.push(' ');
//n.push_str(&u.to_string()); n.push_str(&self.u.to_string());
n n
} }
} }
@ -130,10 +130,9 @@ impl Quantity {
} }
pub fn pow(&self, pwr: Quantity) -> Quantity { pub fn pow(&self, pwr: Quantity) -> Quantity {
if !self.unitless() { panic!() }
Quantity { Quantity {
v: self.v.pow(pwr.v), v: self.v.pow(pwr.v),
u: self.u.clone() u: self.u.pow(2f64)
} }
} }
} }

View File

@ -58,10 +58,12 @@ impl Unit {
}; };
} }
pub fn pow(&mut self, pwr: f64) { pub fn pow(&self, pwr: f64) -> Unit {
for (_, p) in &mut self.val { let mut u = self.clone();
for (_, p) in &mut u.val {
*p *= pwr; *p *= pwr;
} };
return u;
} }
} }

View File

@ -328,19 +328,27 @@ impl Operator{
if let Token::Number(v) = args { if let Token::Number(v) = args {
if v.is_zero() { return Err(()); } if v.is_zero() { return Err(()); }
return Ok(Token::Number(Quantity::new_rational(1f64).unwrap()/v)); return Ok(Token::Number(
Quantity::new_rational(1f64).unwrap()/v
));
} else { panic!(); } } else { panic!(); }
}, },
Operator::Add => { Operator::Add => {
let mut sum = Quantity::new_rational(0f64).unwrap(); let mut sum;
for i in args.iter() { if let Token::Number(s) = args[0].as_number() {
let j = i.as_number(); sum = s;
} else {panic!()};
let mut i: usize = 1;
while i < args.len() {
let j = args[i].as_number();
if let Token::Number(v) = j { if let Token::Number(v) = j {
sum += v; sum += v;
} else { } else {
panic!(); panic!();
} }
i += 1;
} }
return Ok(Token::Number(sum)); return Ok(Token::Number(sum));
}, },