Added unit errors

pull/2/head
Mark 2023-04-10 18:47:00 -07:00
parent fbc0a6557e
commit 6ce4451db7
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 27 additions and 2 deletions

View File

@ -111,6 +111,7 @@ impl Quantity {
pub fn is_negative(&self) -> bool { self.v.is_negative() } pub fn is_negative(&self) -> bool { self.v.is_negative() }
pub fn is_positive(&self) -> bool { self.v.is_positive() } pub fn is_positive(&self) -> bool { self.v.is_positive() }
pub fn unitless(&self) -> bool { self.u.unitless() } pub fn unitless(&self) -> bool { self.u.unitless() }
pub fn unit(&self) -> &Unit { &self.u }
quant_foward!(fract); quant_foward!(fract);
quant_foward!(abs); quant_foward!(abs);

View File

@ -191,7 +191,7 @@ fn units() {
//good_expr("2 m", "rt (4m^2)"); //good_expr("2 m", "rt (4m^2)");
//bad_expr("m + s"); bad_expr("m + s");
//bad_expr("m ^ s"); bad_expr("m ^ s");
//bad_expr("m ^ pi"); //bad_expr("m ^ pi");
} }

View File

@ -70,6 +70,10 @@ impl Function {
let a = args[0].as_number(); let a = args[0].as_number();
let Token::Number(q) = a else {panic!()}; let Token::Number(q) = a else {panic!()};
if !q.unitless() {
return Err(());
}
match self { match self {
Function::Abs => { return Ok(Token::Number(q.abs())); }, Function::Abs => { return Ok(Token::Number(q.abs())); },
Function::Floor => { return Ok(Token::Number(q.floor())); }, Function::Floor => { return Ok(Token::Number(q.floor())); },

View File

@ -369,6 +369,11 @@ impl Operator{
while i < args.len() { while i < args.len() {
let j = args[i].as_number(); let j = args[i].as_number();
if let Token::Number(v) = j { if let Token::Number(v) = j {
if sum.unit() != v.unit() {
return Err(());
}
sum += v; sum += v;
} else { } else {
panic!(); panic!();
@ -399,6 +404,11 @@ impl Operator{
if let Token::Number(va) = a { if let Token::Number(va) = a {
if let Token::Number(vb) = b { if let Token::Number(vb) = b {
if !(va.unitless() && vb.unitless()) {
return Err(());
}
if vb <= Quantity::new_rational(1f64).unwrap() { return Err(()); } if vb <= Quantity::new_rational(1f64).unwrap() { return Err(()); }
if va.fract() != Quantity::new_rational(0f64).unwrap() { return Err(()); } if va.fract() != Quantity::new_rational(0f64).unwrap() { return Err(()); }
if vb.fract() != Quantity::new_rational(0f64).unwrap() { return Err(()); } if vb.fract() != Quantity::new_rational(0f64).unwrap() { return Err(()); }
@ -415,6 +425,11 @@ impl Operator{
if let Token::Number(va) = a { if let Token::Number(va) = a {
if let Token::Number(vb) = b { if let Token::Number(vb) = b {
if va.unit() != vb.unit() {
return Err(());
}
let p = va.pow(vb); let p = va.pow(vb);
if p.is_nan() {return Err(());} if p.is_nan() {return Err(());}
return Ok(Token::Number(p)); return Ok(Token::Number(p));
@ -427,6 +442,11 @@ impl Operator{
let args = args[0].as_number(); let args = args[0].as_number();
if let Token::Number(v) = args { if let Token::Number(v) = args {
if !v.unitless() {
return Err(());
}
if !v.fract().is_zero() { return Err(()); } if !v.fract().is_zero() { return Err(()); }
if v > Quantity::new_rational(50_000f64).unwrap() { return Err(()); } if v > Quantity::new_rational(50_000f64).unwrap() { return Err(()); }