Nodes/JNComfy/Math Operation
ComfyUI Node

Math Operation

One node for +, -, *, /, pow, sqrt, and the rest

By jn-jairo·Created 2 years ago·Updated 2 years ago· 5
Math Operation
  • a
  • b
  • *
operation

Arithmetic as a single dropdown instead of a different node per operation. JN_MathOperation is one of the utility-cluster nodes in JNComfy (jn-jairo/jn_comfyui) - the pack's answer to "I need to do some math between two node outputs and I don't want to bolt on ten separate add/subtract/multiply nodes."

Why you'd want it

Plenty of ComfyUI graphs need a bit of arithmetic that has nothing to do with diffusion itself - scaling a resolution by a factor, offsetting a seed, computing a step count from a percentage, deriving one number from another mid-graph. Core ComfyUI doesn't give you this; you either fake it with a text-concat-and-eval hack or reach for a utility pack. The community's other well-known answer to this problem is rgthree-comfy's Power Puter, which lets you write a python-like expression directly on the node. JN_MathOperation is the same itch scratched more narrowly - pick an operation from a list instead of writing an expression, which is easier to get right if you don't want to think about syntax.

How it works

a is required, b is optional - some operations are unary. The operation enum covers the basics (a + b, a - b, a * b, a / b, a // b, a % b, a ** b) plus a handful of single-operand math functions (a ** (1/b), abs(a), sqrt(a), exp(a), log(a), and one more beyond what's shown in the picker). Both operands are typed * - the generic wildcard type this pack's own patch enables - so you're not locked to feeding it INT/FLOAT nodes specifically; whatever numeric value is already flowing through your graph works.

The inputs and outputs that matter

  • a (any type) - required, your first number.
  • operation - the calculation to run. For binary ops (+, -, *, /, //, %, **) you need b; for unary ones (abs, sqrt, exp, log) you don't.
  • b (any type, optional) - your second operand, only needed for binary operations.

Output is a single *-typed value, so it chains cleanly into another JN_MathOperation, a JN_LogicOperation for comparison, or straight into an INT/FLOAT-expecting widget elsewhere.

How to install it

Via ComfyUI Manager: search "JNComfy", install, restart. Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/jn-jairo/jn_comfyui

then restart ComfyUI. This is a small, single-author pack with essentially no public discussion anywhere we could find - no reddit threads, no knowledge-base entry - so treat it as a solid personal-toolbox utility rather than something with a support community behind it.

Common issues & troubleshooting

Division by zero / domain errors. a / b with b = 0, or sqrt(a)/log(a) with a negative a, will error the same way plain Python math would - this node doesn't add guardrails on top of the underlying operation, so check your upstream values if a run fails here.

Result type surprises a downstream node. Because both the inputs and the output are the generic * type, this node won't stop you from, say, dividing two strings that happen to look numeric - if that fails, the error is coming from Python's own arithmetic rules on whatever you actually wired in, not from this node misbehaving.

You need a comparison, not a calculation. Wrong node for that - equals/greater-than/less-than live on JN_LogicOperation, which returns a BOOLEAN instead of a number.

CategoryJN/Primitive/Process

Inputs (3)

NameTypeDefaultDescription
a*
operationCOMBO13 options: a + b, a - b, a * b, a / b, a // b, a % b, +7
bopt*

Outputs (1)

NameTypeDescription
**