-
-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trac #18358: a cython function that produces long given python input
I met twice the same problem which is the nedd for a standalone function that given a python objects return an associated long (if it makes sense). The following is not safe {{{ cdef long f(x): return x }}} since `f(2/3)` would return `0` (see e.g. #18278). Moreover, one could expect something that would be very fast for both Python int and Sage integers (see #18346). URL: http://trac.sagemath.org/18358 Reported by: vdelecroix Ticket author(s): Vincent Delecroix Reviewer(s): Jeroen Demeyer
- Loading branch information
Showing
10 changed files
with
93 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
r""" | ||
Fast conversion macro to long | ||
""" | ||
|
||
from libc.limits cimport LONG_MIN | ||
|
||
from cpython.int cimport PyInt_AS_LONG | ||
from cpython.long cimport PyLong_AsLong | ||
from cpython.number cimport PyNumber_Index | ||
|
||
from sage.libs.gmp.mpz cimport mpz_fits_slong_p, mpz_get_si | ||
|
||
from sage.rings.integer cimport Integer | ||
|
||
cdef inline long pyobject_to_long(x) except? LONG_MIN: | ||
r""" | ||
Given a Python object ``x`` cast it quickly to a C long. | ||
A ``TypeError`` is raised if the input can not be converted to an integer or | ||
an ``OverflowError`` is raised if it does not fit into a C long. | ||
TESTS: | ||
We test indirectly that ``Integer.__pow__`` works:: | ||
sage: a = 10 | ||
sage: a^10 | ||
10000000000 | ||
sage: a^(10r) | ||
10000000000 | ||
sage: a^(10l) | ||
10000000000 | ||
sage: a^(10/1) | ||
10000000000 | ||
sage: a^(2**258) | ||
Traceback (most recent call last): | ||
... | ||
RuntimeError: exponent must be at most 2147483647 # 32-bit | ||
RuntimeError: exponent must be at most 9223372036854775807 # 64-bit | ||
""" | ||
if isinstance(x, int): | ||
return PyInt_AS_LONG(x) | ||
elif type(x) is Integer: | ||
if mpz_fits_slong_p((<Integer>x).value): | ||
return mpz_get_si((<Integer>x).value) | ||
else: | ||
raise OverflowError("Sage Integer too large to convert to C long") | ||
elif isinstance(x, long): | ||
return PyLong_AsLong(x) | ||
|
||
return PyNumber_Index(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters