V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
ZzFoo
V2EX  ›  问与答

Python 为什么不定义 NaN 呢?

  •  
  •   ZzFoo · 2014-08-17 12:23:00 +08:00 · 7062 次点击
    这是一个创建于 3532 天前的主题,其中的信息可能已经有所发展或是发生改变。
    判断一个变量是不是数字,还要先判断它是不是int或者float,感觉很啰嗦
    不如直接一句 if n is NaN 来判断简洁,易读
    8 条回复    2014-08-17 18:11:05 +08:00
    roricon
        1
    roricon  
       2014-08-17 12:35:40 +08:00   ❤️ 2
    https://docs.python.org/2/reference/datamodel.html#the-standard-type-hierarchy

    你可以直接检测是否继承自numbers.Number类
    from numbers import Number
    >>>isinstance(1, Number)
    >>>True

    >>>isinstance(1.1111, Number)
    >>>True
    ZzFoo
        2
    ZzFoo  
    OP
       2014-08-17 13:00:21 +08:00
    @roricon 非常感谢
    hahastudio
        3
    hahastudio  
       2014-08-17 13:24:00 +08:00   ❤️ 1
    你也许需要 float("nan") 和 math.isnan(x)
    >>> float("nan")
    nan
    >>> nan = float("nan")
    >>> import math
    >>> math.isnan(nan)
    True
    >>> math.isnan(1)
    False
    >>> math.isnan(nan + 1)
    True
    ZzFoo
        4
    ZzFoo  
    OP
       2014-08-17 15:32:35 +08:00
    @hahastudio 谢谢啊。但是math.isnan()只接受float类型做参数,又怎么用它来判断变量类型呢
    msg7086
        5
    msg7086  
       2014-08-17 15:49:35 +08:00
    @ZzFoo 我觉得上面写得很清楚了啊, math.isnan(float("nan"))
    ZzFoo
        6
    ZzFoo  
    OP
       2014-08-17 16:08:47 +08:00
    @msg7086 float()可以把纯数字的字符串和‘nan’这个字符串转换成float型,但是没法转换其他的字符串。

    >>> float('123')
    123.0
    >>> float('nan')
    nan
    >>> float('nana')
    Traceback (most recent call last):
    File "<pyshell#97>", line 1, in <module>
    float('nana')
    ValueError: could not convert string to float: 'nana'
    hahastudio
        7
    hahastudio  
       2014-08-17 16:45:23 +08:00   ❤️ 1
    啊,Python 喜欢先试,后果另说
    你大可以
    try:
    ....n = float(input_str) # e.g. input_str = "123"
    except ValueError:
    ....n = float("nan") # or any other default value you want

    对了,作为豆知识
    >>> float("inf")
    inf
    >>> float("-inf")
    -inf

    对了,突然想起来,也许这个才是 LZ 最想要的:
    >>> from decimal import *
    >>> setcontext(ExtendedContext)
    >>> nan = Decimal("I'm not a number!")
    >>> nan
    Decimal('NaN')
    >>> print nan
    NaN

    它还能做到更多:
    >>> inf = Decimal(1) / Decimal(0)
    >>> print inf
    Infinity
    >>> neginf = Decimal(-1) / Decimal(0)
    >>> print neginf
    -Infinity
    >>> print inf + neginf
    NaN
    >>> print inf * neginf
    -Infinity
    >>> print 2 * inf
    Infinity
    >>> print 1 + nan
    NaN
    ZzFoo
        8
    ZzFoo  
    OP
       2014-08-17 18:11:05 +08:00
    @hahastudio 刚看了一下手册,发现Decimal类有一个is_nan()的方法。谢谢啦~

    >>> Decimal('anything').is_nan()
    True
    >>> Decimal(1).is_nan()
    False
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1289 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:33 · PVG 07:33 · LAX 16:33 · JFK 19:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.