From d2e22555aa4406a81252360b3389a25ef80b376b Mon Sep 17 00:00:00 2001 From: kasemir Date: Wed, 13 May 2026 10:20:18 -0400 Subject: [PATCH 1/2] Formula: Add modulo 'a % b' operator --- .../org/csstudio/apputil/formula/Formula.java | 8 +++- .../apputil/formula/node/ModNode.java | 38 +++++++++++++++++++ .../apputil/formula/FormulaUnitTest.java | 14 ++++++- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 core/formula/src/main/java/org/csstudio/apputil/formula/node/ModNode.java diff --git a/core/formula/src/main/java/org/csstudio/apputil/formula/Formula.java b/core/formula/src/main/java/org/csstudio/apputil/formula/Formula.java index 2b5b8671f0..e515e7aeb6 100644 --- a/core/formula/src/main/java/org/csstudio/apputil/formula/Formula.java +++ b/core/formula/src/main/java/org/csstudio/apputil/formula/Formula.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010-2020 Oak Ridge National Laboratory. + * Copyright (c) 2010-2026 Oak Ridge National Laboratory. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -28,6 +28,7 @@ import org.csstudio.apputil.formula.node.LessThanNode; import org.csstudio.apputil.formula.node.MaxNode; import org.csstudio.apputil.formula.node.MinNode; +import org.csstudio.apputil.formula.node.ModNode; import org.csstudio.apputil.formula.node.MulNode; import org.csstudio.apputil.formula.node.NotEqualNode; import org.csstudio.apputil.formula.node.NotNode; @@ -434,6 +435,11 @@ else if (s.get() == '/') s.next(); n = new DivNode(n, parseUnary(s)); } + else if (s.get() == '%') + { + s.next(); + n = new ModNode(n, parseUnary(s)); + } else break; } return n; diff --git a/core/formula/src/main/java/org/csstudio/apputil/formula/node/ModNode.java b/core/formula/src/main/java/org/csstudio/apputil/formula/node/ModNode.java new file mode 100644 index 0000000000..feb9e80340 --- /dev/null +++ b/core/formula/src/main/java/org/csstudio/apputil/formula/node/ModNode.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2026 Oak Ridge National Laboratory. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + ******************************************************************************/ +package org.csstudio.apputil.formula.node; + +import org.csstudio.apputil.formula.Node; + +/** Modulo + * @author Kay Kasemir + */ +public class ModNode extends AbstractBinaryNode +{ + /** + * Constructor + * @param left , left node + * @param right , right node + */ + public ModNode(final Node left, final Node right) + { + super(left, right); + } + + @Override + protected double calc(final double a, final double b) + { + return a%b; + } + + @Override + public String toString() + { + return "(" + left + " % " + right + ")"; + } +} diff --git a/core/formula/src/test/java/org/csstudio/apputil/formula/FormulaUnitTest.java b/core/formula/src/test/java/org/csstudio/apputil/formula/FormulaUnitTest.java index 5ce412dd28..bbd7337e9e 100644 --- a/core/formula/src/test/java/org/csstudio/apputil/formula/FormulaUnitTest.java +++ b/core/formula/src/test/java/org/csstudio/apputil/formula/FormulaUnitTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010-2020 Oak Ridge National Laboratory. + * Copyright (c) 2010-2026 Oak Ridge National Laboratory. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -81,6 +81,18 @@ public void testBasics() throws Exception { f = new Formula("-12/-3"); assertEquals(4.0, VTypeHelper.toDouble(f.eval()), epsilon); + f = new Formula("3%4"); + assertEquals(3.0, VTypeHelper.toDouble(f.eval()), epsilon); + + f = new Formula("4%4"); + assertEquals(0.0, VTypeHelper.toDouble(f.eval()), epsilon); + + f = new Formula("5%4"); + assertEquals(1.0, VTypeHelper.toDouble(f.eval()), epsilon); + + f = new Formula("-5%4"); + assertEquals(-1.0, VTypeHelper.toDouble(f.eval()), epsilon); + // Order, quotes f = new Formula("1 + 2 * 3 - 4"); assertEquals(3.0, VTypeHelper.toDouble(f.eval()), epsilon); From 5628f8170a1dc2e17454d243500ca3c060686952 Mon Sep 17 00:00:00 2001 From: kasemir Date: Wed, 13 May 2026 10:23:34 -0400 Subject: [PATCH 2/2] Mention modulo in doc --- core/formula/doc/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/core/formula/doc/index.rst b/core/formula/doc/index.rst index bf354a1afa..dc2a5fd190 100644 --- a/core/formula/doc/index.rst +++ b/core/formula/doc/index.rst @@ -16,6 +16,7 @@ You can use the following operators : | " **\*** " - Multiplication. | " **/** " - Division. | " **^** " - Power. +| " **%** " - Modulo. | **rnd(x)** - Return a floating point number between 0 and x. | **max(expression...)** - Return the maximum between all expressions. | **min(expression...)** - Return the minimum between all expressions.