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


Groups > comp.lang.python > #83333 > unrolled thread

where in Nan defined

Started bymaurog <qualsivoglia@dovetipare.nz>
First post2015-01-08 14:15 +0000
Last post2015-01-09 01:28 +1100
Articles 12 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  where in Nan defined maurog <qualsivoglia@dovetipare.nz> - 2015-01-08 14:15 +0000
    Re: where in Nan defined Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-08 07:24 -0700
      Re: where in Nan defined Marko Rauhamaa <marko@pacujo.net> - 2015-01-08 16:37 +0200
        Re: where in Nan defined Chris Angelico <rosuav@gmail.com> - 2015-01-09 01:45 +1100
          Re: where in Nan defined Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-01-08 16:55 +0200
        Re: where in Nan defined Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-01-08 16:50 +0200
          Re: where in Nan defined Chris Angelico <rosuav@gmail.com> - 2015-01-09 01:58 +1100
            Re: where in Nan defined Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-09 12:46 +1100
              Re: where in Nan defined Chris Angelico <rosuav@gmail.com> - 2015-01-09 13:07 +1100
              Re: where in Nan defined Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-08 19:12 -0700
              Re: where in Nan defined Chris Angelico <rosuav@gmail.com> - 2015-01-09 13:25 +1100
    Re: where in Nan defined Chris Angelico <rosuav@gmail.com> - 2015-01-09 01:28 +1100

#83333 — where in Nan defined

Frommaurog <qualsivoglia@dovetipare.nz>
Date2015-01-08 14:15 +0000
Subjectwhere in Nan defined
Message-ID<m8m3et$1gu$2@speranza.aioe.org>
I'm running some pandas examples and I canno find in what module NaN is 
defined. Does anyone know what module I have to import in order to have 
it defined?

[toc] | [next] | [standalone]


#83335

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-01-08 07:24 -0700
Message-ID<mailman.17472.1420727095.18130.python-list@python.org>
In reply to#83333
On Thu, Jan 8, 2015 at 7:15 AM, maurog <qualsivoglia@dovetipare.nz> wrote:
> I'm running some pandas examples and I canno find in what module NaN is
> defined. Does anyone know what module I have to import in order to have
> it defined?

It's not defined anywhere. To get nan as a literal just do:

    nan = float("nan")

[toc] | [prev] | [next] | [standalone]


#83339

FromMarko Rauhamaa <marko@pacujo.net>
Date2015-01-08 16:37 +0200
Message-ID<87zj9te3tz.fsf@elektro.pacujo.net>
In reply to#83335
Ian Kelly <ian.g.kelly@gmail.com>:

> To get nan as a literal just do:
>
>     nan = float("nan")

True, but that got me thinking: what standard Python math operation
evaluates to NaN?


Marko

[toc] | [prev] | [next] | [standalone]


#83341

FromChris Angelico <rosuav@gmail.com>
Date2015-01-09 01:45 +1100
Message-ID<mailman.17476.1420728314.18130.python-list@python.org>
In reply to#83339
On Fri, Jan 9, 2015 at 1:37 AM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Ian Kelly <ian.g.kelly@gmail.com>:
>
>> To get nan as a literal just do:
>>
>>     nan = float("nan")
>
> True, but that got me thinking: what standard Python math operation
> evaluates to NaN?

Subtracting infinity from infinity is one easy way.

>>> 1e309
inf
>>> 1e309-1e309
nan

ChrisA

[toc] | [prev] | [next] | [standalone]


#83343

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2015-01-08 16:55 +0200
Message-ID<qotfvbljp8u.fsf@ruuvi.it.helsinki.fi>
In reply to#83341
Chris Angelico writes:
> On Fri, Jan 9, 2015 at 1:37 AM, Marko Rauhamaa wrote:
> >
> > True, but that got me thinking: what standard Python math
> > operation evaluates to NaN?
> 
> Subtracting infinity from infinity is one easy way.
> 
> >>> 1e309
> inf
> >>> 1e309-1e309
> nan

I managed to get inf by arithmetic on finite numbers:

  >>> 1e300/1e-300
  inf

  >>> o = 1e300/1e-300 ; o-o
  nan

[toc] | [prev] | [next] | [standalone]


#83342

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2015-01-08 16:50 +0200
Message-ID<qotk30xjpib.fsf@ruuvi.it.helsinki.fi>
In reply to#83339
Marko Rauhamaa writes:
> Ian Kelly:
> 
> > To get nan as a literal just do:
> >
> >     nan = float("nan")
> 
> True, but that got me thinking: what standard Python math operation
> evaluates to NaN?

