Python Forum
cannot convert float infinity to integer error despite rounding off floats
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cannot convert float infinity to integer error despite rounding off floats
#1

So here's the full error information
Error:
Traceback (most recent call last): File "<ipython-input-41-862883395604>", line 1, in <module> runfile('C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Intervention Functions.py', wdir='C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction') File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile execfile(filename, namespace) File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Intervention Functions.py", line 98, in <module> ax.plot(x, yFunction(initialPop, popGrowth), 'p-', lw=1, alpha=0.6, label='Major Graph') File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 1377, in plot self.autoscale_view(scalex=scalex, scaley=scaley) File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 1984, in autoscale_view y0, y1 = ylocator.view_limits(y0, y1) File "C:\Python27\lib\site-packages\matplotlib\ticker.py", line 1375, in view_limits return np.take(self.bin_boundaries(dmin, dmax), [0, -1]) File "C:\Python27\lib\site-packages\matplotlib\ticker.py", line 1347, in bin_boundaries extra_bins = int(divmod((best_vmax - vmax), step)[0]) OverflowError: cannot convert float infinity to integer
And here's the code:
def yFunction(initialPop, popGrowth):
    y=initialPop
    yList = np.array([])
    x=0
    while x < 400:
        yList = np.append(yList, y)
        y = (y * np.exp(popGrowth * (x/4))) - (newsDiscussion(1, 20, x, 0.0015, 0.003, 0.55)*y)-(Tax2(156, 160, x, 0.05775, 0.1155, 0.55)*y)-(Tax1(80, 84, x, 0.77, 0.154, 0.55)*y)-(runVeganuaryMain(x)*y)
        y = round(y, 5)
        #y = newsDiscussion(1, 20, x, 0.0015, 0.003, 0.55)+(Tax2(156, 160, x, 0.05775, 0.1155, 0.55)*y)+(Tax1(80, 84, x, 0.77, 0.154, 0.55)*y)+(runVeganuaryMain(x)*y)
        x = x + 0.1
    return yList
yList = yFunction(initialPop, popGrowth)
print(yList)
    


x= np.linspace(0, 400, num=4000)
ax.set_title('Decrease in People Who Consume Animals ')
ax.set_xlabel('$Quarter$')
ax.set_ylabel('$Population$')
ax.plot(x, yFunction(initialPop, popGrowth), 'p-', lw=1, alpha=0.6, label='Major Graph')
Can anyone tell me what's causing this error?

So I figured out how to round the variables, but the function I used returns a string. Easy fix right? Just use Int()? Apparently not. My program is giving me a new error.
def yFunction(initialPop, popGrowth):
    y=initialPop
    yList = np.array([])
    x=0
    while x < 400:
        yList = np.append(yList, y)
        y = (y * np.exp(popGrowth * (x/4))) - (newsDiscussion(1, 20, x, 0.0015, 0.003, 0.55)*y)-(Tax2(156, 160, x, 0.05775, 0.1155, 0.55)*y)-(Tax1(80, 84, x, 0.77, 0.154, 0.55)*y)-(runVeganuaryMain(x)*y)
        y = int(format(y, '.5f'))
        x = x + 0.1
    return yList
yList = yFunction(initialPop, popGrowth)
print(yList)
    


x= np.linspace(0, 400, num=4000)
ax.set_title('Decrease in People Who Consume Animals ')
ax.set_xlabel('$Quarter$')
ax.set_ylabel('$Population$')
ax.plot(x, yFunction(initialPop, popGrowth), 'p-', lw=1, alpha=0.6, label='Major Graph')
Error:
Traceback (most recent call last): File "<ipython-input-52-862883395604>", line 1, in <module> runfile('C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Intervention Functions.py', wdir='C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction') File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile execfile(filename, namespace) File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Intervention Functions.py", line 81, in <module> yList = yFunction(initialPop, popGrowth) File "C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Intervention Functions.py", line 78, in yFunction y = int(format(y, '.5f')) ValueError: invalid literal for int() with base 10: '326300000.00000'
Reply
#2
Let's take a step back and just focus on the one line. What do you what to happen here?
Output:
>>> y = 1 >>> int(format(y, '.5f')) Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> int(format(y, '.5f')) ValueError: invalid literal for int() with base 10: '1.00000'
Reply
#3
I want int(format()) to round off to 5 decimal places, but because it returns a string. I want it to be a integer, so I added the int()

Note, I've also been trying:
y = '{:0.3e}'.format(y)
y = float(y)
but that gives me the same error with float infinity

I think my problem might be that I'm getting 'nan' values in my array. I'm not sure how to fix this
def yFunction(initialPop, popGrowth):
    y= float(initialPop)
    yList = np.array([])
    x=0
    while x < 400:
        yList = np.append(yList, '{:0.9}'.format(y))
        y = (float(y) * np.exp(popGrowth * (x/4))) - (newsDiscussion(1, 20, x, 0.0015, 0.003, 0.55)*float(y))-(Tax2(156, 160, x, 0.05775, 0.1155, 0.55)*float(y))-(Tax1(80, 84, x, 0.77, 0.154, 0.55)*float(y))-(runVeganuaryMain(x)*float(y))
        y = '{:0.9}'.format(y)
        y = float(y)
        x = x + 0.1
    return yList
yList = yFunction(initialPop, popGrowth)
print(yList)
    


x= np.linspace(0, 400, num=4000)
ax.set_title('Decrease in People Who Consume Animals ')
ax.set_xlabel('$Quarter$')
ax.set_ylabel('$Population$')
ax.plot(x, yFunction(initialPop, popGrowth), 'p-', lw=1, alpha=0.6, label='Major Graph')
Here's the weird array
Output:
['3.263e+08' '3.263e+08' '3.26357107e+08' ..., 'nan' 'nan' 'nan']
Reply
#4
If you want more than 0 decimal places, you want a float, not an int. After that, your nans might still be an issue but there's a lot going on here so I don't want to speculate, though you're welcome to give an update after replacing "int" with "float".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 265 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  When is it safe to compare (==) two floats? Radical 4 701 Nov-12-2023, 11:53 AM
Last Post: PyDan
  need help rounding joseph202020 7 1,319 Feb-21-2023, 08:13 PM
Last Post: joseph202020
  convert string to float in list jacklee26 6 1,902 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  openpyxl convert data to float jacklee26 13 5,953 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  from numpy array to csv - rounding SchroedingersLion 6 2,165 Nov-14-2022, 09:09 PM
Last Post: deanhystad
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,528 Aug-02-2022, 01:12 PM
Last Post: deanhystad
  KivyMD App to APK Convert Error Nick_MC 0 2,322 Jul-18-2022, 08:59 AM
Last Post: Nick_MC
Star Infinity loop edek121 0 19,586 Jun-05-2022, 12:51 PM
Last Post: edek121
  Convert string to float problem vasik006 8 3,392 Jun-03-2022, 06:41 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020