comparing nan "number"

G

Guest

Hello !

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

Thanks,

B.
 
G

Georgy Pruss

Boštjan Jerko said:
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:
.... rx = repr(float(x))
.... return rx==repr(NAN) or rx==repr(-NAN)
....True

You can also use binary representation of numbers:
.... xs = struct.pack('d',float(x))
.... return xs==NAN_POS or xs==NAN_NEG
....


HTH
 
A

Alexander Schmolck

Boštjan Jerko said:
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
 
J

John J. Lee

Boštjan Jerko said:
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
 
M

Michael Hudson

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
 
A

Alexander Schmolck

Michael Hudson said:
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
 
J

John J. Lee

Michael Hudson said:
(e-mail address removed) (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
 
J

John J. Lee

Alexander Schmolck said:
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
 
M

Michael Hudson

Alexander Schmolck said:
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
 
J

John J. Lee

Michael Hudson said:
Alexander Schmolck said:
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
 
M

Michael Hudson

Michael Hudson said:
[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top