From 7ed8fc7d09545402318aa5ce753bd07b631af441 Mon Sep 17 00:00:00 2001 From: Christopher White <18whitechristop@gmail.com> Date: Tue, 26 Sep 2023 21:13:47 -0700 Subject: [PATCH] Add max and min to expressions Signed-off-by: Christopher White <18whitechristop@gmail.com> --- .../java/tc/oc/pgm/util/math/Formula.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/util/src/main/java/tc/oc/pgm/util/math/Formula.java b/util/src/main/java/tc/oc/pgm/util/math/Formula.java index f96a26a2fb..ce04eee6df 100644 --- a/util/src/main/java/tc/oc/pgm/util/math/Formula.java +++ b/util/src/main/java/tc/oc/pgm/util/math/Formula.java @@ -29,6 +29,22 @@ public double apply(double... doubles) { } }; + Function MAX = + new Function("max", 2) { + @Override + public double apply(double... doubles) { + return Math.max(doubles[0], doubles[1]); + } + }; + + Function MIN = + new Function("min", 2) { + @Override + public double apply(double... doubles) { + return Math.min(doubles[0], doubles[1]); + } + }; + static >> Formula of( String expression, Set variables, Formula fallback) throws IllegalArgumentException { @@ -41,7 +57,10 @@ static Formula of(String expression, Set variables, ContextBuilde throws IllegalArgumentException { Expression exp = - new ExpressionBuilder(expression).variables(variables).functions(BOUND, RANDOM).build(); + new ExpressionBuilder(expression) + .variables(variables) + .functions(BOUND, RANDOM, MAX, MIN) + .build(); return new ExpFormula<>(exp, context); }