Comfy-Org/ComfyUI · error · ValueError

Math Expression '{expression}' produced a result too large t

Error message

Math Expression '{expression}' produced a result too large to represent as a float: {result}

What it means

Raised by the Math Expression node when simple_eval returns a numeric value whose magnitude exceeds float range (e.g. a Python int too large for a 64-bit IEEE double). The node must convert the result to float for its FLOAT output, and float(huge_int) raises OverflowError, which is re-raised as ValueError with the offending expression and result.

Source

Thrown at comfy_extras/nodes_math.py:108

        cls, expression: str, values: io.Autogrow.Type
    ) -> io.NodeOutput:
        if not expression.strip():
            raise ValueError("Expression cannot be empty.")

        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

  1. Rewrite the expression to keep intermediate values in float range (e.g. replace 'a**b' with 'math.pow'-style float ops or reduce exponents).
  2. Compute the result in stages and clamp: wrap the final value in an expression like 'min(max(expr, -1e308), 1e308)'.
  3. If the huge value is legitimate, use the INT output path only by splitting the expression so the float conversion is never attempted on the oversized value.
  4. Catch ValueError at the caller if the expression is user-supplied and report it back to the UI.

Example fix

// before
expression = "9**9**9"
result = node.execute(expression)  # ValueError: result too large

// after
expression = "min(9**9**9, 1e300)"  # clamp inside the expression
result = node.execute(expression)
Defensive patterns

Strategy: try-catch

Try / catch

try:
    out = math_node.execute(expression)
except ValueError as e:
    if "too large to represent as a float" in str(e):
        raise UserFacingError(f"Expression overflows float range: {expression}") from None
    raise

Prevention

When it happens

Trigger: Evaluating an expression whose exact-integer arithmetic explodes, such as '9**9**9' or ' factorial(30) * 10**400', where simple_eval returns a giant Python int; float(result) then overflows.

Common situations: Exponent towers or factorial chains in a math node feeding a slider/float input; mixing big-integer intermediate steps (Python ints are unbounded) with a node contract that requires a finite float.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/b25fd244c78b6329. Report an issue: GitHub.