ComfyUI Node

Simple Math

A calculator that speaks your graph's variables (and won't eval() your inputs)

By wenchengxiang·Created about a month ago·Updated a day ago· 1
Simple Math
  • a
  • b
  • c
  • INT
  • FLOAT
value

Simple Math is a text-box calculator with a superpower: the expression you type can reference the node's a, b, and c inputs, which themselves can come from wires anywhere in your graph. Type a * 0.8 and you get 80% of whatever a was - which could be a width from a resolution node, a steps value from a primitive, a batch count from a loader. It's the "turn a formula into a live node" tool, and it hands you both the rounded integer and the float result of every calculation.

It earns its place in any workflow that has derived numbers. Your base resolution node feeds a, and this node computes the hi-res pass dimensions. You want the negative-prompt conditioning scaled differently from the positive - that's a * 0.9. You need to double a batch, halve a CFG, cap a step count. Instead of typing the result of a calculation into a widget and hoping you did the math right once, you write the formula once and it recomputes every run. This is the plumbing-layer idea of "one source, many consumers" applied to arithmetic.

It's also safer than the usual "math node," which is worth knowing because math nodes are the classic supply-chain attack vector in ComfyUI - several packs have shipped a math node that was just a thin eval() wrapper. This one parses your expression into an AST and walks it, evaluating only against a hardcoded whitelist of operators and math functions. No eval, no arbitrary code execution. That's genuinely better than a lot of what's out there.

How it works

You type an expression into value - things like a + b, (a - c) * 2, sqrt(a), max(a, b) * 1.5, even comparisons like a > b (which return 1 or 0). The parser knows a, b, c (and pi, e, inf, nan), the usual operators (+ - * / // ** %, comparisons, and/or/not), and a whitelist of single- and double-argument math functions including abs, ceil, floor, sqrt, exp, trig functions, log, min, max, round, sum, and len. It even supports subscripting like a[0].

Two behaviors worth knowing before you rely on them:

  • Tensor inputs become shapes. If you wire an IMAGE or LATENT into a/b/c, the node replaces it with its .shape list. So a[0] on an image gives you the batch size, a[1] the height - a sneaky but handy way to compute resolutions from an actual tensor instead of retyping them.
  • Failures return 0.0, quietly. Divide by zero, a bad function call, a parse error - all come back as 0.0 rather than an exception. The upside is a crashing workflow; the downside is that a typo silently zeroes your math. Watch the outputs when you're first testing an expression.

The two outputs are the same result in two skins: INT (rounded) and FLOAT (exact). Wire whichever your consumer needs.

How to install it

Part of ComfyUI-Practical-Tools by wenchengxiang. ComfyUI Manager → search ComfyUI-Practical-Tools → install → restart, or:

cd ComfyUI/custom_nodes
git clone https://github.com/wenchengxiang/ComfyUI-Practical-Tools

Standard library plus what ComfyUI already ships; nothing to download.

Common issues

  • Silent zeros. The quiet-error behavior is the main trap. If your math suddenly returns 0, check for a typo, a divide-by-zero, or a function you assumed existed - it's a curated whitelist, not full Python, so before relying on a function, confirm it's actually in the list.
  • String inputs auto-coerce. If a comes in as a string, it's converted to float. That's usually what you want; it's surprising only when someone wires a word into it.
  • Comparisons return 1/0, not True/False. If you chain a comparison into a boolean consumer expecting a BOOLEAN socket, remember the output type here is INT/FLOAT.
CategoryPractical-Tools/utils

Inputs (4)

NameTypeDefaultDescription
valueSTRING
aopt*0
bopt*0
copt*0

Outputs (2)

NameTypeDescription
INTINT
FLOATFLOAT