How can I check for NaN values?

后端 未结 17 2112
盖世英雄少女心
盖世英雄少女心 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:54

    I actually just ran into this, but for me it was checking for nan, -inf, or inf. I just used

    if float('-inf') < float(num) < float('inf'):
    

    This is true for numbers, false for nan and both inf, and will raise an exception for things like strings or other types (which is probably a good thing). Also this does not require importing any libraries like math or numpy (numpy is so damn big it doubles the size of any compiled application).

提交回复
热议问题