Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/formula/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 + ")";
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down
Loading