Comfy-Org/ComfyUI · error · ValueError
Math Expression '{expression}' produced a non-finite result:
Error message
Math Expression '{expression}' produced a non-finite result: {result} What it means
Raised by the Math Expression node when the evaluated result converts to a float but that float is inf or NaN. Python float arithmetic silently produces inf on overflow and NaN from operations like inf - inf or 0 * inf, so the node explicitly rejects non-finite values via math.isfinite before emitting them downstream.
Source
Thrown at comfy_extras/nodes_math.py:113
context: dict = dict(values)
context["values"] = list(values.values())
result = simple_eval(expression, names=context, functions=MATH_FUNCTIONS)
# bool check must come first because bool is a subclass of int in Python
if not isinstance(result, (int, float)):
raise ValueError(
f"Math Expression '{expression}' must evaluate to a numeric result, "
f"got {type(result).__name__}: {result!r}"
)
try:
float_result = float(result)
except OverflowError:
raise ValueError(
f"Math Expression '{expression}' produced a result too large to "
f"represent as a float: {result}"
) from None
if not math.isfinite(float_result):
raise ValueError(
f"Math Expression '{expression}' produced a non-finite result: {result}"
)
return io.NodeOutput(float_result, int(result), bool(result))
class MathExtension(ComfyExtension):
@override
async def get_node_list(self) -> list[type[io.ComfyNode]]:
return [MathExpressionNode]
async def comfy_entrypoint() -> MathExtension:
return MathExtension()
View on GitHub (pinned to 1c6d8d45b3)
Solutions
- Clamp the expression result with min/max against a sane range (e.g. 'min(max(expr, -1e30), 1e30)').
- Guard denominators: change 'a/b' to 'a/(b if b != 0 else 1)' or use a conditional so division by ~0 cannot produce inf.
- Reduce the magnitude of intermediate products by reordering operations (divide before multiplying).
- Check the input values feeding 'values' context; an upstream node may already be passing extreme numbers.
Example fix
// before expr = "x * 1e308 * 10" # -> inf, ValueError non-finite // after expr = "min(x * 1e308 * 10, 1e300)" # stays finite
Defensive patterns
Strategy: validation
Validate before calling
import math
from simpleeval import simple_eval
v = simple_eval(expression, names=context)
if isinstance(v, (int, float)) and not math.isfinite(float(v)):
raise UserFacingError('expression yields a non-finite value') Try / catch
try:
out = math_node.execute(expression)
except ValueError as e:
if 'non-finite' in str(e):
# clamp or substitute a safe default
out = 0.0
else:
raise Prevention
- Guard denominators (b != 0) inside expressions.
- Divide before multiplying to keep magnitudes small.
- Clamp results with min/max expressions.
When it happens
Trigger: Float expressions that overflow to inf, e.g. '1e308 * 10' or '10.0**400', or NaN-producing mixes like '1e308*10 - 1e308*10'; math functions from MATH_FUNCTIONS returning inf/nan (e.g. log(0.0) style results depending on the allowed function set).
Common situations: Scaling factors multiplied together until they exceed double range; division that tends to infinity because a denominator input is 0 after implicit float conversion; chained math nodes compounding magnitudes.
Related errors
- Keyframe times must be finite numbers in seconds; got '{valu
- Math Expression '{expression}' produced a result too large t
- Cannot convert non-finite value to number: {float_val}
- Exponent {exp} exceeds maximum allowed ({MAX_EXPONENT})
- Expression cannot be empty.
AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14).
Data as JSON: /api/errors/a3b9bca73bd5d2a4.
Report an issue: GitHub.