By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,583 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

pickle and infinity

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
On 2006-11-29, Bart Ogryczak <B.********@gmail.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(self):
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.Unpickler(f)
unpickler.dispatch[pickle.FLOAT] = myload_float
return unpickler.load()
__________________________________________________ ____________________

--
Grant Edwards grante Yow! I represent a
at sardine!!
visi.com
Nov 29 '06 #3

P: n/a
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

P: n/a

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 FloatPointExeption,
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

P: n/a
On 2006-11-29, Bart Ogryczak <B.********@gmail.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 FloatPointExeption,
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

P: n/a

Grant Edwards wrote:
On 2006-11-29, Bart Ogryczak <B.********@gmail.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 FloatPointExeption,
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 FloatPointExeption 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 discussion thread is closed

Replies have been disabled for this discussion.