I'm trying to put some values into a struct. Some of these values are NaN
and Inf due to the nature of the data. As you well may know, struct (and
other things) in Python <= 2.4 doesn't support inf and nan float values.
You get the dreaded "SystemError: frexp() result out of range" error.
Before I go and write my own little wrapper, has anyone out there written
an "extended" struct that supports the inf and nan values? I know this is
fixed in 2.5, but using that isn't an option at this point, as users will
be running older versions of python.
I suppose I could get the relevant source from the 2.5 source and compile it
as a custom package, but that wouldn't be very transparent for my users,
and would probably be getting in way over my head. :)
Ideas? Suggestions?
j
--
Joshua Kugler
Lead System Admin -- Senior Programmer http://www.eeinternet.com
PGP Key: http://pgp.mit.edu/ Â*ID 0xDB26D7CE 4 2449
On Sep 19, 9:58 pm, "Joshua J. Kugler" <jos...@eeinternet.comwrote:
I'm trying to put some values into a struct. Some of these values are NaN
and Inf due to the nature of the data. As you well may know, struct (and
other things) in Python <= 2.4 doesn't support inf and nan float values.
You get the dreaded "SystemError: frexp() result out of range" error.
Before I go and write my own little wrapper, has anyone out there written
an "extended" struct that supports the inf and nan values? I know this is
fixed in 2.5, but using that isn't an option at this point, as users will
be running older versions of python.
I suppose I could get the relevant source from the 2.5 source and compile it
as a custom package, but that wouldn't be very transparent for my users,
and would probably be getting in way over my head. :)
Ideas? Suggestions?
Here's a wrapped pack:
import struct
pack_double_workaround = {
'inf': struct.pack('Q', 0x7ff0000000000000L),
'-inf': struct.pack('Q', 0xfff0000000000000L),
'nan': struct.pack('Q', 0x7ff8000000000000L)
}
def pack_one_safe(f, a):
if f == 'd' and str(f) in pack_double_workaround:
return pack_double_workaround[str(f)]
return struct.pack(f, a)
def pack(fmt, *args):
return ''.join(pack_one_safe(f, a) for f, a in zip(fmt, args))
Unpacking is similar: unpack doubles with 'Q' and test the
long for equality with +-inf, and find nan's by checking bits
52 to 62. If the number's ok, unpack again using 'd'.
You can get python values for nan, -inf and inf by using
float('nan'), float('-inf'), float('inf').
I've not been able to properly test this, as struct seems
to work fine in Python 2.3 and 2.4 on MacOS X.
HTH
--
Paul Hankin
Both str(f) should be str(a) in pack_one_safe.
--
Paul Hankin
On Thursday 20 September 2007 11:19, Paul Hankin wrote:
>I suppose I could get the relevant source from the 2.5 source and compile it as a custom package, but that wouldn't be very transparent for my users, and would probably be getting in way over my head. :)
Ideas? Suggestions?
Here's a wrapped pack:
import struct
pack_double_workaround = {
'inf': struct.pack('Q', 0x7ff0000000000000L),
'-inf': struct.pack('Q', 0xfff0000000000000L),
'nan': struct.pack('Q', 0x7ff8000000000000L)
}
def pack_one_safe(f, a):
if f == 'd' and str(f) in pack_double_workaround:
return pack_double_workaround[str(f)]
return struct.pack(f, a)
def pack(fmt, *args):
return ''.join(pack_one_safe(f, a) for f, a in zip(fmt, args))
Unpacking is similar: unpack doubles with 'Q' and test the
long for equality with +-inf, and find nan's by checking bits
52 to 62. If the number's ok, unpack again using 'd'.
You can get python values for nan, -inf and inf by using
float('nan'), float('-inf'), float('inf').
I've not been able to properly test this, as struct seems
to work fine in Python 2.3 and 2.4 on MacOS X.
Thanks for the ideas, Paul! I came up with something that works for me, but
this has a few ideas that I'm going to implement in my wrapper to make for
cleaner code.
As to testing it on MacOS X: yeah, it can be a somewhat system-dependent
problem, so may not show up on all architectures.
Thanks for the tips!
j
--
Joshua Kugler
Lead System Admin -- Senior Programmer http://www.eeinternet.com
PGP Key: http://pgp.mit.edu/ Â*ID 0xDB26D7CE
On 2007-09-20, Joshua J. Kugler <jo****@eeinternet.comwrote:
>import struct
pack_double_workaround = { 'inf': struct.pack('Q', 0x7ff0000000000000L), '-inf': struct.pack('Q', 0xfff0000000000000L), 'nan': struct.pack('Q', 0x7ff8000000000000L) }
def pack_one_safe(f, a): if f == 'd' and str(f) in pack_double_workaround: return pack_double_workaround[str(f)] return struct.pack(f, a)
def pack(fmt, *args): return ''.join(pack_one_safe(f, a) for f, a in zip(fmt, args))
NB: the strings returned by str() when passed a NaN or Inf are
system dependent and aren't guaranteed to be consistent from
one day to the next unless you've overridden the floating point
object's __repr__ method to make sure.
>Unpacking is similar: unpack doubles with 'Q' and test the long for equality with +-inf, and find nan's by checking bits 52 to 62. If the number's ok, unpack again using 'd'.
Here are the tests I use for 32-bit work:
def isNaN(u):
return ((u & 0x7f800000) == 0x7f800000) and (u & 0x7fffff)
def isInf(u):
return ((u & 0x7f800000) == 0x7f800000) and ((u & 0x7fffff)==0)
def isNeg(u):
return (u & 0x80000000)
>You can get python values for nan, -inf and inf by using float('nan'), float('-inf'), float('inf').
I've not been able to properly test this, as struct seems to work fine in Python 2.3 and 2.4 on MacOS X.
Thanks for the ideas, Paul! I came up with something that
works for me, but this has a few ideas that I'm going to
implement in my wrapper to make for cleaner code.
As to testing it on MacOS X: yeah, it can be a somewhat
system-dependent problem,
It shouldn't be, but unfortunately it is. If you're careful,
you can come up with something that's fairly portable (I've got
a wrapped pickle/unpickle that's nan/inf aware and works on
both Win32 and Linux. Holler if you want it.
Thanks for the tips!
--
Grant Edwards grante Yow! I Know A Joke!!
at
visi.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Cgacc20 |
last post by:
I have a c struct from old code that cannot be modified and I am trying to
write a wrapper C++ class around it.
This class is often passed as a pointer to some c functions of a library and I...
|
by: Dave |
last post by:
would someone please be so kind as to explain:
|
by: seia0106 |
last post by:
Hello,
Writing a program in c++ that should use complex numbers I have two
choices before me.
1- define a struct for complex data i.e
struct {float real,imag;
}ComplexNum;
2-use an array...
|
by: James Harris |
last post by:
Having updated my Debian system it now complains that I am using
an incompatible pointer type in warnings such as "passing arg 2
of 'bind' from incompatible pointer type" from code,
struct...
|
by: a |
last post by:
I have a struct to write to a file
struct _structA{
long x;
int y;
float z;
}
struct _structA A;
//file open
write(fd,A,sizeof(_structA));
//file close
|
by: Tor Aadnevik |
last post by:
Hi,
I'm trying to call a win32 function using pinvoke and C# .Net.
The function takes a struct reference as a parameter (lpInfo), but on return
the struct is not filled with data, and I'm...
|
by: stromhau |
last post by:
Hi,
I have made a few classes in c++. They somehow cooperate doing some 3d stuff.
Basically it is a moving camera acting as a flight, i have placed a lot of objects around the scene together with...
|
by: cr55 |
last post by:
I was wondering if anyone can help me with this programming code as i keep getting errors and am not sure how to fix them. The error code displayed now is error: C2228: left of '.rent' must have...
|
by: gdarian216 |
last post by:
can I pass grades.projects in my function call that is void get_scores(ifstream& infile, int num_scores, grades.projects)
and the function would look like
void get_scores(ifstream& infile, int...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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...
|
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,...
| |