Fixed mph/mpg behavior

pull/2/head
Mark 2023-06-11 15:05:17 -07:00
parent 3294d4d5fb
commit 7382627041
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
7 changed files with 42 additions and 25 deletions

2
Cargo.lock generated
View File

@ -22,7 +22,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "daisy" name = "daisy"
version = "0.2.1" version = "0.2.2"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"rug", "rug",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "daisy" name = "daisy"
version = "0.2.1" version = "0.2.2"
edition = "2021" edition = "2021"
[profile.release] [profile.release]

View File

@ -1,6 +1,7 @@
use crate::parser::Token; use crate::parser::Token;
use crate::parser::Constant; use crate::parser::Constant;
use crate::quantity::Quantity; use crate::quantity::Quantity;
use crate::quantity::Unit;
use super::EvalError; use super::EvalError;
@ -19,5 +20,27 @@ pub fn eval_constant(c: &Constant) -> Result<Token, EvalError> {
Constant::Phi => { Token::Quantity(Quantity::new_float_from_string( Constant::Phi => { Token::Quantity(Quantity::new_float_from_string(
"1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137" "1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137"
).unwrap()) }, ).unwrap()) },
Constant::MPG => {
let mut q = Quantity::new_float_from_string("1").unwrap();
q.set_unit(
(
Unit::from_string("mile").unwrap() /
Unit::from_string("gallon").unwrap()
).unit
);
Token::Quantity(q)
},
Constant::MPH => {
let mut q = Quantity::new_float_from_string("1").unwrap();
q.set_unit(
(
Unit::from_string("mile").unwrap() /
Unit::from_string("hour").unwrap()
).unit
);
Token::Quantity(q)
},
}) })
} }

View File

@ -78,6 +78,8 @@ impl PreToken {
"π"|"pi" => { Some(Constant::Pi)}, "π"|"pi" => { Some(Constant::Pi)},
"e" => { Some(Constant::E) }, "e" => { Some(Constant::E) },
"phi"|"φ" => { Some(Constant::Phi) }, "phi"|"φ" => { Some(Constant::Phi) },
"mpg" => { Some(Constant::MPG) },
"mph" => { Some(Constant::MPH) },
_ => { None } _ => { None }
}; };

View File

@ -1,17 +1,27 @@
#[derive(Debug)] #[derive(Debug)]
#[derive(Clone)] #[derive(Clone)]
pub enum Constant { pub enum Constant {
// Fake units
MPG,
MPH,
// Mathematics
Pi, Pi,
Phi, Phi,
E E,
} }
impl Constant { impl Constant {
pub fn to_string(&self) -> String { pub fn to_string(&self) -> String {
match self { match self {
// Fake units
Constant::MPG => { String::from("mpg") },
Constant::MPH => { String::from("mph") },
// Mathematics
Constant::Pi => { String::from("π") }, Constant::Pi => { String::from("π") },
Constant::Phi => { String::from("φ") }, Constant::Phi => { String::from("φ") },
Constant::E => { String::from("e") }, Constant::E => { String::from("e") }
} }
} }
} }

View File

@ -39,8 +39,6 @@ pub enum UnitBase {
// Area // Area
Barn, Barn,
Hectare, Hectare,
MilesPerHour,
MilesPerGallon,
Acre, Acre,
@ -190,8 +188,6 @@ macro_rules! fromstring_db {
(UnitBase::JulianYear, "julianYears"), (UnitBase::JulianYear, "julianYears"),
// Misc // Misc
(UnitBase::MilesPerHour, "mph"),
(UnitBase::MilesPerGallon, "mpg"),
(UnitBase::Barn, "b"), (UnitBase::Barn, "b"),
(UnitBase::Barn, "barn"), (UnitBase::Barn, "barn"),
(UnitBase::Hectare, "ha"), (UnitBase::Hectare, "ha"),
@ -346,7 +342,7 @@ macro_rules! unit_db {
), ),
UnitBase::Hour => $X!( UnitBase::Hour => $X!(
UnitBase::Hour, "hour", UnitBase::Hour, "h",
rational, "3600", rational, "3600",
(UnitBase::Second, 1f64) (UnitBase::Second, 1f64)
), ),
@ -457,21 +453,6 @@ macro_rules! unit_db {
(UnitBase::Meter, 1f64) (UnitBase::Meter, 1f64)
), ),
// Misc
UnitBase::MilesPerHour => $X!(
UnitBase::MilesPerHour, "mph",
float, "0.44704",
(UnitBase::Meter, 1f64),
(UnitBase::Second, -1f64)
),
UnitBase::MilesPerGallon => $X!(
UnitBase::MilesPerGallon, "mpg",
float, "425144",
(UnitBase::Meter, -2f64)
),
UnitBase::Barn => $X!( UnitBase::Barn => $X!(
UnitBase::Barn, "b", UnitBase::Barn, "b",
rational, "1e-28", rational, "1e-28",

View File

@ -206,5 +206,6 @@ fn basic_units() {
fn complex_units() { fn complex_units() {
good_expr("0.62137 mi", "1km to mi"); good_expr("0.62137 mi", "1km to mi");
good_expr("3280.8 ft", "1km to ft"); good_expr("3280.8 ft", "1km to ft");
good_expr("62.137 mph", "100 km/h to mph"); good_expr("62.137 mi/h", "100 km/h to mph");
good_expr("20 mi", "10 mph * 2 hours");
} }