469,935 Members | 1,983 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,935 developers. It's quick & easy.

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Alex Martelli | last post: by
5 posts views Thread by Jeffrey Bradshaw | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.