I have a C extension module that is returning some doubles. When the
doubles get out of range, the numbers print as 'nan'.
Is there a better way to test for NaN than
str(p1)=='nan'
where p1 is a float?
python2.3
Thanks,
John Hunter 11 12077
At some point, John Hunter <jd******@ace.bsd.uchicago.edu> wrote: I have a C extension module that is returning some doubles. When the doubles get out of range, the numbers print as 'nan'.
Is there a better way to test for NaN than
str(p1)=='nan'
where p1 is a float?
The string representation of NaN is system-dependent. On Windows it's
something like #NaN. You'd be better off with Jeff Epler's suggestion
of wrapping isnan() (since you already have a C extension module, you
could through it in there).
If you're on something that uses IEEE floating-point representations,
something like this in pure python should work:
import struct
def isnan(x):
s = struct.pack('d', x)
if struct.pack('h', 1) == '\x01\x00':
return s == '\x00\x00\x00\x00\x00\x00\xf0\x7f':
else:
return s == '\x7f\xf8\x00\x00\x00\x00\x00\x00':
The test for endianness is there since struct.unpack('<d', x)
complains that frexp() is out of range.
--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
John Hunter <jd******@ace.bsd.uchicago.edu> writes: I have a C extension module that is returning some doubles. When the doubles get out of range, the numbers print as 'nan'.
Is there a better way to test for NaN than
str(p1)=='nan'
where p1 is a float?
I'd hope so: that's exceeding unportable. Of course, all things to do
with nans are unportable, so you're going to have to tell us more of
your requirements...
Cheers,
mwh
--
I have *both* hands clapping, but I'm still not sure it's a sound.
When I tried deciding if it were a sound while clapping only one
hand, I fell off my chair.
-- Peter Hansen, Zen master, comp.lang.python
John Hunter wrote: I have a C extension module that is returning some doubles. When the doubles get out of range, the numbers print as 'nan'.
Is there a better way to test for NaN than
str(p1)=='nan'
where p1 is a float?
python2.3
Thanks, John Hunter
As far as I know NaN is th only value that yields false when compared
to itself. This leads to
def isNaN(x):
return (x == x) == False
Mit freundlichen Gruessen,
Peter Maas
--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplusr.de
-------------------------------------------------------------------
Peter Maas wrote: John Hunter wrote: Is there a better way to test for NaN than
str(p1)=='nan'
where p1 is a float? [...] As far as I know NaN is th only value that yields false when compared to itself. This leads to
def isNaN(x): return (x == x) == False
Shorter:
def isNaN(x):
return x != x
Mit freundlichen Gruessen,
Peter Maas
--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplusr.de
-------------------------------------------------------------------
Peter Maas wrote: As far as I know NaN is th only value that yields false when compared to itself. This leads to
def isNaN(x): return (x == x) == False
That is not portable:
Python 2.3.3 (#1, Jan 3 2004, 13:57:08)
[GCC 3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information. nan = float("nan") nan != nan
False
Peter
Peter Otten wrote: def isNaN(x): return (x == x) == False
That is not portable:
Python 2.3.3 (#1, Jan 3 2004, 13:57:08) [GCC 3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information.
nan = float("nan") nan != nan
False
Thanks for correction. I firmly believed that NaN != NaN -> True
was a property of IEEE 754 and of all software using this standard.
For gcc/Linux you could use
def isNaN(x):
return (x == 0) and (x == 1)
and utilize sys.platform. :)
Mit freundlichen Gruessen,
Peter Maas
--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplusr.de
-------------------------------------------------------------------
Peter Maas <fp********@netscape.net> writes: John Hunter wrote: I have a C extension module that is returning some doubles. When the doubles get out of range, the numbers print as 'nan'. Is there a better way to test for NaN than str(p1)=='nan' where p1 is a float? python2.3 Thanks, John Hunter
As far as I know NaN is th only value that yields false when compared to itself.
There can be more than one NaN, by the way.
This leads to
def isNaN(x): return (x == x) == False
This will work with 2.3 on Windows (I believe), current CVS on Windows
(if compiled with VC7.1), current CVS on Linux (assuming an even
vaguely recent gcc), but not current CVS on Windows compiled with VC6,
nor Python 2.3 on Linux/gcc. Confused yet?
Other platforms I haven't the faintest idea about.
Cheers,
mwh
--
41. Some programming languages manage to absorb change, but
withstand progress.
-- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html
Peter Maas wrote: def isNaN(x): return (x == 0) and (x == 1)
This works here. Do you have an idea what the rationale behind this
behaviour (i. e. any number == nan) is?
Peter
>>>>> "Michael" == Michael Hudson <mw*@python.net> writes:
Michael> This will work with 2.3 on Windows (I believe), current
Michael> CVS on Windows (if compiled with VC7.1), current CVS on
Michael> Linux (assuming an even vaguely recent gcc), but not
Michael> current CVS on Windows compiled with VC6, nor Python 2.3
Michael> on Linux/gcc. Confused yet?
Fortunately I only need linux for this particular app, so I can use
one of the platform dependent solutions, but the bevy of proposed
solutions and gotchas have definitely been interesting.
Out of curiosity, are there any platforms where this is known to fail?
def is_nan(x):
return str(x).lower().find('nan')>=0
JDH
John Hunter wrote: Out of curiosity, are there any platforms where this is known to fail?
def is_nan(x): return str(x).lower().find('nan')>=0
Yes...
Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on
win32
IDLE 1.0.2 def is_nan(x):
return str(x).lower().find('nan') >= 0
inf=float('1e9999') inf
1.#INF nan=inf-inf is_nan(nan)
False nan
-1.#IND
--
"Codito ergo sum"
Roel Schroeven
Michael Hudson wrote: There can be more than one NaN, by the way.
I know that but if all NaNs have the same distinct algorithmic behaviour
that's not a problem. This leads to
def isNaN(x): return (x == x) == False
This will work with 2.3 on Windows (I believe), current CVS on Windows (if compiled with VC7.1), current CVS on Linux (assuming an even vaguely recent gcc), but not current CVS on Windows compiled with VC6, nor Python 2.3 on Linux/gcc. Confused yet?
No:
def isNaN1(x):
return x != x
def isNaN2(x):
return (x == 0) and (x == 1)
if sys.platform == 'win32' and '2.3' in sys.version:
isNaN = isNaN1
elif sys.platform == 'win32' and 'good_CVS_VC_stuff' in sys.version:
isNaN = isNaN1
elif sys.platform == 'win32' and 'bad_CVS_VC_stuff' in sys.version:
isNaN = isNaN2
elif sys.platform == 'linux2':
isNaN = isNaN2
else:
raise Exception, "Roll your own isNaN() for platform " \
"%s and version %s\n" % (sys.platform, sys.version)
bad/good_CVS_VC_stuff has to be worked out, don't have the version strings
available.
Mit freundlichen Gruessen,
Peter Maas
--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplusr.de
------------------------------------------------------------------- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Berthold Hoellmann |
last post by:
Hello,
When I use
./configure --with-thread --with-fpectl --with-signal-module \
--with-pymalloc --enable-shared --with-cxx=g++
make test
on 2.3.3 I get
|
by: Remy Blank |
last post by:
Ok, here we go.
I added the possibility for tests using the unittest.py framework
to be skipped. Basically, I added two methods to TestCase:
TestCase.skip(msg): skips unconditionally...
|
by: Edvard Majakari |
last post by:
Hi,
I just found py.test and converted a large unit test module to py.test
format (which is actually almost-no-format-at-all, but I won't get there
now). Having 348 test cases in the module and...
|
by: Andrea M. Segovia |
last post by:
I just compiled (but did not install) perl 5.8.0 on an SGI Origin 300
server (IP35) running IRIX 6.5.20m.
Make test reported one test error, which I narrowed down to
.../lib/ExUtils/t/Constant.t...
|
by: Jussi Mononen |
last post by:
Hi,
I'm having problems to successfully execute the test scripts on a
Compaq host ( OSF1 tr51bdev V5.1 2650 alpha ). Almost all tests end up
with the following error message
"PARI: *** ...
| |
by: arotem |
last post by:
Hi,
I am trying to call an unbound method (PrintInput) with the object
instance as the first argument but getting the following error:
"TypeError: unbound method PrintInput() must be called with...
|
by: Tim Haughton |
last post by:
I've just released an article on using Test Driven Development with C# and
Windows Forms. GUI's are often difficult to test, so I thought it might be
of interest.
The article along with the...
|
by: Ben Finney |
last post by:
Howdy all,
Summary: I'm looking for idioms in unit tests for factoring out
repetitive iteration over test data. I explain my current practice,
and why it's unsatisfactory.
When following...
|
by: Josh |
last post by:
We have a program written in VB6 (over 100,000 lines of code and 230 UI
screens) that we want to get out of VB and into a better language. The
program is over 10 years old and has already been...
|
by: ypjofficial |
last post by:
HI,
I have following terrific confusion.
class test
{
public:
int i;
int * j;
int **k;
|
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,...
| |
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,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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 project—planning, coding, testing,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |