Math operation
One formula string instead of a pile of Add/Multiply nodes
- a
- b
- c
- d
- ui_widget
- int_result
- float_result
Stock ComfyUI math is a node per operation - one for add, one for multiply, one for divide - so a formula with three or four terms turns into a small maze of wires before you've computed anything useful. LF_MathOperation collapses that into a single node: write the formula as a string, wire in whatever variables it references, done.
The operation field takes an expression using up to four named variables - a, b, c, d - with the default shipped as a * b / c + d just to show the syntax. Each of those four variables is a separate optional input, typed as "any" (*), so you can feed them a number directly, or the output of something else in your graph that resolves to one. You don't have to use all four - if your formula only references a and b, leave c and d unconnected. Two outputs come back simultaneously: int_result and float_result, so whichever type your downstream node actually wants, it's already there without a separate cast step.
This is the node to reach for when you're deriving one value from a couple of others mid-graph - computing a resize target from an aspect ratio, scaling a step count by a percentage, that kind of thing - without breaking your workflow's visual flow into a cluster of arithmetic nodes.
The "any type" typing on a through d is more flexible than it first looks, too: since the inputs aren't locked to INT or FLOAT specifically, you can often wire in the output of a completely different kind of node - say something producing a numeric string - and let the formula engine handle coercion, rather than needing an explicit conversion node in between first. That's part of why this stays a single node instead of the usual stack: the type flexibility is doing some of the work the separate cast nodes would otherwise have to.
Installing it. ComfyUI Manager: search "LF Nodes," install, restart. Manual: cd ComfyUI/custom_nodes && git clone https://github.com/lucafoscili/lf-nodes, then restart. Get it from that repo - comfyui-lf, the one comfy.icu links, is the pack's original home and is now archived; the author's active work moved to lf-nodes. No models, no heavy dependencies, pure arithmetic.
Where people get tripped up. The formula string only understands the four variable names a, b, c, d - if you type something else expecting it to resolve, it won't; rename your wiring intent to match those letters, not the other way around. Division by an unconnected or zero-valued variable is the other classic gotcha - since these are optional inputs, an unwired c in a * b / c + d will error rather than silently skip that term, so make sure every variable your formula actually references is wired to something. And if you need a result type beyond int and float - say boolean logic or string formatting - this isn't the node; LF_ExtractString's typed outputs or LF_RandomBoolean cover other type conversions in this pack.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| operation | STRING | a * b / c + d | Math operation to execute. Use variables like 'a', 'b', 'c', 'd'. |
| aopt | * | Value or list of values for 'a'. | |
| bopt | * | Value or list of values for 'b'. | |
| copt | * | Value or list of values for 'c'. | |
| dopt | * | Value or list of values for 'd'. | |
| ui_widgetopt | KUL_CODE | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| int_result | INT | — |
| float_result | FLOAT | — |