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

he 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?

+3
source share
3 answers

One of the four values ​​is valueWI, valueHI, valueWF, valueHFset to float infinity. Just wipe it to something reasonable, for example, for a general and completely local solution, change your call DrawLineto:

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

, , , - , - . , , , ! )

+3

, , , . , , NaN (Not a Number), . , .

, (, 1600 x 1200 ).

+1

OverflowError: unable to convert infinity float to integer

def round_int(x):
    if x == float("inf") or x == float("-inf"):
        return float('nan') # or x or return whatever makes sense
    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
+1
source

Source: https://habr.com/ru/post/1745671/


All Articles