Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6670

Re: float("nan") in set or as key

From Chris Torek <nospam@torek.net>
Newsgroups comp.lang.python
Subject Re: float("nan") in set or as key
Date 2011-05-30 19:58 +0000
Organization None of the Above
Message-ID <is0stb018r3@news2.newsguy.com> (permalink)
References <271acea3-c38f-4123-9038-e348fb841971@glegroupsg2000goo.googlegroups.com> <4de31635$0$29990$c3e8da3$5496439d@news.astraweb.com> <irv6ev01jht@news1.newsguy.com> <4de3358b$0$29990$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


In article <4de3358b$0$29990$c3e8da3$5496439d@news.astraweb.com>
Steven D'Aprano  <steve+comp.lang.python@pearwood.info> wrote:
>Better than a float method is a function which takes any number as 
>argument:
>
>>>> import math, fractions, decimal
>>>> math.isnan(fractions.Fraction(2, 3))
>False
>>>> math.isnan(decimal.Decimal('nan'))
>True

Ah, apparently someone's been using Larry Wall's time machine. :-)

I should have looked at documentation.  In my case, though:

    $ python
    Python 2.5.1 (r251:54863, Dec 16 2010, 14:12:43) 
    [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import math
    >>> math.isnan
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'module' object has no attribute 'isnan'

>You can even handle complex NANs with the cmath module:
>
>>>> import cmath
>>>> cmath.isnan(complex(1, float('nan')))
>True

Would it be appropriate to have isnan() methods for Fraction,
Decimal, and complex, so that you do not need to worry about whether
to use math.isnan() vs cmath.isnan()?  (I almost never work with
complex numbers so am not sure if the "or" behavior -- cmath.isinf
and cmath.isnan return true if either real or complex part are
Infinity or NaN respectively -- is appropriate in algorithms that
might be working on any of these types of numbers.)

It might also be appropriate to have trivial always-False isinf and
isnan methods for integers.
-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)      http://web.torek.net/torek/index.html

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: float("nan") in set or as key Carl Banks <pavlovevidence@gmail.com> - 2011-05-29 17:55 -0700
  Re: float("nan") in set or as key Chris Angelico <rosuav@gmail.com> - 2011-05-30 11:14 +1000
    Re: float("nan") in set or as key Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-30 04:15 +0000
      Re: float("nan") in set or as key John Nagle <nagle@animats.com> - 2011-05-29 21:25 -0700
      Re: float("nan") in set or as key Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-30 06:14 +0000
  Re: float("nan") in set or as key Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-30 03:59 +0000
    Re: float("nan") in set or as key Chris Torek <nospam@torek.net> - 2011-05-30 04:29 +0000
      Re: float("nan") in set or as key Chris Torek <nospam@torek.net> - 2011-05-30 05:53 +0000
      Re: float("nan") in set or as key Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-30 06:13 +0000
        Re: float("nan") in set or as key Chris Torek <nospam@torek.net> - 2011-05-30 19:58 +0000
          Re: float("nan") in set or as key Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-30 23:22 +0000

csiph-web