mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2026-05-06 15:22:31 +08:00
fix(number-convert): preserve int precision for large numbers
The previous implementation converted all inputs through float before producing the INT output (text -> float -> int). This loses precision for integers beyond 2^53, since IEEE 754 doubles cannot represent them exactly (e.g. "9007199254740993" became 9007199254740992). Fix by: - Attempting direct int(text) for string inputs, falling back to int(float_val) only for decimal/scientific strings - Preserving the original value for native int inputs instead of round-tripping through float - Moving the non-finite check before int conversion to properly reject inf/nan strings with a clear error message
This commit is contained in:
parent
b53b10ea61
commit
8bc08b0e70
@ -44,8 +44,13 @@ class NumberConvertNode(io.ComfyNode):
|
||||
def execute(cls, value) -> io.NodeOutput:
|
||||
if isinstance(value, bool):
|
||||
float_val = 1.0 if value else 0.0
|
||||
elif isinstance(value, (int, float)):
|
||||
int_val = 1 if value else 0
|
||||
elif isinstance(value, int):
|
||||
float_val = float(value)
|
||||
int_val = value
|
||||
elif isinstance(value, float):
|
||||
float_val = value
|
||||
int_val = int(value)
|
||||
elif isinstance(value, str):
|
||||
text = value.strip()
|
||||
if not text:
|
||||
@ -56,6 +61,14 @@ class NumberConvertNode(io.ComfyNode):
|
||||
raise ValueError(
|
||||
f"Cannot convert string to number: {value!r}"
|
||||
) from None
|
||||
if not math.isfinite(float_val):
|
||||
raise ValueError(
|
||||
f"Cannot convert non-finite value to number: {float_val}"
|
||||
)
|
||||
try:
|
||||
int_val = int(text)
|
||||
except ValueError:
|
||||
int_val = int(float_val)
|
||||
else:
|
||||
raise TypeError(
|
||||
f"Unsupported input type: {type(value).__name__}"
|
||||
@ -66,7 +79,7 @@ class NumberConvertNode(io.ComfyNode):
|
||||
f"Cannot convert non-finite value to number: {float_val}"
|
||||
)
|
||||
|
||||
return io.NodeOutput(float_val, int(float_val))
|
||||
return io.NodeOutput(float_val, int_val)
|
||||
|
||||
|
||||
class NumberConvertExtension(ComfyExtension):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user