Assigning a variable NaN in python without numpy

后端 未结 6 1495
天涯浪人
天涯浪人 2020-12-02 15:00

Most languages have a NaN constant you can use to assign a variable the value NaN. Can python do this without using numpy?

6条回答
  •  鱼传尺愫
    2020-12-02 15:41

    A more consistent (and less opaque) way to generate inf and -inf is to again use float():

    >> positive_inf = float('inf')
    >> positive_inf
    inf
    >> negative_inf = float('-inf')
    >> negative_inf
    -inf
    

    Note that the size of a float varies depending on the architecture, so it probably best to avoid using magic numbers like 9e999, even if that is likely to work.

    import sys
    sys.float_info
    sys.float_info(max=1.7976931348623157e+308,
                   max_exp=1024, max_10_exp=308,
                   min=2.2250738585072014e-308, min_exp=-1021,
                   min_10_exp=-307, dig=15, mant_dig=53,
                   epsilon=2.220446049250313e-16, radix=2, rounds=1)
    

提交回复
热议问题