All manner of arithmetics gives overflow errors ("Numerical result out
of range") but a literal with a large exponent gives an inf, so:

   >>> 0*1e400
   nan

(That's on a Python 3.2.3 ... on linux2.)

[toc] | [prev] | [next] | [standalone]


#83344

FromChris Angelico <rosuav@gmail.com>
Date2015-01-09 01:58 +1100
Message-ID<mailman.17477.1420729115.18130.python-list@python.org>
In reply to#83342
On Fri, Jan 9, 2015 at 1:50 AM, Jussi Piitulainen
<jpiitula@ling.helsinki.fi> wrote:
>    >>> 0*1e400
>    nan

Nice, that's shorter than mine.

ChrisA

[toc] | [prev] | [next] | [standalone]


#83386

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-01-09 12:46 +1100
Message-ID<54af32f4$0$12990$c3e8da3$5496439d@news.astraweb.com>
In reply to#83344
Chris Angelico wrote:

> On Fri, Jan 9, 2015 at 1:50 AM, Jussi Piitulainen
> <jpiitula@ling.helsinki.fi> wrote:
>>    >>> 0*1e400
>>    nan
> 
> Nice, that's shorter than mine.

o_O

Is that really the sort of thing you should be revealing here?

Oh wait, you're talking about code...

I'm not entirely sure, but I suspect that 0*1e400 may not be quite as
portable as your inf-inf. By the rules of IEEE-754 arithmetic, inf-inf has
to return a NAN, but if your floats have sufficient precision available to
represent 1e400, 0*1e400 might return 0.

The fallback rule I use when float('nan') fails is

    INF = 1e3000  # Hopefully, this should overflow to INF.
    NAN = INF-INF  # And this hopefully will give a NaN.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#83387

FromChris Angelico <rosuav@gmail.com>
Date2015-01-09 13:07 +1100
Message-ID<mailman.17493.1420769224.18130.python-list@python.org>
In reply to#83386
On Fri, Jan 9, 2015 at 12:46 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Chris Angelico wrote:
>
>> On Fri, Jan 9, 2015 at 1:50 AM, Jussi Piitulainen
>> <jpiitula@ling.helsinki.fi> wrote:
>>>    >>> 0*1e400
>>>    nan
>>
>> Nice, that's shorter than mine.
>
> I'm not entirely sure, but I suspect that 0*1e400 may not be quite as
> portable as your inf-inf. By the rules of IEEE-754 arithmetic, inf-inf has
> to return a NAN, but if your floats have sufficient precision available to
> represent 1e400, 0*1e400 might return 0.
>
> The fallback rule I use when float('nan') fails is
>
>     INF = 1e3000  # Hopefully, this should overflow to INF.
>     NAN = INF-INF  # And this hopefully will give a NaN.

The first question is "will 1eN overflow to inf?". I'm fairly sure
Python guarantees that an unrepresentable float constant will be
treated as infinity, so all you have to do is crank up the exponent
any time you suspect it'll be representable. The second is "will 0*inf
result in nan?", and I would expect that to be as reliable as INF-INF.

ChrisA

[toc] | [prev] | [next] | [standalone]


#83389

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-01-08 19:12 -0700
Message-ID<mailman.17494.1420769609.18130.python-list@python.org>
In reply to#83386
On Thu, Jan 8, 2015 at 7:07 PM, Chris Angelico <rosuav@gmail.com> wrote:
> On Fri, Jan 9, 2015 at 12:46 PM, Steven D'Aprano
>> The fallback rule I use when float('nan') fails is
>>
>>     INF = 1e3000  # Hopefully, this should overflow to INF.
>>     NAN = INF-INF  # And this hopefully will give a NaN.
>
> The first question is "will 1eN overflow to inf?". I'm fairly sure
> Python guarantees that an unrepresentable float constant will be
> treated as infinity, so all you have to do is crank up the exponent
> any time you suspect it'll be representable.

Or to never have to worry about it:

INF = 1e400
while not math.isinf(INF):
    INF *= INF

[toc] | [prev] | [next] | [standalone]


#83391

FromChris Angelico <rosuav@gmail.com>
Date2015-01-09 13:25 +1100
Message-ID<mailman.17495.1420770350.18130.python-list@python.org>
In reply to#83386
On Fri, Jan 9, 2015 at 1:12 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> Or to never have to worry about it:
>
> INF = 1e400
> while not math.isinf(INF):
>     INF *= INF

With no imports whatsoever:

inf = 1e400
nan = inf-inf
while nan == nan:
    inf *= inf
    nan = inf-inf

But now we're getting stupid :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#83336

FromChris Angelico <rosuav@gmail.com>
Date2015-01-09 01:28 +1100
Message-ID<mailman.17473.1420727304.18130.python-list@python.org>
In reply to#83333
On Fri, Jan 9, 2015 at 1:15 AM, maurog <qualsivoglia@dovetipare.nz> wrote:
> I'm running some pandas examples and I canno find in what module NaN is
> defined. Does anyone know what module I have to import in order to have
> it defined?

It's simply float("nan"). If you want a name, you can give it one:

NaN = float("nan")
# or
nan = float("nan")

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web