Nifty Math
Replace a tower of math nodes with one expression
- values
- int
- float
- boolean
Every ComfyUI graph eventually grows a little tower of math nodes: add two floats, multiply by a scale, clamp the result, feed it into a latent size. It works, but it's five nodes to express one formula. Nifty Math collapses that into a single node with a text field - type a * b + clamp(c, 0, 1) and you're done. If you've ever wished ComfyUI math were more like a calculator and less like plumbing, this is the node.
How it works
The node gives you up to 16 named inputs, a through p. Connect values to them, write an expression in the expression field using those letters as variables, and it evaluates. Unconnected slots default to 0, so a + b works the moment you connect two things.
Under the hood it's a Python eval - but a carefully sandboxed one. The author ships a whitelist of safe globals with __builtins__ disabled, so you get math functions, trig, and the listed helpers, not arbitrary code execution. That's the right instinct for a node that evaluates strings; you're still running whatever the workflow author typed, so only load expressions you trust.
What you can express:
- Arithmetic:
+ - * / // % **(including integer division and powers). - Comparisons:
a == b,a != b,a < b,a > b,a <= b,a >= b. - Boolean logic:
a and b,a or b,not a, plus ternary:a if a > 0 else b. - Functions:
abs,round,min,max,pow,sqrt,floor,ceil,log,log2,log10,exp, and full trig (sin,cos,tan,asin,acos,atan,atan2,radians,degrees,hypot). - Helpers:
clamp(v, lo, hi),lerp(a, b, t),remap(v, a, b, c, d)- the remap alone kills a lot of chained math nodes. - Constants:
pi,tau,e,inf.
The three outputs - and the trap
The result is cast to all three output types at once, so you can grab whichever you need:
- float - the actual result.
- int - the result cast to integer. Note: it truncates, it doesn't round.
2.9becomes2, not3. If you need proper rounding, wrap it:round(a / b). - boolean -
Falsewhen the result is0/0.0,Trueotherwise. Handy for feeding a switch.
Two edge-case behaviors from the source are worth remembering: division by zero returns 0 instead of erroring (a deliberate choice), and any other expression error raises a descriptive exception so you can see what went wrong.
Installing it
Part of the Nifty Nodes pack:
cd ComfyUI/custom_nodes
git clone https://github.com/Stibo/comfyui-nifty-nodes
or search "Nifty Nodes" in ComfyUI Manager, then restart. No models, no heavy dependencies - just the pack's own requirements.
Gotchas
The int-output truncation is the one that actually bites people - wire a latent dimension through and you'll get 511 instead of 512 if you forgot to round. And because slots default to 0, a typo'd variable name silently evaluates as zero rather than erroring; if a formula looks wrong, check for a misspelled letter first. It's the most direct math node in the pack, and for the price of remembering one expression syntax it deletes a lot of clutter from your graph.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| expression | STRING | a + b | Mathematical expression evaluated with the slot letters as variables. Supports: + - * / // % ** (power), comparisons (==, !=, <, >, <=, >=), boolean logic (and, or, not), ternary (x if cond else y), and functions: abs, round, min, max, pow, sqrt, floor, ceil, log, log2, log10, exp, sin, cos, tan, asin, acos, atan, atan2, radians, degrees, hypot, clamp(v,lo,hi), lerp(a,b,t), remap(v,a,b,c,d), and constants pi, tau, e, inf. Unconnected slots default to 0. |
| valuesopt | COMFY_AUTOGROW_V3 | Named variable inputs a–p. Connect values here to use them in the expression. Unconnected slots default to 0. |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| int | INT | — |
| float | FLOAT | — |
| boolean | BOOLEAN | — |