Simple Math

There are often situations where you want to perform simple arithmetic operations, such as halving the image size neatly or adjusting the batch size. Let's look at the nodes for that.

Recommended Custom Nodes

Since it is a simple function, you can find as many implementations as you want if you look for them, but either of the following custom nodes is sufficient. (I wish it would be implemented as a core node...)

Here, we will explain using the Simple Math node of ComfyUI_essentials as an example.


Simple Math

Simple_Math.json

You can put numerical values in a, b, and c respectively. Using those variables, if you write like a * b - c, you can perform simple arithmetic.

Also, since this uses Python expressions as they are, slightly more advanced calculations can also be performed.

a // b       # Integer division (truncate decimal places)
a % b        # Modulo (remainder of division)
a ** b       # Exponentiation (power)
(a + b) * c  # Specify priority with parentheses

abs(a - b)   # Calculate absolute value
min(a, b)    # Return minimum value
max(a, b)    # Return maximum value
round(a / b) # Round off

(a > b) * 1  # Logical expression: Quantify condition (1 if a > b, otherwise 0)
(a == b) * 1 # Logical expression: Determine if equal
(a != b) * 1 # Logical expression: Determine if different

int type and float type

Numbers have "types". In ComfyUI, we mainly use two types: int and float.

  • int type: Integers only (e.g., 512, 32, 1)
    • Batch size, image resolution, etc.
  • float type: Can handle decimals (e.g., 0.7, 1.5, 24.0)
    • KSampler strength, video fps, etc.

You cannot connect to a node unless you input/output with the appropriate type. I can almost hear the retort "Why not just use float for everything?", but they are distinguished for calculation efficiency and precision... let's get used to it.

Type Conversion

By the way, passing a value through the Simple Math node once allows you to convert between intfloat.

Even if the input is float, if the output destination is int, it will automatically convert it.

Simple_Math_FloatInt.json

[Tip] Simple Calculation in Input Field

For simple calculations that don't warrant using a node, if you write the calculation formula directly in the input field, the calculated value will be input.


Power Puter (rgthree)

Using Power Puter added by rgthree-comfy allows for more complex processing, such as getting image sizes or using if statements, which is almost programming.

Power_Puter.json