Skip to content

Fix int(QuadPrecision) for NaN, Inf, and out-of-int64 values#104

Open
SwayamInSync wants to merge 2 commits into
numpy:mainfrom
SwayamInSync:fix-int-conversion-97
Open

Fix int(QuadPrecision) for NaN, Inf, and out-of-int64 values#104
SwayamInSync wants to merge 2 commits into
numpy:mainfrom
SwayamInSync:fix-int-conversion-97

Conversation

@SwayamInSync
Copy link
Copy Markdown
Member

closes #97

AI Disclosure:
Claude wrote the test cases

@SwayamInSync SwayamInSync changed the title Fix int(QuadPrecision) for NaN, Inf, and out-of-int64 values (#97) Fix int(QuadPrecision) for NaN, Inf, and out-of-int64 values May 19, 2026
Comment thread src/csrc/scalar_ops.cpp
// Route the longdouble backend through quad as as_integer_ratio does;
// the prior `(long long)longdouble_value` cast also saturated/UBed on
// NaN/Inf/out-of-range.
value = Sleef_cast_from_doubleq1((double)self->value.longdouble_value);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both this and the similar cast alluded to in the comment will lose precision on any platform with extended precision native long double.

Instead of doing this, you should write a utility function that checks for NaN and inf with std::isnan and std::isinf, which do support extended precision long double natively and then write the value to a string buffer, which you can parse however you need it.

Comment thread tests/test_quaddtype.py
assert int(QuadPrecision(str(n))) == n

@pytest.mark.parametrize("exponent", [40, 60, 80, 100])
def test_int_powers_of_two_far_above_int64(self, exponent):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and many other tests in this file aren't parametrized by the backend, which leads to missing the issue I pointed out above.

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.

[BUG] int(QuadPrecision('inf'|'nan'|'1e30')) silently returns INT64_MAX

2 participants