ComfyUI Node

formula

A math expression node that refuses to eval() you

By StableLlama·Created about a year ago·Updated 4 days ago· 48
formula
  • a
  • FLOAT
formula-pi() ** 2

Most "math expression" nodes in this ecosystem are a security incident waiting to happen, because the lazy way to evaluate a string like a * b + sqrt(c) is Python's eval(), and eval() on user input is how you get code execution inside your ComfyUI process. The formula node from Basic data handling is the version that does it properly: it tokenizes the string, parses it with a shunting-yard algorithm, and evaluates the result against an allowlist of functions. No eval, no arbitrary code. You can feel the difference in the error messages - this thing tells you exactly which character or function it choked on.

If you're doing any multi-variable math in a workflow - combining a CFG value, a step count, and a seed-derived number into one derived parameter - this is the node that keeps it in one place instead of a chain of five arithmetic nodes.

What you type

The formula input is a STRING with a default of -pi() ** 2 (which, thanks to the precedence rule below, means (-pi) ** 2 ≈ 9.87). You write a normal math expression:

  • Operators: + - * / // % ** and parentheses. // is integer division, ** is power.
  • Variables: single lowercase letters - a, b, c, and so on. They show up as input widgets on the node as you use them (a is there by default). There are no multi-letter variables; that's a deliberate trade-off so the parser stays safe.
  • Functions, called with parens: sin, cos, tan, asin, acos, atan, atan2(y,x), degrees, radians, sinh, cosh, tanh, asinh, acosh, atanh, exp, log, log10, log2, sqrt, pow(base,exp), plus abs, floor, ceil, round, min(a,b), max(a,b).
  • Constants, but only with empty parens: pi() and e(). A bare pi or e is not allowed - if you write pi the parser treats it as a variable name and asks for an input.

So (a + b) / 2 works, sin(a) * cos(b) works, pow(2, a) works, and 2pi does not - write 2 * pi().

The precedence trap

The author is upfront about this one: unary minus binds tighter than exponentiation. -a ** 2 parses as (-a) ** 2, not -(a ** 2). If you want the latter, write it with parens. This is the one place the node will silently give you a different number than a calculator, so when a formula looks wrong, check the minus sign first.

Inputs and output

  • formula - the string expression (the only required input).
  • a (and more letter inputs as you use them) - FLOAT/INT values for your variables.
  • FLOAT output - one number, ready to wire into anything numeric.

Installing it

Same story as the whole pack: Basic data handling is dependency-free with no models to download. ComfyUI Manager, search "Basic data handling", restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/StableLlama/ComfyUI-basic_data_handling

Restart and find formula under Basic/maths.

Real talk

This is the node from the pack I'd actually recommend to a beginner who wants one math node instead of six. It's safe, it's explicit, and the error messages teach you the grammar. Just remember the -a ** 2 thing and keep your variables to single letters.

CategoryBasic/maths

Inputs (2)

NameTypeDefaultDescription
formulaSTRING-pi() ** 2
aoptFLOAT,INT0

Outputs (1)

NameTypeDescription
FLOATFLOAT