aF.
aF.

Reputation: 66747

How to fix this python error? OverflowError: cannot convert float infinity to integer

it gives me this error:

Traceback (most recent call last):
  File "C:\Users\Public\SoundLog\Code\Código Python\SoundLog\Plugins\NoisePlugin.py", line 113, in onPaint
    dc.DrawLine(valueWI, valueHI, valueWF, valueHF)
  File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_gdi.py", line 3177, in DrawLine
    return _gdi_.DC_DrawLine(*args, **kwargs)
OverflowError: cannot convert float infinity to integer

How can I avoid this to happen?

Upvotes: 15

Views: 99574

Answers (4)

Sandwich
Sandwich

Reputation: 1

If your loop is in a while loop, there is no need to make another one. You can't do infinite actions for an infinity amount of times. In other words, infinity * infinity is impossible.

Upvotes: -1

Uki D. Lucas
Uki D. Lucas

Reputation: 566

OverflowError: cannot convert float infinity to integer

def round_int(x):
    if x in [float("-inf"),float("inf")]: return float("nan")
    return int(round(x))

# TEST
print(round_int(174.919753086))
print(round_int(0))
print(round_int(float("inf")))
print(round_int(float("-inf")))

175
0
nan
nan

Upvotes: 10

Alex Martelli
Alex Martelli

Reputation: 882691

One of the four values valueWI, valueHI, valueWF, valueHF is set to float infinity. Just truncate it to something reasonable, e.g., for a general and totally local solution, change your DrawLine call to:

ALOT = 1e6
vals = [max(min(x, ALOT), -ALOT) for x in (valueWI, valueHI, valueWF, valueHF)]
dc.DrawLine(*vals)

best, of course, would be to understand which of the values is infinity, and why -- and fix that. But, this preferable course is very application-dependent, and entirely depends on the code leading to the computation of those values, which you give us absolutely no clue about, so it's hard for us to offer very specific help about this preferable option!-)

Upvotes: 13

ChrisF
ChrisF

Reputation: 137188

You'll need to post some code to get a definitive answer, but I would guess that one of your float values is unset. As such it could hold any value such as NaN (Not a Number), but in this case it's set to infinity. This can't be cast to integer, hence the error.

It will be being cast to an integer, as ultimately the screen is an integer space (1600 x 1200 pixels for example).

Upvotes: 2

Related Questions