Math node

Math Expression

Evaluate arbitrary mathematical expressions written as text, with variables that automatically become input ports. The Math Expression node lets you write a formula once and have it computed every frame, replacing chains of individual Math nodes with a single, readable expression.

Outputs

PortType
Resultfloat

Overview

The Math Expression node provides a full expression evaluator with support for arithmetic, comparison, logical, and bitwise operators, the ternary conditional, built-in constants, and over 35 mathematical functions. You enter the expression in the Settings tab of the Inspector. Any unrecognized identifiers in the expression are treated as variables and automatically create corresponding float input ports on the node. When the expression is evaluated each frame, the current values from those input ports are substituted into the formula.

The expression is compiled into an Abstract Syntax Tree (AST) when you type it, and this compiled form is cached for efficient per-frame evaluation. A compile status indicator in the Settings panel shows whether the expression is valid (green), empty (gray), or contains errors (red with a description of the problem). If you make a typo, existing input ports are preserved so cables are not lost while you are editing.

Expression Syntax

Operators (by precedence, lowest to highest)

PrecedenceOperatorDescriptionExample
1? :Ternary conditionalx > 0 ? x : -x
2`\\`Logical OR`a \\b`
3&&Logical ANDa && b
4`\`Bitwise OR`a \b`
5&Bitwise ANDa & b
6== !=Equality / inequalityx == 1
7< > <= >=Relational comparisonx >= 0
8+ -Addition / subtractiona + b
9* / %Multiplication / division / moduloa * b % c
10^ or **Power (right-associative)x ^ 2 or x ** 2
11- ! ~Unary minus, logical NOT, bitwise NOT-x, !flag, ~bits
12()Parentheses / function callssin(x)

Constants

NameValueDescription
pi3.14159...The ratio of a circle's circumference to its diameter.
e2.71828...Euler's number, the base of natural logarithms.
true1.0Boolean true as a float.
false0.0Boolean false as a float.

Number Formats

  • Decimal: 42, 3.14, .5
  • Scientific notation: 1.5e3 (= 1500), 2.0e-4 (= 0.0002), 1E+2 (= 100)

Functions

Trigonometric

FunctionArgumentsDescription
sin(x)1Sine of x (radians).
cos(x)1Cosine of x (radians).
tan(x)1Tangent of x (radians).
asin(x)1Arcsine, returns radians.
acos(x)1Arccosine, returns radians.
atan(x)1Arctangent, returns radians.
atan2(y, x)2Two-argument arctangent, returns radians.

Hyperbolic

FunctionArgumentsDescription
sinh(x)1Hyperbolic sine.
cosh(x)1Hyperbolic cosine.
tanh(x)1Hyperbolic tangent.

Exponential and Logarithmic

FunctionArgumentsDescription
exp(x)1e raised to the power x.
ln(x)1Natural logarithm (base e). Returns 0 for x <= 0.
log(x)1Common logarithm (base 10). Returns 0 for x <= 0.
log2(x)1Logarithm base 2. Returns 0 for x <= 0.

Power and Root

FunctionArgumentsDescription
sqrt(x)1Square root. Returns 0 for negative x.
pow(x, y)2x raised to the power y. Same as x ^ y.
cbrt(x)1Cube root. Works for negative values.

Rounding

FunctionArgumentsDescription
floor(x)1Round down to nearest integer.
ceil(x)1Round up to nearest integer.
round(x)1Round to nearest integer.

Absolute Value and Sign

FunctionArgumentsDescription
abs(x)1Absolute value.
sign(x)1Returns 1 if x > 0, -1 if x < 0, 0 if x is 0.

Range and Clamping

FunctionArgumentsDescription
min(a, b)2Returns the smaller of a and b.
max(a, b)2Returns the larger of a and b.
clamp(x, lo, hi)3Constrains x to the range [lo, hi].
fmod(a, b)2Floating-point remainder. Returns 0 when b is 0.

Random

FunctionArgumentsDescription
rand()0Returns a random float in [0, 1). Different value each frame.

Step and Interpolation

FunctionArgumentsDescription
step(edge, x)2Returns 1 if x >= edge, otherwise 0.
smoothstep(edge0, edge1, x)3Hermite interpolation between 0 and 1 when x is between edge0 and edge1. Uses the formula t*t*(3-2*t).
mix(a, b, t)3Linear interpolation: a + (b - a) * t.
lerp(a, b, t)3Alias for mix. Linear interpolation: a + (b - a) * t.

Conversion

FunctionArgumentsDescription
radians(deg)1Converts degrees to radians.
degrees(rad)1Converts radians to degrees.

Fractional

FunctionArgumentsDescription
fract(x)1Fractional part: x - floor(x).

Comparison and Logical Operators

Comparison and logical operators return 1.0 for true and 0.0 for false. In logical contexts, any non-zero value is treated as true.

OperatorDescriptionExampleResult
==Equal1 == 11.0
!=Not equal1 != 21.0
<Less than1 < 21.0
>Greater than2 > 11.0
<=Less or equal1 <= 11.0
>=Greater or equal2 >= 30.0
&&Logical AND1 && 11.0
`\\`Logical OR`0 \\1`1.0
!Logical NOT (unary)!01.0

Bitwise Operators

Bitwise operators convert their float operands to 32-bit integers, perform the operation, and convert back to float.

OperatorDescriptionExample
&Bitwise AND5 & 3 = 1
`\`Bitwise OR`5 \3` = 7
~Bitwise NOT (unary)~0 = -1

Example Expressions

ExpressionDescription
a + bSimple addition of two variables. Creates input ports "a" and "b".
sin(t 2 pi) * amplitudeSine wave generator. Creates ports "amplitude" and "t".
clamp(x, 0, 1)Clamp x to the 0-1 range. Creates port "x".
x > threshold ? 1 : 0Binary threshold. Creates ports "threshold" and "x".
mix(colorA, colorB, t)Linear blend between two values. Creates ports "colorA", "colorB", "t".
smoothstep(0.3, 0.7, d)Smooth threshold with soft edges. Creates port "d".
a cos(angle) - b sin(angle)2D rotation (x component). Creates ports "a", "angle", "b".
pow(x, 2) + pow(y, 2)Distance squared from origin. Creates ports "x", "y".
floor(value * 10) / 10Quantize to nearest 0.1. Creates port "value".
amplitude sin(freq t + phase)Parameterized sine wave. Creates ports "amplitude", "freq", "phase", "t".

Settings

The expression is entered in the Settings tab of the Inspector panel (not the Input Parameters tab). The Settings tab shows:

  1. Expression text editor: A monospaced text field where you type your formula. Changes are compiled in real time as you type.
  2. Compile status indicator: A colored dot with a message.
  • Gray: "Enter an expression" (empty).
  • Green: "Compiled successfully" (valid expression).
  • Red: Error message describing what went wrong (syntax error, unknown function, wrong argument count, etc.).
  1. Variables list: Shows all variables detected in the expression, each with a green dot. These correspond to the dynamically created input ports.

Use Cases

  • Complex formulas in one node: Replace a chain of 5-10 Math nodes with a single readable expression like a sin(b t + c) + d.
  • Conditional logic: Use the ternary operator for if/else logic: temperature > 100 ? maxPower : temperature * scale.
  • Parameterized oscillators: Build complex oscillators: amplitude sin(2 pi frequency t + phase).
  • Distance calculations: Compute 2D distance: sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)).
  • Smooth thresholds: Use smoothstep for soft transitions: smoothstep(low, high, value).
  • Color math: Compute component values for HSL-to-RGB conversion or other color transformations.
  • Random variation: Add controlled randomness: baseValue + rand() * noiseAmount.

Practical Tips

  • Variable names must start with a letter or underscore and can contain letters, numbers, and underscores. Examples: x, myValue, _temp, input3.
  • Variable names are case-sensitive: Value and value create two separate input ports.
  • The names pi, e, true, and false are reserved constants and cannot be used as variable names. They will be replaced with their numeric values.
  • Function names (like sin, cos, max) are also reserved. If you use one as a variable name, it will be interpreted as a function call and cause an error if not followed by parentheses.
  • The operator is an alias for ^ (power). Both x 2 and x ^ 2 produce the same result.
  • The log function computes the common (base-10) logarithm, while ln computes the natural (base-e) logarithm. This differs from some programming languages where log means natural logarithm.
  • Division by zero and logarithm of non-positive numbers return 0 instead of producing errors.
  • If the final result is NaN or Infinity, the output is 0.
  • When you edit the expression and introduce a syntax error, existing input ports are preserved. This prevents cables from being disconnected while you are in the middle of editing.
  • Input ports appear in alphabetical order by variable name.

Open Math Expression in the interactive catalog →

Math Expression is one of 381 nodes in Grafit Pro.

Free download. Every feature for 7 days, no card. Then €99.99 once — or keep playing your projects as a Player.

More Math nodes