Skip to content

fix(number-convert): preserve int precision for large numbers#13147

Merged
comfyanonymous merged 2 commits intomasterfrom
fix/number-convert-int-precision
Mar 25, 2026
Merged

fix(number-convert): preserve int precision for large numbers#13147
comfyanonymous merged 2 commits intomasterfrom
fix/number-convert-int-precision

Conversation

@dante01yoon
Copy link
Copy Markdown
Contributor

@dante01yoon dante01yoon commented Mar 25, 2026

Summary

Fixes precision loss in the Number Convert node when converting large integer strings to INT output.

Reported by: comfy — Slack thread
Original PR: #13041

Problem

The execute() method always routed through float before producing the INT output:

text → float(text) → int(float_val)

IEEE 754 double-precision floats can only represent integers exactly up to 2^53. For larger values, this round-trip silently loses precision:

Input Expected INT Actual INT (before fix)
"9007199254740993" 9007199254740993 9007199254740992
9007199254740993 (int) 9007199254740993 9007199254740992
스크린샷 2026-03-25 오후 2 24 37 스크린샷 2026-03-25 오후 2 25 37 스크린샷 2026-03-25 오후 2 29 01 스크린샷 2026-03-25 오후 2 29 32 스크린샷 2026-03-25 오후 2 30 00

Fix

  • String inputs: Try int(text) directly first; fall back to int(float_val) only for decimal/scientific notation strings
  • Int inputs: Preserve the original Python int instead of round-tripping through float
  • Non-finite check: Moved before int conversion so inf/nan strings still produce a clear error

Test plan

All 31 tests pass (was 24, added 7 new precision tests).

New test coverage

Test Input Validates
test_string_large_int_above_2_53 "9007199254740993" INT output == 2^53+1 exactly
test_string_large_negative_int_above_2_53 "-9007199254740993" Negative large int preserved
test_string_very_large_int str(2^63+42) Arbitrary-precision int from string
test_string_large_int_float_output_is_float str(2^53+1) FLOAT output is still float type
test_int_large_above_2_53 2^53+1 (native int) Native int passthrough preserved
test_int_large_negative_above_2_53 -(2^53+1) Negative native int preserved
test_int_very_large 2^100 Very large native int preserved

Existing regression coverage (all passing)

  • INT/FLOAT/BOOL/STRING basic conversions
  • String edge cases: whitespace, scientific notation, negative decimals
  • Error paths: empty string, non-numeric string, inf, nan, unsupported types
  • Type assertions: FLOAT output is float, INT output is int

Manual testing checklist

  • Load a workflow with Number Convert node
  • Connect a STRING input with value "9007199254740993" — verify INT output is 9007199254740993
  • Connect an INT input with value 9007199254740993 — verify INT output matches
  • Connect a FLOAT input with 3.14 — verify INT output is 3
  • Verify error dialogs for "inf", "nan", "", and "abc" inputs

🤖 Generated with Claude Code

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
Cover the reported precision loss scenario and related edge cases:
- String inputs beyond 2^53 (positive, negative, very large)
- Native int inputs beyond 2^53
- Decimal string fallback behavior
- Large scientific notation strings (1e18)
- Type assertion for FLOAT output of large int strings
@dante01yoon dante01yoon marked this pull request as ready for review March 25, 2026 05:30
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 1f6146ec-539f-4a5a-9d74-ff3ce7a62b96

📥 Commits

Reviewing files that changed from the base of the PR and between b53b10e and 30ebfa6.

📒 Files selected for processing (2)
  • comfy_extras/nodes_number_convert.py
  • tests-unit/comfy_extras_test/nodes_number_convert_test.py

📝 Walkthrough

Walkthrough

This pull request modifies the number conversion node to independently track integer and float values through distinct parsing paths for each input type (boolean, integer, float, string), rather than deriving the integer output solely from the float result. For string inputs, it attempts direct integer parsing before falling back to conversion from the parsed float. The accompanying test suite additions verify correct precision and conversion behavior for large numeric values, including integers exceeding typical floating-point precision limits and scientific notation edge cases.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 30.77% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately captures the main fix: preserving integer precision for large numbers in the number-convert node, matching the primary changeset focus.
Description check ✅ Passed The description comprehensively covers the problem (IEEE 754 precision loss above 2^53), the fix (direct int parsing for strings, preserving native ints), test coverage, and manual testing checklist—all directly related to the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@comfyanonymous comfyanonymous merged commit 6580a6b into master Mar 25, 2026
16 checks passed
@comfyanonymous comfyanonymous deleted the fix/number-convert-int-precision branch March 25, 2026 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants