Math Expressions
Math expressions let you compute values inline on either side of a comparison, or inside computed values, without needing to pre-compute fields in your data.
Syntax
Section titled “Syntax”Math expressions support full nested arithmetic:
<expression> <comparison> <expression>An expression can contain:
- properties
- literal numbers
- computed value references like
@total +,-,*,/,%,^- math functions:
floor,ceil,round,abs,sqrt,min,max,clamp - aggregate expressions:
sum of,average of,min of,max of - natural-language aliases
- parentheses
| Operator | Accepted forms |
|---|---|
| addition | +, plus, added to |
| subtraction | -, minus, subtracted by |
| multiplication | *, times, multiplied by |
| division | /, divided by |
| modulo | %, modulo, remainder of |
| power | ^, power of, to the power of |
Precedence
Section titled “Precedence”The engine applies normal arithmetic precedence:
- parentheses and math functions
- exponentiation
- multiplication, division, and modulo
- addition and subtraction
This means multiplication happens before addition:
__subtotal__ of **Order** + __tax__ of **Order** * 2 is greater than 100Use parentheses when you want a different grouping:
(__subtotal__ of **Order** + __tax__ of **Order**) * 2 is greater than 100Addition
Section titled “Addition”__subtotal__ of **Order** + __tax__ of **Order** is greater than 100Aliases also work:
__subtotal__ of **Order** plus __tax__ of **Order** is greater than 100Interactive Example
Mix a property with a literal:
Interactive Example
Subtraction
Section titled “Subtraction”__balance__ of **Account** - __pending__ of **Account** is at least 0Interactive Example
Multiplication
Section titled “Multiplication”__quantity__ of **Order** * __unit_price__ of **Order** is greater than 1000Interactive Example
Division
Section titled “Division”__revenue__ of **Store** / __expenses__ of **Store** is greater than 1.5Interactive Example
Modulo
Section titled “Modulo”Modulo returns the remainder after division. It shares precedence with * and /.
__count__ of **Order** % 2 is equal to 0Aliases also work:
__count__ of **Order** modulo 2 is equal to 0__count__ of **Order** remainder of 2 is equal to 0Interactive Example
Exponentiation raises the left side to the power of the right side.
__score__ of **Applicant** power of 2 is greater than 49Aliases also work:
__score__ of **Applicant** ^ 2 is greater than 49__score__ of **Applicant** to the power of 2 is greater than 49Interactive Example
Math Functions
Section titled “Math Functions”Math functions return transformed numbers. Unary functions accept one expression; min, max, and clamp accept multiple arguments.
| Function | Description |
|---|---|
floor of (...) | round down to the nearest integer |
ceil of (...) | round up to the nearest integer |
round of (...) | round to the nearest integer |
round X to N places | round to a fixed number of decimal places |
abs of (...) | absolute value |
sqrt(...) | square root |
min(...) | smallest argument |
max(...) | largest argument |
clamp(value, min, max) | bound a value within a range |
floor of
Section titled “floor of”floor of (__price__ of **Order** * 1.1) is equal to 11Interactive Example
ceil of
Section titled “ceil of”ceil of (__price__ of **Order** * 1.1) is equal to 12Interactive Example
round of
Section titled “round of”round of (__score__ of **User** / 3) is equal to 4Interactive Example
You can also use the natural form round X to N places:
round __score__ of **User** to 2 places is equal to 3.14abs of
Section titled “abs of”abs of (__balance__ of **Account** - __limit__ of **Account**) is less than 50Interactive Example
sqrt(81) is equal to 9min / max / clamp
Section titled “min / max / clamp”min(9, 4, 7) is equal to 4max(2, 9, 4) is equal to 9clamp(20, 0, 10) is equal to 10Aggregate Expressions In Math
Section titled “Aggregate Expressions In Math”sum of, average of, min of, and max of can be used as arithmetic expressions, not just directly in comparisons:
total := sum of __prices__ of **Order**.
average_price := average of __prices__ of **Order**.
A **Order** qualifies
if @total is greater than 50
and @average_price is greater than 10.These aggregate expressions also accept numeric strings in arrays, such as ["15", "25", "20"].
Math functions compose with the rest of the grammar:
Interactive Example
Nested Expressions
Section titled “Nested Expressions”You can nest expressions and put arithmetic on both sides of the comparison:
(__used__ of **Quota** + __buffer__ of **Quota**) * 2 is less than __limit__ of **Quota** * 3 - 1Interactive Example
Combining with Other Conditions
Section titled “Combining with Other Conditions”Math expressions can be combined with and and or like any other condition:
Interactive Example
Errors And Traces
Section titled “Errors And Traces”Every arithmetic step must resolve to numbers.
- Numeric strings such as
"12.5"are accepted in arithmetic. - Non-numeric operands return a type error that includes the failing expression.
- Division by zero returns an evaluation error that includes the full expression.
- Execution traces expose the evaluated left-hand expression path and the final right-hand value, so the UI can explain what was compared.
Example errors:
Arithmetic expression $.Applicant.status resolved to a non-numeric string "active", but numeric operands are requiredDivision by zero in arithmetic expression ($.Student.total / ($.Student.count - $.Student.count))Summary
Section titled “Summary”| Capability | Supported |
|---|---|
| literal numbers | yes |
| property references | yes |
| math on the left side | yes |
| math on the right side | yes |
| nested expressions | yes |
| parentheses | yes |
precedence (^ before *///% before +/-) | yes |
computed value references (@name) | yes |
aggregate expressions (sum of, average of, min of, max of) | yes |
math functions (floor, ceil, round, abs, sqrt, min, max, clamp) | yes |
| Operator | Accepted forms | Example |
|---|---|---|
| addition | +, plus, added to | __subtotal__ of **Order** plus __tax__ of **Order** is greater than 50 |
| subtraction | -, minus, subtracted by | __balance__ of **Account** minus __reserved__ of **Account** is at least 0 |
| multiplication | *, times, multiplied by | __qty__ of **Order** times __price__ of **Order** is greater than 100 |
| division | /, divided by | __revenue__ of **Store** divided by __costs__ of **Store** is greater than 1 |
| modulo | %, modulo, remainder of | __count__ of **Order** % 2 is equal to 0 |
| power | ^, power of, to the power of | __score__ of **Applicant** power of 2 is greater than 49 |
| Function | Example |
|---|---|
floor of | floor of (__price__ of **Order** * 1.1) is equal to 11 |
ceil of | ceil of (__price__ of **Order** * 1.1) is equal to 12 |
round of | round of (__score__ of **User** / 3) is equal to 4 |
round ... to ... places | round __score__ of **User** to 2 places is equal to 3.14 |
abs of | abs of (__balance__ of **Account** - __limit__ of **Account**) is less than 50 |
sqrt | sqrt(81) is equal to 9 |
min | min(9, 4, 7) is equal to 4 |
max | max(2, 9, 4) is equal to 9 |
clamp | clamp(20, 0, 10) is equal to 10 |