473,586 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pickle and infinity

Hello,
I´ve got this problem with pickle, it seems it doesn´t handle
correctly infinite values (nor does Python return overflow/underflow
error). What could I do about it? Example code:
>>x = 1e310 #actually it would be a result of calculations
type(x)
<type 'float'>
>>x
1.#INF
>>import pickle
pickle.loads( pickle.dumps(x) )
[...]
ValueError: invalid literal for float(): 1.#INF

Nov 29 '06 #1
6 1732
To make things more interesting -- Solaris version:
>>x = 1e310
x
Infinity
>>import pickle
pickle.dumps( x)
'FInfinity\n.'
>>pickle.loads( _)
Infinity
>>pickle.dumps( x,1)
[...]
SystemError: frexp() result out of range

Nov 29 '06 #2
On 2006-11-29, Bart Ogryczak <B.********@gma il.comwrote:
I´ve got this problem with pickle, it seems it doesn´t handle
correctly infinite values (nor does Python return
overflow/underflow error). What could I do about it?
Here's what I did. I'm sure it'll fall down on some systems,
but it works on Linux and Windows.

_______________ _______________ _______________ _______________ __________
# unpickle can't handle nan/inf/ind floats, so we need to
# handle that ourselves

def myload_float(se lf):
s = self.readline()[:-1]
try:
f = float(s)
except ValueError:
s = s.upper()
if s in ["1.#INF", "INF"]:
f = 1e300*1e300
elif s in ["-1.#INF", "-INF"]:
f = -1e300*1e300
elif s in ["NAN","1.#QNAN" ,"-1.#QNAN","QNAN" ,"1.#IND","IND" ,"-1.#IND"]:
f = (1e300*1e300)/(1e300*1e300)
else:
raise ValueError, "Don't know what to do with "+`s`
self.append(f)

# unpickle routine that overrides the float load method

def unpickle(f):
unpickler = pickle.Unpickle r(f)
unpickler.dispa tch[pickle.FLOAT] = myload_float
return unpickler.load( )
_______________ _______________ _______________ _______________ __________

--
Grant Edwards grante Yow! I represent a
at sardine!!
visi.com
Nov 29 '06 #3
Bart Ogryczak wrote:
I´ve got this problem with pickle, it seems it doesn´t handle
correctly infinite values (nor does Python return overflow/underflow
error).
Python 2.X relies on the C library to serialize floats, and, as you've
noticed, some C libraries can produce values that they themselves cannot
read.

</F>

Nov 29 '06 #4

Fredrik Lundh wrote:
Bart Ogryczak wrote:
I´ve got this problem with pickle, it seems it doesn´t handle
correctly infinite values (nor does Python return overflow/underflow
error).

Python 2.X relies on the C library to serialize floats, and, as you've
noticed, some C libraries can produce values that they themselves cannot
read.
Actually I´d be very happy, if Python would throw FloatPointExept ion,
rather then allow infinite values. Haven´t got idea how to do it.
fpectl would do the trick, but it´s not present nither on Solaris, nor
on Windows platform. And I´d need it on both.

Nov 29 '06 #5
On 2006-11-29, Bart Ogryczak <B.********@gma il.comwrote:
>
Fredrik Lundh wrote:
>Bart Ogryczak wrote:
I´ve got this problem with pickle, it seems it doesn´t handle
correctly infinite values (nor does Python return overflow/underflow
error).

Python 2.X relies on the C library to serialize floats, and, as you've
noticed, some C libraries can produce values that they themselves cannot
read.

Actually I´d be very happy, if Python would throw FloatPointExept ion,
rather then allow infinite values.
I'd be very unhappy. I use both NaNs and infinities in many
programs, so it needs to be selectable should that feature be
added.
Haven´t got idea how to do it. fpectl would do the trick, but
it´s not present nither on Solaris, nor on Windows platform.
And I´d need it on both.
In my experience anything than needs to be portable and
involves NaN, inf, or denormals is rather painful.

--
Grant Edwards grante Yow! I wish I was a
at sex-starved manicurist
visi.com found dead in the Bronx!!
Nov 29 '06 #6

Grant Edwards wrote:
On 2006-11-29, Bart Ogryczak <B.********@gma il.comwrote:

Fredrik Lundh wrote:
Bart Ogryczak wrote:

I´ve got this problem with pickle, it seems it doesn´t handle
correctly infinite values (nor does Python return overflow/underflow
error).

Python 2.X relies on the C library to serialize floats, and, as you've
noticed, some C libraries can produce values that they themselves cannot
read.
Actually I´d be very happy, if Python would throw FloatPointExept ion,
rather then allow infinite values.

I'd be very unhappy. I use both NaNs and infinities in many
programs, so it needs to be selectable should that feature be
added.
Like I´ve said. I´d be satisfied with something like fpectl, which
gives you option to either throw FloatPointExept ion or go on with NaNs
and infs. NaNs and infinities are pain in the a** if you´re
integrating with C libraries or you´re using pickle, pyro, whatever.
The problem is, that the error pops out in the middle of the code that
really has nothing to do with the actual erroneous calculation. Its
impossible to trace back where the hell the NaN or infinite value comes
from.
Haven´t got idea how to do it. fpectl would do the trick, but
it´s not present nither on Solaris, nor on Windows platform.
And I´d need it on both.

In my experience anything than needs to be portable and
involves NaN, inf, or denormals is rather painful.
That´s not very comforting :-/

Nov 30 '06 #7

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

Similar topics

3
4008
by: Michael Hohn | last post by:
Hi, under python 2.2, the pickle/unpickle sequence incorrectly restores a larger data structure I have. Under Python 2.3, these structures now give an explicit exception from Pickle.memoize(): assert id(obj) not in self.memo I'm shrinking the offending data structure down to find the problem
28
2660
by: Grant Edwards | last post by:
I finally figured out why one of my apps sometimes fails under Win32 when it always works fine under Linux: Under Win32, the pickle module only works with a subset of floating point values. In particular the if you try to dump/load an infinity or nan value, the load operation chokes: Under Linux: $ python
0
1774
by: Mike P. | last post by:
Hi all, I'm working on a simulation (can be considered a game) in Python where I want to be able to dump the simulation state to a file and be able to load it up later. I have used the standard Python pickle module and it works fine pickling/unpickling from files. However, I want to be able to use a third party tool like an XML editor (or...
2
4340
by: Russell Smith | last post by:
Timestamps support infinity. However if appears dates do not. When timestamps are cast to dates, there is no output. Is this an acceptable option or not? Below are a number of examples showing what I am experiencing. The last own shows how converting timestamps to dates and then ordering doesn't give you the order you want. Maybe you...
6
12331
by: Jim Lewis | last post by:
Pickling an instance of a class, gives "can't pickle instancemethod objects". What does this mean? How do I find the class method creating the problem?
5
4187
by: Peter Hansen | last post by:
I'm investigating a puzzling problem involving an attempt to generate a constant containing an (IEEE 754) "infinity" value. (I understand that special float values are a "platform-dependent accident" etc...) The issue appears possibly to point to a bug in the Python compiler, with it producing inconsistent results. I'm using "Python 2.4.2...
5
92988
by: Chris | last post by:
Why can pickle serialize references to functions, but not methods? Pickling a function serializes the function name, but pickling a staticmethod, classmethod, or instancemethod generates an error. In these cases, pickle knows the instance or class, and the method, so what's the problem? Pickle doesn't serialize code objects, so why can't it...
2
1903
by: Pierre Rouleau | last post by:
Hi all, When using Python 2.4.x on a Win32 box, marshal.loads(marshal.dumps(1e66666)) returns 1.0 instead of infinity as it should and does under Python 2.5 (also running on Win32 ). This problem was reported in another thread here by Peter Hansen...
1
6336
by: IceMan85 | last post by:
Hi to all, I have spent the whole morning trying, with no success to pickle an object that I have created. The error that I get is : Can't pickle 'SRE_Match' object: <_sre.SRE_Match object at 0x2a969c0ad0> the complete stack is the following : Traceback (most recent call last): File "manager.py", line 305, in ? commandLineExec...
0
7911
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8200
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
8338
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...
1
7954
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
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
3836
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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 we have to send another system

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.