469,935 Members | 1,983 Online

comparing nan "number"

Hello !

I need to know if the result of math formula is nan (Not a number).
How can I do that?

Thanks,

B.

Jul 18 '05 #1
10 2458 "BoÅ¡tjan Jerko" <bo***********@mf.uni-lj.si> wrote in message news:ma*************************************@pytho n.org...
Hello !

I need to know if the result of math formula is nan (Not a number).
How can I do that?

Thanks,

B.

I used repr() as a workaround:
INF = 1e9999
NAN = INF - INF
def isnan( x ): .... rx = repr(float(x))
.... return rx==repr(NAN) or rx==repr(-NAN)
.... t = 2*NAN
t == NAN False t is NAN False isnan(t) True

You can also use binary representation of numbers:
import struct
NAN_POS = struct.pack('d',NAN)
NAN_NEG = struct.pack('d',-NAN)
def isnan2( x ):

.... xs = struct.pack('d',float(x))
.... return xs==NAN_POS or xs==NAN_NEG
....
HTH
--
Georgy Pruss
E^mail: 'ZDAwMTEyMHQwMzMwQGhvdG1haWwuY29t\n'.decode('base6 4')
Jul 18 '05 #2
BoÅ¡tjan Jerko <bo***********@mf.uni-lj.si> writes:
Hello !

I need to know if the result of math formula is nan (Not a number).
How can I do that?

The only non-hacky way I know of is installing scipy and using scipy.isnan.

'as
Jul 18 '05 #3
BoÅ¡tjan Jerko <bo***********@mf.uni-lj.si> writes:
I need to know if the result of math formula is nan (Not a number).
How can I do that?

I suppose:

NAN = any_calculation_that_returns_nan()

if some_other_calculation() is NAN:
...
John
Jul 18 '05 #4
jj*@pobox.com (John J. Lee) writes:
BoÅ¡tjan Jerko <bo***********@mf.uni-lj.si> writes:
I need to know if the result of math formula is nan (Not a number).
How can I do that?

I suppose:

NAN = any_calculation_that_returns_nan()

if some_other_calculation() is NAN:
...

I wouldn't expect that to work. Unfortunately, I don't think there is
a reliable way.

result = some_calculation()
if result != result:
# it's a nan

*might* work, depending probably on compiler, Python version, platform
and phase of the moon.

Cheers,
mwh

--
There's a difference between random people with stripy jumpers,
and a respected scientist with a reputation.
-- Steve Kitson, ucam.chat
Jul 18 '05 #5
Michael Hudson <mw*@python.net> writes:
result = some_calculation()
if result != result:
# it's a nan

*might* work, depending probably on compiler, Python version, platform
and phase of the moon.

Since this gives different results for two recent python versions installed on
my machine, I'd rather recommend against it if you value your sanity. IMO it's
either the repr hack or scipy, unlike you like to live dangerously.

[In a similar vain, I'd also recommend resisting the temptation of putting
nans (or richly comparing types) into containers like lists etc. because
that's just asking for undefined behavior. E.g. ``x in [x,x,x]`` will quite
possibly be `False` and {x:"some value}[x] might well raise an exception if
`x` is a nan (or indeed one of infinitely many Numeric arrays with
non-symmetric `==`) . Somehow all this suggests to me that too many things are
folded into python's __eq__.]
'as
Jul 18 '05 #6
Michael Hudson <mw*@python.net> writes:
jj*@pobox.com (John J. Lee) writes:

[...]
I suppose:

NAN = any_calculation_that_returns_nan()

if some_other_calculation() is NAN:
...

I wouldn't expect that to work. Unfortunately, I don't think there is
a reliable way.

[...]

Yeah, I forgot nan is a floating point value, not a special Python
thingy like None, duh (thanks Alexander).
John
Jul 18 '05 #7
Alexander Schmolck <a.********@gmx.net> writes:
[...]
possibly be `False` and {x:"some value}[x] might well raise an exception if
`x` is a nan (or indeed one of infinitely many Numeric arrays with

[...]

Don't you mean SomeMapping((x, "some value"))[x]? A dict lookup
doesn't use __eq__.
John
Jul 18 '05 #8
jj*@pobox.com (John J. Lee) writes:
Alexander Schmolck <a.********@gmx.net> writes:
[...]
possibly be `False` and {x:"some value}[x] might well raise an exception if
`x` is a nan (or indeed one of infinitely many Numeric arrays with

[...]

Don't you mean SomeMapping((x, "some value"))[x]? A dict lookup
doesn't use __eq__.

Huh? Yes it does (*after* __hash__, of course, but...).

Cheers,
mwh

--
how am I expected to quit smoking if I have to deal with NT
every day -- Ben Raia
Jul 18 '05 #9
Michael Hudson <mw*@python.net> writes:
jj*@pobox.com (John J. Lee) writes:
Alexander Schmolck <a.********@gmx.net> writes:
[...]
possibly be `False` and {x:"some value}[x] might well raise an exception if
`x` is a nan (or indeed one of infinitely many Numeric arrays with

[...]

Don't you mean SomeMapping((x, "some value"))[x]? A dict lookup
doesn't use __eq__.

Huh? Yes it does (*after* __hash__, of course, but...).

Oh, collisions, right?
John
Jul 18 '05 #10
jj*@pobox.com (John J. Lee) writes:
Michael Hudson <mw*@python.net> writes:
jj*@pobox.com (John J. Lee) writes:
Alexander Schmolck <a.********@gmx.net> writes:
[...]
> possibly be `False` and {x:"some value}[x] might well raise an exception if
> `x` is a nan (or indeed one of infinitely many Numeric arrays with
[...]

Don't you mean SomeMapping((x, "some value"))[x]? A dict lookup
doesn't use __eq__.

Huh? Yes it does (*after* __hash__, of course, but...).

Oh, collisions, right?

Yes. You can't know a collision has (or has not) happened until after
you've tried __eq__...

Cheers,
mwh

--
ARTHUR: Why should a rock hum?
FORD: Maybe it feels good about being a rock.
-- The Hitch-Hikers Guide to the Galaxy, Episode 8
Jul 18 '05 #11

 21 posts views Thread by Alex Martelli | last post: by 16 posts views Thread by John Baker | last post: by 2 posts views Thread by Gianluca_Venezia | last post: by 5 posts views Thread by Jeffrey Bradshaw | last post: by 7 posts views Thread by jccorreu | last post: by 1 post views Thread by Ducknut | last post: by 3 posts views Thread by Nevyn | last post: by 6 posts views Thread by jasjas | last post: by 2 posts views Thread by mktselvan | last post: by reply views Thread by eddparker01 | last post: by reply views Thread by eddparker01 | last post: by reply views Thread by isladogs | last post: by reply views Thread by Trystan | last post: by reply views Thread by Trystan | last post: by reply views Thread by WIPE | last post: by reply views Thread by MikeCant | last post: by 1 post views Thread by MikeCant | last post: by 2 posts views Thread by Usman55 | last post: by