How to get NaN when I divide by zero

后端 未结 5 633
北海茫月
北海茫月 2020-12-08 08:54

When I do floating point division in Python, if I divide by zero, I get an exception:

>>> 1.0/0.0
Traceback (most recent call last):
  File \"

        
5条回答
  •  醉梦人生
    2020-12-08 09:38

    If i understand your problem properly then this should be the solution:

    try:
       1.0/0.0
    except:    
       return 'inf'
    

    you can modified it according to various python exception handling method available

提交回复
热议问题