Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG: Converting large integer to np.longdouble can raise ValueError #28722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More comprehensive testing
  • Loading branch information
Tontonio3 committed Apr 16, 2025
commit 90be12ee5d7c00ea021e246ddbc0198fca0487c1
2 changes: 1 addition & 1 deletion 2 numpy/_core/src/common/npy_longdouble.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ npy_longdouble _int_to_ld(int64_t *val, int exp, int sign) {
for (int i = 0; i < 3; i++) { mantissa[i] = (uint64_t)val[i]; }
npy_longdouble ld;
if (exp == 0) {
ld = mantissa[0];
ld = (npy_longdouble)sign * (npy_longdouble)mantissa[0];
} else if (exp == LDBL_MAX_EXP && mantissa[0] == 0) {
ld = (npy_longdouble)sign * ((npy_longdouble)mantissa[1] * powl(2.0L, (npy_longdouble)(exp - 64)) +
(npy_longdouble)mantissa[2] * powl(2.0L, (npy_longdouble)(exp - 128)));
Expand Down
12 changes: 8 additions & 4 deletions 12 numpy/_core/tests/test_longdouble.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,20 @@ def test_fromstring_foreign_value(self):
# 2^1023 + 2^1022 + ... + 2^970
# largest number that can be stored in a double without loosing precision
# Now gives an overflow error if te number is too big
@pytest.mark.parametrize("sign", [1, -1])
@pytest.mark.parametrize("int_val", [
2 ** 1023, 0, 10, 3, -(2 ** 1023), 15653215315])
def test_longdouble_from_int(int_val):
2 ** 1023, 0, 10, 3, 15653215315, 5511, 644861])
def test_longdouble_from_int(sign, int_val):
# for issue gh-9968
int_val *= sign
str_val = str(int_val)
assert np.longdouble(int_val) == np.longdouble(str_val)

@pytest.mark.parametrize("sign", [1, -1])
@pytest.mark.parametrize("int_val", [
1000, 1815361, 358165, 646153161, 1, 0])
def test_longdouble_from_int_rounding(int_val):
1000, 1815361, 358165, 646153161, 1])
def test_longdouble_from_int_rounding(sign, int_val):
int_val *= sign
flt_val = float(int_val)
assert np.isclose(np.longdouble(int_val), flt_val)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a minor comment in passing--for testing, usage of assert_allclose() will typically give you better error messages when there is a failure (since it is purpose-built for testing of course).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, will do


Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.