mirror of https://github.com/rm-dr/daisy
Added CtoF and FtoC
parent
819e444378
commit
b8bde580a3
|
@ -75,6 +75,10 @@ Daisy instead provides four functions (`fromCelsius`, `toCelsius`, `fromFahrenhe
|
||||||
- "from" functions take a scalar and return a value in Kelvin: `fromCelsius(0) = 273.15K`
|
- "from" functions take a scalar and return a value in Kelvin: `fromCelsius(0) = 273.15K`
|
||||||
- "to" functions take a value in Kelvin and return a scalar: `toCelsius(273.15 K) = 0`
|
- "to" functions take a value in Kelvin and return a scalar: `toCelsius(273.15 K) = 0`
|
||||||
|
|
||||||
|
Functions `FtoC` and `CtoF` are also provided:
|
||||||
|
- `FtoC(x) = toCelsius(fromFahrenheit(x))`
|
||||||
|
- `CtoF(x) = toFahrenheit(fromCelsius(x))`
|
||||||
|
|
||||||
|
|
||||||
## Multiplication Order
|
## Multiplication Order
|
||||||
|
|
||||||
|
|
|
@ -152,6 +152,9 @@ pub fn do_command(
|
||||||
" Fahrenheit to Kelvin [c]fromF, fromFahrenheit[n]\n",
|
" Fahrenheit to Kelvin [c]fromF, fromFahrenheit[n]\n",
|
||||||
" Kelvin to Fahrenheit [c]toF, toFahrenheit[n]\n",
|
" Kelvin to Fahrenheit [c]toF, toFahrenheit[n]\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
" Celsius to Fahrenheit [c]CtoF[n]\n",
|
||||||
|
" Fahrenheit to Celsius [c]FtoC[n]\n",
|
||||||
|
"\n",
|
||||||
" convert to base unit [c]tobase[n]\n",
|
" convert to base unit [c]tobase[n]\n",
|
||||||
" remove units [c]nounit[n]\n",
|
" remove units [c]nounit[n]\n",
|
||||||
"\n\n"
|
"\n\n"
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
use crate::parser::Expression;
|
use crate::parser::Expression;
|
||||||
use crate::parser::Function;
|
use crate::parser::Function;
|
||||||
use crate::parser::Operator;
|
use crate::parser::Operator;
|
||||||
|
@ -9,6 +11,7 @@ use crate::quantity::Quantity;
|
||||||
use crate::quantity::Scalar;
|
use crate::quantity::Scalar;
|
||||||
use crate::errors::DaisyError;
|
use crate::errors::DaisyError;
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
|
use super::evaluate;
|
||||||
|
|
||||||
// If unitless, do nothing
|
// If unitless, do nothing
|
||||||
// If compatible with radians, convert to radians and return unitless
|
// If compatible with radians, convert to radians and return unitless
|
||||||
|
@ -151,6 +154,33 @@ pub fn eval_function(context: &mut Context, g: &Expression) -> Result<Option<Exp
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
Function::CtoF => {
|
||||||
|
return Ok(evaluate(context,
|
||||||
|
&Expression::Operator(
|
||||||
|
*l + *loc,
|
||||||
|
Operator::Function(Function::ToFahrenheit),
|
||||||
|
VecDeque::from(vec![Expression::Operator(
|
||||||
|
*l + *loc,
|
||||||
|
Operator::Function(Function::FromCelsius),
|
||||||
|
VecDeque::from(vec![Expression::Quantity(*l, q.clone())])
|
||||||
|
)])
|
||||||
|
)
|
||||||
|
).ok());
|
||||||
|
},
|
||||||
|
|
||||||
|
Function::FtoC => {
|
||||||
|
return Ok(evaluate(context,
|
||||||
|
&Expression::Operator(
|
||||||
|
*l + *loc,
|
||||||
|
Operator::Function(Function::ToCelsius),
|
||||||
|
VecDeque::from(vec![Expression::Operator(
|
||||||
|
*l + *loc,
|
||||||
|
Operator::Function(Function::FromFahrenheit),
|
||||||
|
VecDeque::from(vec![Expression::Quantity(*l, q.clone())])
|
||||||
|
)])
|
||||||
|
)
|
||||||
|
).ok());
|
||||||
|
},
|
||||||
|
|
||||||
Function::ToCelsius => {
|
Function::ToCelsius => {
|
||||||
let mut k = Quantity::new_rational(1f64).unwrap();
|
let mut k = Quantity::new_rational(1f64).unwrap();
|
||||||
|
|
|
@ -37,7 +37,9 @@ pub enum Function {
|
||||||
FromCelsius,
|
FromCelsius,
|
||||||
ToCelsius,
|
ToCelsius,
|
||||||
FromFahrenheit,
|
FromFahrenheit,
|
||||||
ToFahrenheit
|
ToFahrenheit,
|
||||||
|
CtoF,
|
||||||
|
FtoC,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -74,6 +76,8 @@ impl ToString for Function {
|
||||||
Function::ToCelsius => {String::from("tocelsius") },
|
Function::ToCelsius => {String::from("tocelsius") },
|
||||||
Function::FromFahrenheit => { String::from("fromfahrenheit") },
|
Function::FromFahrenheit => { String::from("fromfahrenheit") },
|
||||||
Function::ToFahrenheit => { String::from("tofahrenheit") },
|
Function::ToFahrenheit => { String::from("tofahrenheit") },
|
||||||
|
Function::FtoC => { String::from("FtoC") },
|
||||||
|
Function::CtoF => { String::from("CtoF") },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,6 +130,11 @@ impl Function {
|
||||||
"fromF" => {Some(Function::FromFahrenheit)},
|
"fromF" => {Some(Function::FromFahrenheit)},
|
||||||
"fromfahrenheit" => {Some(Function::FromFahrenheit)},
|
"fromfahrenheit" => {Some(Function::FromFahrenheit)},
|
||||||
"fromFahrenheit" => {Some(Function::FromFahrenheit)},
|
"fromFahrenheit" => {Some(Function::FromFahrenheit)},
|
||||||
|
|
||||||
|
"FtoC" => {Some(Function::FtoC)},
|
||||||
|
"ftoc" => {Some(Function::FtoC)},
|
||||||
|
"ctof" => {Some(Function::CtoF)},
|
||||||
|
"CtoF" => {Some(Function::CtoF)},
|
||||||
_ => None
|
_ => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -232,5 +232,11 @@ fn functions() {
|
||||||
|
|
||||||
good_expr("2", "nounit(2 mm)");
|
good_expr("2", "nounit(2 mm)");
|
||||||
good_expr("2", "nounit(2 meter * second)");
|
good_expr("2", "nounit(2 meter * second)");
|
||||||
|
|
||||||
|
good_expr("37.778", "FtoC(100)");
|
||||||
|
good_expr("73.399", "CtoF(23)");
|
||||||
|
good_expr("-17.778", "FtoC(0)");
|
||||||
|
good_expr("31.999", "CtoF(0)");
|
||||||
|
|
||||||
//good_expr("5000 m²·g/(s²·A²)", "tobase(5H)");
|
//good_expr("5000 m²·g/(s²·A²)", "tobase(5H)");
|
||||||
}
|
}
|
Loading…
Reference in New Issue