I would like to represent a value as a 64bit signed long, such that values larger than (2**63)-1 are represented as negative, however Python long has infinite precision. Is there a 'quick' way for me to achieve this?
Join Stack Overflow to learn, share knowledge, and build your career.
|
|
||||
|
|
|
You could use
This is really only an option if you know for sure that a Edit: jorendorff's idea of defining a class for 64 bit numbers is appealing. Ideally you want to minimize the number of explicit class creations. Using
In this way the result of |
||||
|
|
|
Can you use numpy? It has an int64 type that does exactly what you want.
It's transparent to users, unlike the ctypes example, and it's coded in C so it'll be faster than rolling your own class in Python. Numpy may be bigger than the other solutions, but if you're doing numerical analysis, you will appreciate having it. |
|||
|
|
|
The quickest thing is probably to truncate the result to 64 bits yourself:
You can of course define your own numeric type that automatically does this every time you do any sort of arithmetic operation:
but that is not particularly "quick" to implement. |
||||
|
|
|
Have a look at the ctypes module, it is used to call foreign DLLs/libraries from python. There a some data types that correspond to C types, for example
|
|||
|
|