Formulas & Functions

    Build calculations using mathematical expressions, operators, and built-in functions.

    Syntax

    Reference connected variables by their variable name (the identifier, not the display name). Input ports are created automatically for each variable referenced in your expression.

    js

    // Calculate profit margin

    (revenue - cost) / revenue * 100

    This expression creates two input ports: revenue and cost.

    Operators

    +
    Additiona + b
    -
    Subtractiona - b
    *
    Multiplicationa * b
    /
    Divisiona / b
    ^
    Exponentiationa ^ 2
    %
    Moduloa % b
    ()
    Grouping(a + b) * c

    ** is also accepted for exponentiation.

    Constants

    PI
    3.14159...Circle constant π
    E
    2.71828...Euler's number e

    Functions

    Basic Math

    abs(x)Absolute valueabs(-5) → 5
    sqrt(x)Square rootsqrt(16) → 4
    pow(x, y)Powerpow(2, 3) → 8
    exp(x)Exponential (e^x)exp(1) → 2.718
    log(x)Natural logarithmlog(E) → 1

    Rounding

    floor(x)Round downfloor(3.7) → 3
    ceil(x)Round upceil(3.2) → 4
    round(x)Round to nearestround(3.5) → 4

    Comparison

    min(a, b, ...)Minimum valuemin(3, 1, 4) → 1
    max(a, b, ...)Maximum valuemax(3, 1, 4) → 4

    Trigonometry

    sin(x)Sine (radians)sin(PI/2) → 1
    cos(x)Cosine (radians)cos(0) → 1
    tan(x)Tangent (radians)tan(PI/4) → 1

    Best Practices

    Prevent division by zero

    Guard denominators using the max() function to ensure they never reach zero. This prevents errors when random samples produce extreme values.

    value / max(divisor, 0.0001)

    Protect logarithms

    Logarithm functions require positive arguments. Wrap inputs in max() to handle cases where random sampling might produce zero or negative values.

    log(max(x, 0.0001))

    Use parentheses for clarity

    Even when operator precedence handles the order correctly, explicit parentheses make your formulas easier to read and verify. This is especially important for complex expressions.

    (a * b) + (c * d)

    Automatic port generation

    When you type a variable name in your expression, an input port is automatically created for it. Connect other blocks to these ports to provide values. Renaming variables in the expression updates the ports accordingly.