464,492 Members | 929 Online Need help? Post your question and get tips & solutions from a community of 464,492 IT Pros & Developers. It's quick & easy.

# comparing nan "number"

 P: n/a 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 Replies

 P: n/a "BoÅ¡tjan Jerko" 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

 P: n/a BoÅ¡tjan Jerko 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

 P: n/a BoÅ¡tjan Jerko 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

 P: n/a jj*@pobox.com (John J. Lee) writes: BoÅ¡tjan Jerko 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

 P: n/a Michael Hudson 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

 P: n/a Michael Hudson 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

 P: n/a Alexander Schmolck 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

 P: n/a jj*@pobox.com (John J. Lee) writes: Alexander Schmolck 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

 P: n/a Michael Hudson writes: jj*@pobox.com (John J. Lee) writes: Alexander Schmolck 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

 P: n/a jj*@pobox.com (John J. Lee) writes: Michael Hudson writes: jj*@pobox.com (John J. Lee) writes: Alexander Schmolck 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. 