Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Tontonio3 wants to merge17 commits intonumpy:main
base:main
Choose a base branch
Loading
fromTontonio3:int_to_ld
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
17 commits
Select commitHold shift + click to select a range
94137f8
Added general handling and testing
Tontonio3Apr 15, 2025
9f5cee3
Improved overflow handling
Tontonio3Apr 15, 2025
13d6e0f
minor improvements with rounding and warning
Tontonio3Apr 16, 2025
90be12e
More comprehensive testing
Tontonio3Apr 16, 2025
3810619
Fixed testing, made code more robust
Tontonio3Apr 16, 2025
2e791ec
made code more robust
Tontonio3Apr 16, 2025
ce48cdd
Improved performance for some cases
Tontonio3Apr 21, 2025
5c1d383
Improved testing and comments
Tontonio3Apr 24, 2025
782bc2a
Improved error checking and fixed memory leak
Tontonio3May 2, 2025
491825d
Fixed ;
Tontonio3May 2, 2025
ac78367
Merge branch 'main' into int_to_ld
Tontonio3May 15, 2025
7aff0e2
Added release note and removed platform specific conversion
Tontonio3May 15, 2025
03c6ba6
Merge remote-tracking branch 'origin/int_to_ld' into int_to_ld
Tontonio3May 15, 2025
17f8555
Merge branch 'main' into int_to_ld
Tontonio3May 15, 2025
75c2b54
fix lint
Tontonio3May 15, 2025
6eb63eb
fix lint
Tontonio3May 15, 2025
b166974
lint
Tontonio3May 22, 2025
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
PrevPrevious commit
NextNext commit
Added release note and removed platform specific conversion
  • Loading branch information
@Tontonio3
Tontonio3 committedMay 15, 2025
commit7aff0e2d06a76f92b692cbb85ec2d43a486592b5
8 changes: 8 additions & 0 deletionsdoc/release/upcoming_changes/28639.improvement.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
Improved range and speed of conversion from a ``PyInt`` to a `np.longdouble`
----------------------------------------------------------------------------
To convert to a `np.longdouble` from a ``PyInt``, the conversion is now done
numerically instead of through a string. Which means that:

* For platforms that support 80/128 bit ``longdoubles``, it can handle numbers with more than 10^4300 digits
* If the number is too large to be converted (platform dependent) it now raises an ``OverflowError``, same behavior as Python's ``float`` function
* Improved performance
115 changes: 0 additions & 115 deletionsnumpy/_core/src/common/npy_longdouble.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -107,120 +107,6 @@ npy_longdouble _ldbl_ovfl_err(void) {
return -1;
}

// When Double is the same as the long double, and it is little endian
// It then (thanfully) follows IEEE 754, so it is possible to do bitwise operations
#if LDBL_MANT_DIG == 53 && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__

#define NPY_LGDB_SIGN_POS 0
#define NPY_LGDB_SIGN_NEG 1

typedef union {
uint64_t u64;
npy_longdouble d;
} uint64_double_union;

// Helper fucntion that uses bitwise opperations to transform a mantissa, exponent and sign
// into a double, by putting the bits in the correct memory locations
npy_longdouble _int_to_ld(int64_t *val, int e, int s) {
if (PyErr_Occurred()) { return -1; }
uint64_t mantissa[3];
uint64_t exp = (uint64_t)e;
uint64_t sign = (uint64_t)s;
for (int i = 0; i < 3; i++) { mantissa[i] = (uint64_t)val[i]; }
int foo = 0;
uint64_double_union value;

if (exp == 0) {
s = (sign == 1) ? -1 : 1;
return (npy_longdouble)s*(npy_longdouble)mantissa[0];
} else if (mantissa[0] == 0) {
mantissa[1] <<= 1;
exp--;
} else {
for (int i = 63; i >= 0; i--) {
if ((mantissa[0] >> i) & 1) {
foo = i;
break;
}
}
}
exp += foo;
mantissa[0] = (mantissa[1] >> foo) | (mantissa[0] << (64 - foo));
uint64_t guard = (mantissa[0] >> 11) & 1;
mantissa[0] >>= 12;
// Rounding (round to nearest even)
if (guard) {
mantissa[0]++;
if (mantissa[0] == 0x10000000000000) {
mantissa[0] = 0;
exp++;
}
}
if (exp >= DBL_MAX_EXP) {
return _ldbl_ovfl_err();
}
sign <<= 63;
exp += 1023;
exp <<= 52;
value.u64 = mantissa[0] | exp | sign;
return value.d;
}

// For 80bit platforms (x86 architectures), if it is little endian the layout of the longdouble is known
#elif LDBL_MANT_DIG == 64 && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__

#define NPY_LGDB_SIGN_POS 0
#define NPY_LGDB_SIGN_NEG 1

typedef union {
npy_longdouble ld;
uint64_t u64[2]; // Two 64-bit halves
} ld_128_union;

// Helper fucntion that uses bitwise opperations to transform a mantissa, exponent and sign
// into a long double, by putting the bits in the correct memory locations
npy_longdouble _int_to_ld(int64_t *val, int exp, int sign) {
if (PyErr_Occurred()) { return -1; }
uint64_t mantissa[3];
for (int i = 0; i < 3; i++) { mantissa[i] = (uint64_t)val[i]; }

ld_128_union value;
uint64_t guard;
int foo = 0;
if (exp == 0) {
sign = (sign == 1) ? -1 : 1;
return (npy_longdouble)sign*(npy_longdouble)mantissa[0];
} else if (mantissa[0] == 0) {
guard = (mantissa[2] >> 63) & 1;
} else {
for (int i = 63; i >= 0; i--) {
if ((mantissa[0] >> i) & 1) {
foo = i + 1;
break;
}
}
guard = (mantissa[1] >> (foo - 1)) & 1;
}
exp += foo - 1;
value.u64[0] = (mantissa[1] >> foo) | (mantissa[0] << (64 - foo));

// Rounding (round to nearest even)
if (guard) {
value.u64[0]++;
if (value.u64[0] == 0) {
value.u64[0] = 0x8000000000000000;
exp++;
}
}
if (exp >= LDBL_MAX_EXP) {
return _ldbl_ovfl_err();
}
value.u64[1] = (uint64_t)(exp + 16383) | (uint64_t)(sign << 15);
return value.ld;

}
#else

#define NPY_LGDB_SIGN_POS 1
#define NPY_LGDB_SIGN_NEG -1

Expand DownExpand Up@@ -258,7 +144,6 @@ npy_longdouble _int_to_ld(int64_t *val, int exp, int sign) {
}
return ld;
}
#endif

// Helper function that get the exponent and mantissa, this works on all platforms
// This function works by getting the bits of the number in increments of 64 bits
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp