• Register
0 votes
2.9k views

Problem :

While I am running the python code on windows with python 3.5 64 bit. Getting below error.

 overflowerror: python int too large to convert to c long

6 5 3
6,930 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

The issue is because the numbers are greater than sys.maxsize:

>>> p = [sys.maxsize]
>>> preds[0] = p
>>> p = [sys.maxsize+1]
>>> preds[0] = p
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long

Please confirm this by checking:

>>> import sys >>> sys.maxsize 

To resolve the issue use the default float:

>>> preds = np.zeros((1, 3))

Further Readings:

https://github.com/pydicom/pydicom/issues/640

9 7 4
38,600 points
0 votes

In python 2, a Python int is equivalent to a C long data type. In python 3 an int is an arbitrary precision type but the numpy can still use it to represent the C type “long” while creating the array.

Platform:

The size of a C long is platform dependent. On the Windows operating system it is always 32 bits. On Linux operating systems it behaves as 32 bits on 32 bits systems and 64 bits on 64 bits systems.

Suggestion:

So it is suggested that choose a platform-independent data type. Probably this would be the most sensible choice in np.int64.

Example:

For example, you are getting this error when your numbers are greater than sys.maxsize

>>> p = [sys.maxsize]

>>> preds[0] = p

>>> p = [sys.maxsize+1]

>>> preds[0] = p

Error:

Traceback (most recent call last):

       File “<stdin>”, lin1, in <module>

OverflowError: Python int too large to convert to C long

Confirmation:

You can confirm this by using the following code;

>>> import sys

>>> sys.maxsize

2147487456

Solution:

To use the numbers with large precision, never use an int type which uses a bounded C integer behind the scenes. For this purpose use the default float value;

>>> preds = np.zeros((1, 3))

 

11 5 2
3,890 points

Related questions

0 votes
1 answer 496 views
496 views
Problem: I tried to calculate poisson distribution in python as below: p = math.pow(3,idx) depart = math.exp(-3) * p depart = depart / math.factorial(idx) idx ranges from 0 But I got OverflowError: long int too large to convert to float I tried to convert depart to float but no results.
asked Feb 21 Mashhoodch 13k points
1 vote
1 answer 20 views
20 views
Problem: How can I deal with this problem : python int too large to convert to c long
asked Mar 20 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 754 views
754 views
Problem: I am very new to Java and trying to learn arrays in Java. I am trying to enter one int and the other long ex: 1 and 1000000000 and now I am trying to create the array of size 1000000000. And after creating the array at each index of my array willing to store int val, ex: arr[ ... [1]);     long []arr;        arr = new long[y+1];         for(long j=0;j<=y;j++)         arr[j] = 4;     }
asked Jul 29, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 12 views
12 views
Problem I want to write a c++ source code which convert a string value to long long int
asked Apr 5 Subhendu 4.5k points
0 votes
1 answer 13 views
13 views
Problem: I tried to find out the solution on the web when I face this problem but I did not get any fruitful information that I can continue if you read the above question and understand what is the solution of this please cooperate with me- Thanks
asked Mar 23 rubelkhan 20.4k points
0 votes
1 answer 33 views
33 views
Problem Anyone can help me?How to convert int to char in c++
asked Mar 23 Subhendu 4.5k points
1 vote
1 answer 11 views
11 views
Problem: How to convert a string to int c++ ?
asked Mar 6 Raj Tyagi 15.6k points
0 votes
1 answer 20 views
20 views
Problem: Hi! I want to know if there is any way in C++ to convert an integer to a hexadecimal number? If yes, then please provide the demo code for better understanding. Commented code would be much appreciated. Thanks!
asked Feb 23 Code Learner 9.9k points
0 votes
1 answer 18 views
18 views
Problem: here is we are going to discuss the problem how to convert &ldquo;int&rdquo; into &ldquo;char&rdquo;
asked Feb 15 jasimchaudary 1.5k points
0 votes
1 answer 28 views
28 views
Problem: I am typing to get a sale amount (by input) to be multiplied by a defined sales tax (0.08) and then have it print the total amount (sales tax times sale amount). I run into this error. Anyone know what could be wrong or have any suggestions?
asked Feb 14 Mashhoodch 13k points