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!

sven
  • 51
  • 4
  • 1
    What do you actually mean by "floats of arbitrary precision"? Standard Python `float` is fixed-precision, as is anything floating-point in NumPy. – user2357112 Oct 16 '22 at 19:12
  • Do you want round or floor the items of the array? – naraghi Oct 16 '22 at 19:20
  • "I have a numpy array of floats of arbitrary precision (dtype=object)." what does that mean? That seem contradictory. – juanpa.arrivillaga Oct 16 '22 at 19:29
  • This might help: https://stackoverflow.com/q/38314118/3216427 – joanis Oct 16 '22 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. – joanis Oct 16 '22 at 19:48

1 Answers1

-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
Snehal Patel
  • 192
  • 10
  • "The error arises when the size of the integer exceeds sys.maxsize" no? What does `sys.maxsize` have to do with this? – juanpa.arrivillaga Oct 16 '22 at 19:30
  • 1
    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. – joanis Oct 16 '22 at 19:55
  • On my machine, `sys.maxsize` is 9223372036854775807, but ìnt` overflows on your example of 2354234235. – joanis Oct 16 '22 at 19:57
  • @joanis, good call. Let me revise the Answer. – Snehal Patel Oct 16 '22 at 19:57
  • 1
    See also https://docs.python.org/3/library/sys.html#sys.maxsize for the official definition of `sys.maxsize`. – joanis Oct 16 '22 at 19:59
  • 1
    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. – hpaulj Oct 16 '22 at 20:23
  • @juanpa.arrivillaga, I don't see where where `sys.maxsize` is in my Answer? – Snehal Patel Oct 16 '22 at 21:18
  • 1
    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) – juanpa.arrivillaga Oct 16 '22 at 21:18
  • Your comment was "sys.maxsize has nothing to do with any of this. Just remove it." So my question is where do you see `sys.maxsize`. – Snehal Patel Oct 16 '22 at 21:22
  • @juanpa.arrivillaga, without any justification for why the link & explanation are "totally wrong", I can't really address your concern. However, the linked article is explaining the behavior that I am demonstrating with the output of the code in my Answer. Specifically, the article states, "When the result of an operation would fall outside this range, the result is normally returned as a long integer (in some cases, the exception OverflowError is raised instead). " This is exactly what I am seeing when the value exceeds the upper limit stated in the article (2147483647). – Snehal Patel Oct 16 '22 at 21:39