mirror of https://github.com/rm-dr/daisy
Fixed mph/mpg behavior
parent
3294d4d5fb
commit
7382627041
|
@ -22,7 +22,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||
|
||||
[[package]]
|
||||
name = "daisy"
|
||||
version = "0.2.1"
|
||||
version = "0.2.2"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"rug",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "daisy"
|
||||
version = "0.2.1"
|
||||
version = "0.2.2"
|
||||
edition = "2021"
|
||||
|
||||
[profile.release]
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use crate::parser::Token;
|
||||
use crate::parser::Constant;
|
||||
use crate::quantity::Quantity;
|
||||
use crate::quantity::Unit;
|
||||
|
||||
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(
|
||||
"1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137"
|
||||
).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)
|
||||
},
|
||||
})
|
||||
}
|
|
@ -78,6 +78,8 @@ impl PreToken {
|
|||
"π"|"pi" => { Some(Constant::Pi)},
|
||||
"e" => { Some(Constant::E) },
|
||||
"phi"|"φ" => { Some(Constant::Phi) },
|
||||
"mpg" => { Some(Constant::MPG) },
|
||||
"mph" => { Some(Constant::MPH) },
|
||||
_ => { None }
|
||||
};
|
||||
|
||||
|
|
|
@ -1,17 +1,27 @@
|
|||
#[derive(Debug)]
|
||||
#[derive(Clone)]
|
||||
pub enum Constant {
|
||||
// Fake units
|
||||
MPG,
|
||||
MPH,
|
||||
|
||||
// Mathematics
|
||||
Pi,
|
||||
Phi,
|
||||
E
|
||||
E,
|
||||
}
|
||||
|
||||
impl Constant {
|
||||
pub fn to_string(&self) -> String {
|
||||
match self {
|
||||
// Fake units
|
||||
Constant::MPG => { String::from("mpg") },
|
||||
Constant::MPH => { String::from("mph") },
|
||||
|
||||
// Mathematics
|
||||
Constant::Pi => { String::from("π") },
|
||||
Constant::Phi => { String::from("φ") },
|
||||
Constant::E => { String::from("e") },
|
||||
Constant::E => { String::from("e") }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -39,8 +39,6 @@ pub enum UnitBase {
|
|||
// Area
|
||||
Barn,
|
||||
Hectare,
|
||||
MilesPerHour,
|
||||
MilesPerGallon,
|
||||
Acre,
|
||||
|
||||
|
||||
|
@ -190,8 +188,6 @@ macro_rules! fromstring_db {
|
|||
(UnitBase::JulianYear, "julianYears"),
|
||||
|
||||
// Misc
|
||||
(UnitBase::MilesPerHour, "mph"),
|
||||
(UnitBase::MilesPerGallon, "mpg"),
|
||||
(UnitBase::Barn, "b"),
|
||||
(UnitBase::Barn, "barn"),
|
||||
(UnitBase::Hectare, "ha"),
|
||||
|
@ -346,7 +342,7 @@ macro_rules! unit_db {
|
|||
),
|
||||
|
||||
UnitBase::Hour => $X!(
|
||||
UnitBase::Hour, "hour",
|
||||
UnitBase::Hour, "h",
|
||||
rational, "3600",
|
||||
(UnitBase::Second, 1f64)
|
||||
),
|
||||
|
@ -457,21 +453,6 @@ macro_rules! unit_db {
|
|||
(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, "b",
|
||||
rational, "1e-28",
|
||||
|
|
|
@ -206,5 +206,6 @@ fn basic_units() {
|
|||
fn complex_units() {
|
||||
good_expr("0.62137 mi", "1km to mi");
|
||||
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");
|
||||
}
|
Loading…
Reference in New Issue