0

I have a numpy array of floats of arbitrary precision (dtype=object). How do I convert this array of floats with arbitrary precision to array of integers? a = np.array([10662878767676765765756765768768787987, 1068768988765462575276572656879287982,..], dtype= object) b = a*821627.1218279279222972 a = b.astype(int) How do I round off b to get back array of integers without losing precision?

np.astype gives me the following error:

Overflowerror: Python int too large to convert to C long.

Thanks much for the help!

5
  • 1
    What do you actually mean by "floats of arbitrary precision"? Standard Python float is fixed-precision, as is anything floating-point in NumPy. Commented Oct 16, 2022 at 19:12
  • Do you want round or floor the items of the array? Commented Oct 16, 2022 at 19:20
  • "I have a numpy array of floats of arbitrary precision (dtype=object)." what does that mean? That seem contradictory. Commented Oct 16, 2022 at 19:29
  • This might help: stackoverflow.com/q/38314118/3216427 Commented Oct 16, 2022 at 19:45
  • 1
    Using np.int64 will allow for larger ints, but note that, as far as I know, NumPy won't let you have truly arbitrary precision ints. Using 64 bit ints moves the limit a lot higher, but there will still be a limit. Commented Oct 16, 2022 at 19:48

1 Answer 1

-1

The error arises when the size of the integer exceeds the maximum python int size (See article). If you are trying .astype(dtype=int), try using .astype(dtype=np.int64) instead. NOTE: Still there is an upper limit!

With "int"

A = np.array([1.3, 3.5, 0.2545345345345, 2354234235], dtype=object)
A.astype(dtype=int)

> OverflowError: Python int too large to convert to C long

With "np.int64

A.astype(dtype=np.int64)

> array([         1,          3,          0, 2354234235], dtype=int64)

The max int size in python is 2147483647

Values less than or equal to the max

A = np.array([2147483647], dtype=object)
A.astype(dtype=int)

> array([2147483647])

Values greater than the max

A = np.array([2147483647 + 1], dtype=object)
A.astype(dtype=int)

> OverflowError: Python int too large to convert to C long
Sign up to request clarification or add additional context in comments.

10 Comments

"The error arises when the size of the integer exceeds sys.maxsize" no? What does sys.maxsize have to do with this?
This is a great answer, with just one detail: sys.maxsize is about pointer sizes for your hardware. I'm not sure it's about int size.
See also docs.python.org/3/library/sys.html#sys.maxsize for the official definition of sys.maxsize.
Your article link is old 2015, and seems to be written from a Py 2 view point. In Py3, there isn't a (formal) distinction between Python int and long int. sys.maxsize gives the largest 64 bit int value, matching the numpy int64 dtype. For y=np.array(sys.maxsize), y+1 is a negaive, wrapped around. `y.item()+1 is a Python int, and does not wrap around.
You mentioned a "maximum integer size". There is none (ok, theoretically you will run out of address space, and practically, you'll run out of memory)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.