How can I check for NaN values?

后端 未结 17 2247
盖世英雄少女心
盖世英雄少女心 2020-11-22 05:02

float(\'nan\') results in Nan (not a number). But how do I check for it? Should be very easy, but I cannot find it.

17条回答
  •  感动是毒
    2020-11-22 05:50

    Well I entered this post, because i've had some issues with the function:

    math.isnan()
    

    There are problem when you run this code:

    a = "hello"
    math.isnan(a)
    

    It raises exception. My solution for that is to make another check:

    def is_nan(x):
        return isinstance(x, float) and math.isnan(x)
    

提交回复
热议问题