fix: avoid ValueError/OverflowError on NaN/Inf with significant_digits=0 by gaoflow · Pull Request #604 · qlustered/deepdiff · GitHub
Skip to content

fix: avoid ValueError/OverflowError on NaN/Inf with significant_digits=0#604

Open
gaoflow wants to merge 3 commits into
qlustered:devfrom
gaoflow:bug-hunt
Open

fix: avoid ValueError/OverflowError on NaN/Inf with significant_digits=0#604
gaoflow wants to merge 3 commits into
qlustered:devfrom
gaoflow:bug-hunt

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 24, 2026

Copy link
Copy Markdown

Problem

DeepDiff crashes with ValueError or OverflowError when comparing NaN or Inf float values with significant_digits=0:

>>> from deepdiff import DeepDiff
>>> DeepDiff(float('nan'), float('nan'), significant_digits=0)
ValueError: cannot convert float NaN to integer

>>> DeepDiff(float('inf'), float('inf'), significant_digits=0)
OverflowError: cannot convert float infinity to integer

The crash also occurs when NaN/Inf values are nested inside dicts, lists, or other structures.

Root Cause

In number_to_string, when significant_digits=0, the code calls int(number) on the rounded value. For NaN and Inf, round() returns NaN/Inf unchanged, and int() raises because these values cannot be converted to integers.

Fix

Guard the int() conversion with math.isfinite() so that NaN and Inf values pass through without attempting integer conversion. Their string representations ("nan", "inf", "-inf") will compare correctly in the existing string-comparison path.

The fix is a one-line change: if significant_digits == 0 becomes if significant_digits == 0 and math.isfinite(number).

Additional Note

When significant_digits > 0, NaN and Inf do not crash because round(nan, n) returns NaN and the string formatting produces "nan" which compares equal to another "nan". This behavior is preserved.

seperman and others added 3 commits June 24, 2026 12:08
number_to_string called int() on non-finite float values (NaN, Inf,
-Inf) when significant_digits=0, raising ValueError or OverflowError.
Guard the int() conversion with math.isfinite() so that NaN and Inf
pass through as-is (their string representations compare correctly).

Fixes the crash:

    >>> DeepDiff(float('nan'), float('nan'), significant_digits=0)
    ValueError: cannot convert float NaN to integer

    >>> DeepDiff(float('inf'), float('inf'), significant_digits=0)
    OverflowError: cannot convert float infinity to integer
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.

2 participants