Skip to content

Commit

Permalink
more built-in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vic1707 committed Oct 27, 2023
1 parent c4aabbf commit 16f81f0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/token/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub mod built_in_functions {
pub const TAN: Function = xprs_fn!("tan", f64::tan, 1);
pub const TANH: Function = xprs_fn!("tanh", f64::tanh, 1);
pub const ATAN: Function = xprs_fn!("atan", f64::atan, 1);
pub const ATAN2: Function = xprs_fn!("atan2", f64::atan2, 2);
pub const ATANH: Function = xprs_fn!("atanh", f64::atanh, 1);
// log
pub const LN: Function = xprs_fn!("ln", f64::ln, 1);
Expand All @@ -94,4 +95,13 @@ pub mod built_in_functions {
pub const MEAN: Function = xprs_fn!("mean", |args| {
args.iter().sum::<f64>() / args.len() as f64
});
pub const INVERT: Function = xprs_fn!("invert", f64::recip, 1);
pub const MIN: Function = xprs_fn!("min", |args| {
args.iter().fold(f64::INFINITY, |acc, &x| acc.min(x))
});
pub const MAX: Function = xprs_fn!("max", |args| {
args.iter().fold(f64::NEG_INFINITY, |acc, &x| acc.max(x))
});
pub const HYPOT: Function = xprs_fn!("hypot", f64::hypot, 2);
pub const FRACT: Function = xprs_fn!("fract", f64::fract, 1);
}
6 changes: 6 additions & 0 deletions src/token/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl<'a> From<&'a str> for Identifier<'a> {
"tan" => built_in_functions::TAN.into(),
"tanh" => built_in_functions::TANH.into(),
"atan" => built_in_functions::ATAN.into(),
"atan2" => built_in_functions::ATAN2.into(),
"atanh" => built_in_functions::ATANH.into(),
// log
"ln" => built_in_functions::LN.into(),
Expand All @@ -48,6 +49,11 @@ impl<'a> From<&'a str> for Identifier<'a> {
"trunc" => built_in_functions::TRUNC.into(),
"sum" => built_in_functions::SUM.into(),
"mean" => built_in_functions::MEAN.into(),
"invert" => built_in_functions::INVERT.into(),
"min" => built_in_functions::MIN.into(),
"max" => built_in_functions::MAX.into(),
"hypot" => built_in_functions::HYPOT.into(),
"fract" => built_in_functions::FRACT.into(),
/* Variables */
_ => Identifier::Variable(value),
}
Expand Down

0 comments on commit 16f81f0

Please sign in to comment.