473,549 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2658

"Boštjan Jerko" <bo***********@ mf.uni-lj.si> wrote in message news:ma******** *************** **************@ python.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: 'ZDAwMTEyMHQwMz MwQGhvdG1haWwuY 29t\n'.decode(' base64')
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_n an()

if some_other_calc ulation() 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_n an()

if some_other_calc ulation() is NAN:
...


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

result = some_calculatio n()
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_calculatio n()
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_n an()

if some_other_calc ulation() 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
2129
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 string which represents it in an arbitrary base N (given a sequence with a len of N that gives the digits to use) as "a single expression". I haven't found a really general way, much less a clear...
16
20457
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) )frequently, and I am wondering how high the number can go without the system crashing. An ancillary question is how one resets an auto number so that the sequence starts again at 1. In the case...
2
1390
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 open and the use of that form is slow, and very slow if the form has combo controls. I have a order form, and a combo control for custemer codes. Order table counts 88.000 records, customer...
5
4641
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 trying to get the number of lines in the document and when I try docWord.BuiltInDocumentProperties("NUMBER OF LINES") it returns an Object. When I look in the debugger, it is of type...
7
5535
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 program we may be using a differnt number of files, with different file names each time. So i'm writing into the code to ask the user how many files, and what their names are. From each we'll read in...
1
1789
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., concentrations of heavy metals in drinking water). Water samples will be sent to a lab for analysis and the lab will send back a report (usually in excel or .txt format), that data will be imported into...
3
1538
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, and it looks like this: lsvItem.SubItems(1) = Right("0000" & CStr(rs!customerID), 5), but how do I affect the layout?
6
2490
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 integer. after that my calculation just wanna user the "whole number" of the result to calculate other things. can anyone help me? thks. For eg: 9 / 2 = 4.5 and i just wanna take the number "4" to...
2
19290
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 users for this application. select SESSIONS_MAX, SESSIONS_WARNING,
0
7527
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7459
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7726
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7967
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7819
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3488
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.