473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

A struct for 2.4 that supports float's inf and nan?

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

Sep 19 '07 #1
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

Sep 20 '07 #2
Both str(f) should be str(a) in pack_one_safe.
--
Paul Hankin

Sep 20 '07 #3
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

Sep 20 '07 #4
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
Sep 21 '07 #5

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

Similar topics

3
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...
16
by: Dave | last post by:
would someone please be so kind as to explain:
7
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...
4
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...
8
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
11
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...
1
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...
2
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...
5
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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 project—planning, coding, testing,...

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.