ComfyUI Node

XMath

Arithmetic without the eval-node roulette

By Xz3r0-M·Created 8 months ago·Updated about 6 hours ago· 13
XMath
  • input_a
  • input_b
  • int_result
  • float_result
basic_a0.0
basic_b0.0
operationAddition (+)
use_input_atrue
use_input_btrue
swap_abfalse

Sooner or later every ComfyUI workflow needs arithmetic that isn't "plug a number into a widget." Batch sizes, resolution math, step counts derived from something else, CFG tweaks relative to a seed - that's XMath's lane. It does the six basic operations (add, subtract, multiply, divide, power, modulo) between two values and hands you both an integer and a float result, so you're never stuck converting types by hand. It's the "calculator node" of the pack, and it's refreshingly free of the eval-string footguns that plague math-in-text nodes.

How it works

Two values, a and b, combine under an operation (default Addition). Each value can come from a widget (basic_a, basic_b) or from an input port (input_a, input_b) - the ports are MatchType and accept INT or FLOAT independently, so you can mix a float from one node with an int from another without a converter. The use_input_a / use_input_b toggles (both default on) decide whether the port takes priority; with the toggle on and a port connected, the port wins, otherwise it falls back to the widget. swap_ab flips the operands, which is handy when you want B minus A without re-plugging.

The two outputs are the design's best feature:

  • float_result - the exact value with decimals (2.5 + 2 = 4.5).
  • int_result - the same result truncated toward zero (4). No rounding surprises, just truncation - the tooltip says it plainly.

Want 5 divided by 2 as an int? You get 2, not 3, and you knew that going in. That dual output saves you the "IntToFloat/FloatToInt conversion node" detour that most workflows accumulate.

Inputs that matter

  • basic_a / basic_b - the fallback widget values (range ±10 billion, step 0.1).
  • operation - Addition, Subtraction, Multiplication, Division, Power, Modulo.
  • use_input_a / use_input_b - port-precedence switches, both on by default.
  • swap_ab - swap operands.
  • input_a / input_b - optional INT/FLOAT ports.

Outputs: int_result and float_result.

Installing it

Part of ComfyUI-Xz3r0-Nodes. ComfyUI Manager: search ComfyUI-Xz3r0-Nodes, or:

cd ComfyUI/custom_nodes
git clone https://github.com/Xz3r0-M/ComfyUI-Xz3r0-Nodes.git
cd ComfyUI-Xz3r0-Nodes
pip install -r requirements.txt

Restart ComfyUI. Pure Python arithmetic - no dependencies.

Gotchas

Two things. First, division by zero or modulo by zero will throw - there's no silent Infinity in the outputs, so guard your inputs if they can be zero (a max(1, x) upstream or a check node is the usual fix). Second, remember the truncation semantics: int_result is truncation toward zero, not rounding, so if your mental model expects 3 from 7/2, the node will hand you 2. If you need rounding, chain it through a node that rounds before converting. It's a calculator, not a mathematician - simple, predictable, and that's exactly why it works.

Category♾️ Xz3r0/Workflow-Processing

Inputs (8)

NameTypeDefaultDescription
basic_aFLOAT0.0-10000000000–10000000000basic value A (FLOAT)
basic_bFLOAT0.0-10000000000–10000000000basic value B (FLOAT)
operationCOMBOAddition (+)Mathematical operation type
use_input_aBOOLEANtrueuse input value A (input_a takes precedence when enabled, fallbacks to basic_a if not connected to other node)
use_input_bBOOLEANtrueuse input value B (input_b takes precedence when enabled, fallbacks to basic_b if not connected to other node)
swap_abBOOLEANfalseswap a and b values
input_aoptCOMFY_MATCHTYPE_V3Optional numeric input A (allows INT or FLOAT independently from input B, takes priority when use_input_a is enabled)
input_boptCOMFY_MATCHTYPE_V3Optional numeric input B (allows INT or FLOAT independently from input A, takes priority when use_input_b is enabled)

Outputs (2)

NameTypeDescription
int_resultINTInteger result (truncated decimal part towards zero)
float_resultFLOATFloat result (exact value with decimal)