Skip to content

Commit

Permalink
feat: add max and min functions #13
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 7, 2022
1 parent 352105b commit 5af43c1
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions extensions/ext-computing/src/expr/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,24 @@ fn execute_func(func_name: &str, args: Vec<f64>) -> f64 {
"ceil" => args[0].ceil(),
"round" => args[0].round(),
"signum" => args[0].signum(),
"max" => args[0].max(args[1]),
"min" => args[0].min(args[1]),
"max" => {
let mut max = args[0];
for arg in args {
if arg > max {
max = arg;
}
}
max
},
"min" => {
let mut min = args[0];
for arg in args {
if arg < min {
min = arg;
}
}
min
},
"pow" => args[0].powf(args[1]),
"clamp" => args[0].max(args[1]).min(args[2]),
_ => panic!("Function not implemented: {}", func_name),
Expand Down Expand Up @@ -167,6 +183,17 @@ mod tests {
assert_eq!(parse("sqrt(1 - (3 / 3^2))", &vars), 0.816496580927726);
}

#[test]
fn max_min() {
assert_eq!(parse("max(1, 2)", &Default::default()), 2.0);
assert_eq!(parse("min(1, 2)", &Default::default()), 1.0);
assert_eq!(parse("max(1, 2, 3)", &Default::default()), 3.0);
assert_eq!(parse("min(1, 2, 3)", &Default::default()), 1.0);

let vars = BTreeMap::from_iter(vec![("x".to_string(), Instruction::Const(2.0))]);
assert_eq!(parse("1.2 + max(1, 2, 3, x)", &vars), 4.2);
}

#[test]
fn const_e() {
let vars2 = BTreeMap::from_iter(vec![("x".to_string(), Instruction::Const(2.0))]);
Expand Down

0 comments on commit 5af43c1

Please # to comment.