ValueError: invalid literal for float(): -1.#IND (pickle.py)

Discussion in 'Python' started by Alexander Eisenhuth, Jul 12, 2010.

  1. Hello together,

    python: 2.5.1
    palttform: winXP

    I'm using pickle.dump and pickle.load with data that is created in a wrapped
    (boost.python) piece of C++ code. pickle.dump works fine. pickle.load creates
    the following exception:

    [...]
    data = pickle.load(input)
    File "C:\Python25\lib\pickle.py", line 1370, in load
    return Unpickler(file).load()
    File "C:\Python25\lib\pickle.py", line 858, in load
    dispatch[key](self)
    File "C:\Python25\lib\pickle.py", line 954, in load_float
    self.append(float(self.readline()[:-1]))
    ValueError: invalid literal for float(): -1.#IND

    - I'm not sure what -1.#IND means. Can somebody assist?

    - As pickle write the data I'm a bit confused, that is can't be unpickled it. Is
    that a bug or a feature?

    BTW: I'm tied to version 2.5 of python

    Thank and regards
    Alexander
    Alexander Eisenhuth, Jul 12, 2010
    #1
    1. Advertising

  2. On 2010-07-12, Alexander Eisenhuth <> wrote:

    > python: 2.5.1
    > palttform: winXP
    >
    > I'm using pickle.dump and pickle.load with data that is created in a
    > wrapped (boost.python) piece of C++ code. pickle.dump works fine.
    > pickle.load creates the following exception:
    >
    > [...]
    > data = pickle.load(input)
    > File "C:\Python25\lib\pickle.py", line 1370, in load
    > return Unpickler(file).load()
    > File "C:\Python25\lib\pickle.py", line 858, in load
    > dispatch[key](self)
    > File "C:\Python25\lib\pickle.py", line 954, in load_float
    > self.append(float(self.readline()[:-1]))
    > ValueError: invalid literal for float(): -1.#IND
    >
    > - I'm not sure what -1.#IND means.


    That's an infinity.

    > Can somebody assist?


    In Python 2.x, pickle doesn't handle infinities and NaNs.

    > - As pickle write the data I'm a bit confused, that is can't be
    > unpickled it. Is that a bug or a feature?


    IMO, it's a bug. It's been fixed in 3.x.

    > BTW: I'm tied to version 2.5 of python


    If you want to pickle floating point data that includes infinities and
    NaNs, you need to write your own handler for floating point values.
    When pickling you need to check for infinities and NaNs and output
    some defined strings. When unpickling, you need to check for those
    strings and generate infinities and NaNs as appropriate.

    The CPython 2.x version of pickle just uses the underlying C library
    routines to print and parse floating point values. If those libraries
    are broken in the way that they represent infinity and NaN, then
    pickle is broken in the same manner. Windows standard libraries are
    broken. When formatting floating point values, it produces strings
    that it can't parse as input.

    IIRC, Linux works better -- but if you want something that you know is
    going to work (and work cross-platform), then you need to write your
    own floating point pickle/unpickle methods, and hook them into the
    pickle module.

    --
    Grant Edwards grant.b.edwards Yow! Uh-oh!! I'm having
    at TOO MUCH FUN!!
    gmail.com
    Grant Edwards, Jul 12, 2010
    #2
    1. Advertising

  3. On 2010-07-12, Grant Edwards <> wrote:
    > On 2010-07-12, Alexander Eisenhuth <> wrote:
    >
    >> python: 2.5.1
    >> palttform: winXP
    >>
    >> I'm using pickle.dump and pickle.load with data that is created in a
    >> wrapped (boost.python) piece of C++ code. pickle.dump works fine.
    >> pickle.load creates the following exception:
    >>
    >> [...]
    >> data = pickle.load(input)
    >> File "C:\Python25\lib\pickle.py", line 1370, in load
    >> return Unpickler(file).load()
    >> File "C:\Python25\lib\pickle.py", line 858, in load
    >> dispatch[key](self)
    >> File "C:\Python25\lib\pickle.py", line 954, in load_float
    >> self.append(float(self.readline()[:-1]))
    >> ValueError: invalid literal for float(): -1.#IND
    >>
    >> - I'm not sure what -1.#IND means.

    >
    > That's an infinity.


    Oops, I just noticed that I misread that. 1.#INF is an infinity,
    1.#IND is an "indefinite", or what everybody outside of Microsoft
    calls a NaN (Not-a-Number).

    --
    Grant Edwards grant.b.edwards Yow! ONE LIFE TO LIVE for
    at ALL MY CHILDREN in ANOTHER
    gmail.com WORLD all THE DAYS OF
    OUR LIVES.
    Grant Edwards, Jul 12, 2010
    #3
  4. Alexander Eisenhuth <newsuser <at> stacom-software.de> writes:

    > File "C:\Python25\lib\pickle.py", line 954, in load_float
    > self.append(float(self.readline()[:-1]))
    > ValueError: invalid literal for float(): -1.#IND
    >
    > - I'm not sure what -1.#IND means. Can somebody assist?


    It's the Windows way of representing a NaN (not a number).
    A NaN is typically what you get when you try to perform an
    invalid floating-point operation, like taking the square root
    of a negative number, or dividing 0.0 by 0.0. Some people
    also use NaNs to represent uninitialized values, or as
    placeholders for missing data.

    > - As pickle write the data I'm a bit confused, that is can't
    > be unpickled it. Is that a bug or a feature?


    Well, it's certainly not ideal. It shouldn't be a problem in
    Python 2.6 or 2.7, though; unfortunately, Python 2.5 is no
    longer receiving bugfixes, so it's not going to change there.

    > BTW: I'm tied to version 2.5 of python


    Have you tried using pickle protocol 1 or 2, instead of pickle
    protocol 0? That may well solve your problem. (Those
    protocols write out the binary form of a float directly, instead
    of reading and writing a string representation.)

    --
    Mark
    Mark Dickinson, Jul 12, 2010
    #4
  5. Mark Dickinson schrieb:

    >
    >> BTW: I'm tied to version 2.5 of python

    >
    > Have you tried using pickle protocol 1 or 2, instead of pickle
    > protocol 0? That may well solve your problem. (Those
    > protocols write out the binary form of a float directly, instead
    > of reading and writing a string representation.)
    >


    Thanks a lot, it looks like it solves the problem using another version of the
    protocol

    Regards
    Alexander Eisenhuth, Jul 14, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bd
    Replies:
    0
    Views:
    605
  2. Tim Williams
    Replies:
    2
    Views:
    631
    Tim Williams
    May 28, 2004
  3. =?ISO-8859-2?Q?Martin_MOKREJ=A9?=

    ValueError: invalid literal for int(): 1.0000000000e+00

    =?ISO-8859-2?Q?Martin_MOKREJ=A9?=, Feb 14, 2005, in forum: Python
    Replies:
    1
    Views:
    369
    Grant Edwards
    Feb 14, 2005
  4. Terry Reedy
    Replies:
    0
    Views:
    2,775
    Terry Reedy
    Feb 15, 2005
  5. -dresden.de
    Replies:
    2
    Views:
    501
    Peter Otten
    Mar 12, 2008
Loading...

Share This Page