Skip to content

Commit d2e2255

Browse files
author
kasemir
committed
Formula: Add modulo 'a % b' operator
1 parent 5cf96a7 commit d2e2255

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

core/formula/src/main/java/org/csstudio/apputil/formula/Formula.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2010-2020 Oak Ridge National Laboratory.
2+
* Copyright (c) 2010-2026 Oak Ridge National Laboratory.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -28,6 +28,7 @@
2828
import org.csstudio.apputil.formula.node.LessThanNode;
2929
import org.csstudio.apputil.formula.node.MaxNode;
3030
import org.csstudio.apputil.formula.node.MinNode;
31+
import org.csstudio.apputil.formula.node.ModNode;
3132
import org.csstudio.apputil.formula.node.MulNode;
3233
import org.csstudio.apputil.formula.node.NotEqualNode;
3334
import org.csstudio.apputil.formula.node.NotNode;
@@ -434,6 +435,11 @@ else if (s.get() == '/')
434435
s.next();
435436
n = new DivNode(n, parseUnary(s));
436437
}
438+
else if (s.get() == '%')
439+
{
440+
s.next();
441+
n = new ModNode(n, parseUnary(s));
442+
}
437443
else break;
438444
}
439445
return n;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2026 Oak Ridge National Laboratory.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
******************************************************************************/
8+
package org.csstudio.apputil.formula.node;
9+
10+
import org.csstudio.apputil.formula.Node;
11+
12+
/** Modulo
13+
* @author Kay Kasemir
14+
*/
15+
public class ModNode extends AbstractBinaryNode
16+
{
17+
/**
18+
* Constructor
19+
* @param left , left node
20+
* @param right , right node
21+
*/
22+
public ModNode(final Node left, final Node right)
23+
{
24+
super(left, right);
25+
}
26+
27+
@Override
28+
protected double calc(final double a, final double b)
29+
{
30+
return a%b;
31+
}
32+
33+
@Override
34+
public String toString()
35+
{
36+
return "(" + left + " % " + right + ")";
37+
}
38+
}

core/formula/src/test/java/org/csstudio/apputil/formula/FormulaUnitTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2010-2020 Oak Ridge National Laboratory.
2+
* Copyright (c) 2010-2026 Oak Ridge National Laboratory.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -81,6 +81,18 @@ public void testBasics() throws Exception {
8181
f = new Formula("-12/-3");
8282
assertEquals(4.0, VTypeHelper.toDouble(f.eval()), epsilon);
8383

84+
f = new Formula("3%4");
85+
assertEquals(3.0, VTypeHelper.toDouble(f.eval()), epsilon);
86+
87+
f = new Formula("4%4");
88+
assertEquals(0.0, VTypeHelper.toDouble(f.eval()), epsilon);
89+
90+
f = new Formula("5%4");
91+
assertEquals(1.0, VTypeHelper.toDouble(f.eval()), epsilon);
92+
93+
f = new Formula("-5%4");
94+
assertEquals(-1.0, VTypeHelper.toDouble(f.eval()), epsilon);
95+
8496
// Order, quotes
8597
f = new Formula("1 + 2 * 3 - 4");
8698
assertEquals(3.0, VTypeHelper.toDouble(f.eval()), epsilon);

0 commit comments

Comments
 (0)