473,376 Members | 1,218 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,376 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 2447
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.