• Register
0 votes
2k 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 16 views
16 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 3 days ago Mashhoodch 9.1k points
0 votes
1 answer 502 views
502 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 3 views
3 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 1 day ago Code Learner 5.7k points
0 votes
1 answer 8 views
8 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 640 points
0 votes
1 answer 13 views
13 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 9.1k points
0 votes
1 answer 16 views
16 views
Problem: I am creating a program that reads a file and if the first line of the file is not blank, it reads the next four lines. Calculations are performed on those lines and then the next line is read. If that line is not empty it continues. However, I am getting this error: ... with base 10: ''. It is reading the first line but can't convert it to an integer. What can I do to fix this problem?
asked Jan 21 Mashhoodch 9.1k points
0 votes
1 answer 22 views
22 views
Problem: I am creating a program that reads a file and if the first line of the file is not blank, it reads the next four lines. Calculations are performed on those lines and then the next line is read. If that line is not empty it continues. However, I am getting this error: ... with base 10: ''. It is reading the first line but can't convert it to an integer. What can I do to fix this problem?
asked Jan 9 Mashhoodch 9.1k points
0 votes
1 answer 150 views
150 views
Problem : Every time I submitted Python code, I got "invalid literal for int() with base 10:"
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 23 views
23 views
Problem: Hi there! I just recently learned loops in Python that are a bit complex and tricky. I got an assignment from my teacher in which I have to write a program that takes a number as input from the user and prints the multiplication table of that number. I tried ... that the user does not enter a negative number. I do not know what the problem is and how to resolve it. Any help? Thanks.
asked Dec 2, 2020 Code Learner 5.7k points
0 votes
1 answer 15 views
15 views
Problem: I want to convert a character to integer in C#. I have tried Convert.ToInt32(), int32.Parse(), (int)char, but nothing works as desired. I do not want to convert my character into string first. I want to convert my character directly into integer. How I can do that? Is there any mechanism for doing so?
asked Oct 22, 2020 Code Learner 5.7k points