472,331 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 software developers and data experts.

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 2560

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Alex Martelli | last post by:
I hesitate a bit to post this, but... on the Italian Python NG, somebody was asking whether there was any way to convert an integer number x into a...
16
by: John Baker | last post by:
Hi: I know this is a strange question, but I have inherited a system where files are copied and records re auto numbered (as an index field)...
2
by: Gianluca_Venezia | last post by:
Talking about high number of record seem ridiculus, if this number is about 88.000 but when I open a form, linked via ODBC to a MYSQL table, the...
5
by: Jeffrey Bradshaw | last post by:
Hey everybody, I've got a VB.NET program that is trying to access a Word document and I'm having all kinds of trouble. First on is the above. I'm...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the...
1
by: Ducknut | last post by:
Not so much a problem as a discussion. I am currently in the early stages of designing a database to hold a bunch of water quality data (e.g.,...
3
by: Nevyn | last post by:
How do I do that? Turning the layout from the customerID-number "10205" into looking like "10 205"? I've just incresed it from four digits to five,...
6
jasjas
by: jasjas | last post by:
my Q is like this, i want to design a calculation which use the number user enter to "/" by 100. so , offcouse the result mayb in double or...
2
by: mktselvan | last post by:
Hi, Existing running oracle application 11i (11.5.8) Database version is 8.1.7.4 There is any command / way to know the number of concurrent...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.