🧮 Math Expression
A real calculator node that won't run arbitrary code
- A
- B
- C
- D
- E
- F
- G
- H
- I
- J
- float
- int
- boolean
Core ComfyUI gives you a few individual math nodes and makes you chain them like a 1980s spreadsheet. The 🧮 Math Expression is the "stop chaining, just type the formula" answer: one text box, an expression like clamp(A * 2 + 5, 0, 100), and three outputs - float, int, and boolean - all computed from the same expression. It's the node that turns "five widgets of add/multiply" into one line of math.
The variable inputs are the clever part. The node declares ten optional sockets named A through J, and they're dynamic like the Switcher's: connect A, plug something in, and it adds the next letter. Any type goes in - it auto-converts numbers, parses numeric strings, and turns single-element tensors into scalars (a seed pulled off a tensor works fine). Non-numeric junk becomes 0.0 rather than erroring, which is forgiving but means "silent 0" is your most likely bug. In the expression you reference them by letter: A + B, max(A, 10), floor(sqrt(C)).
What you can write is a genuinely useful whitelist. Operators: + - * / // % **. Comparisons (==, !=, <, <=, >, >=) evaluate to 1.0 or 0.0, so A > B works as arithmetic. Functions: sin cos tan asin acos atan atan2 abs round ceil floor sqrt pow log log2 log10 min max, plus a clamp that's worth its weight - clamp(v, lo, hi) is exactly the safety you want on a scale or a CFG value. Constants: pi, e, inf. Ternary expressions work too: A if A > 10 else B.
Here's the part that matters for trust: it evaluates your expression by parsing it with Python's ast module and walking a hand-whitelisted set of node types - no eval(), no builtins, no attribute access. You can't inject code through the text box; the worst you can do is ask for unknown_var and get a 0.0 with the error message printed in the node's UI. That's the right security posture for a node that takes text input, and it's the main reason to prefer this over a generic "eval anything" node some packs ship.
Outputs to know: float is the raw result, int is int(result) (truncated, not rounded), and boolean is result != 0.0. Non-zero is True - so -0.5 is True, and a comparison that evaluated to 0.0 is False. If you need the answer as a plain number for a sampler step count, wire int; if you're feeding a scale, wire float.
Two behaviors to keep in mind. First, like everything in this pack it always re-runs (IS_CHANGED → NaN), so the result updates live as you edit the expression - which makes the inline header display (it shows the computed result) a handy mini-calculator while you're building. Second, there's a 64-entry expression cache, so a workflow that toggles between a handful of formulas doesn't re-parse every time; nothing to configure, just know it's there.
Installing is the usual pack dance:
cd ComfyUI/custom_nodes
git clone https://github.com/playboy-dongan/ComfyUI-Logic-nodes
or ComfyUI Manager → search "ComfyUI-Logic-nodes", then restart. No models, no dependencies beyond the Python standard library.
If your expression needs are one-liners, this is the node. The moment you want multi-line logic with variables and intermediate steps, you've outgrown it - but for "scale this seed by 1.5 and clamp it," there's nothing to complain about.
Inputs (11)
| Name | Type | Default | Description |
|---|---|---|---|
| expression | STRING | A + B | — |
| Aopt | * | — | |
| Bopt | * | — | |
| Copt | * | — | |
| Dopt | * | — | |
| Eopt | * | — | |
| Fopt | * | — | |
| Gopt | * | — | |
| Hopt | * | — | |
| Iopt | * | — | |
| Jopt | * | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| float | FLOAT | — |
| int | INT | — |
| boolean | BOOLEAN | — |