Array and floating point

Discussion in 'Python' started by Jonathan Shan, Aug 17, 2007.

  1. Hello,

    I'm experiencing a problem where the float being appended to the array
    is not the same as the result of the appending.

    >>> from array import *
    >>> x = array('f')
    >>> x.append(float("0.1"))
    >>> x[0]

    0.10000000149011612
    >>> float("0.1")

    0.10000000000000001

    I'm expecting x[0] = 0.10000000000000001

    Thanks
    Jonathan Shan
     
    Jonathan Shan, Aug 17, 2007
    #1
    1. Advertising

  2. Jonathan Shan

    Zentrader Guest

    On Aug 17, 2:06 pm, Jonathan Shan <> wrote:
    > Hello,
    >
    > I'm experiencing a problem where the float being appended to the array
    > is not the same as the result of the appending.
    >
    > >>> from array import *
    > >>> x = array('f')
    > >>> x.append(float("0.1"))
    > >>> x[0]

    > 0.10000000149011612
    > >>> float("0.1")

    >
    > 0.10000000000000001
    >
    > I'm expecting x[0] = 0.10000000000000001
    >
    > Thanks
    > Jonathan Shan


    Floating point precision problems on x86 type machines is well
    documented on the web if you want to know more about it. For your
    example use Python's decimal class instead of floating point. Gmpy is
    also available for scientific type apps,
    http://docs.python.org/lib/module-decimal.html
    http://pydoc.org/2.4.1/decimal.html
    http://gmpy.sourceforge.net/
    import decimal
    x = decimal.Decimal( "0.1" )
    print x
    y = decimal.Decimal( "0.10000000000000001" )
    print y
    print y/x
    print y*x
     
    Zentrader, Aug 17, 2007
    #2
    1. Advertising

  3. Jonathan Shan

    Peter Otten Guest

    Jonathan Shan wrote:

    > Hello,
    >
    > I'm experiencing a problem where the float being appended to the array
    > is not the same as the result of the appending.
    >
    >>>> from array import *
    >>>> x = array('f')
    >>>> x.append(float("0.1"))
    >>>> x[0]

    > 0.10000000149011612
    >>>> float("0.1")

    > 0.10000000000000001
    >
    > I'm expecting x[0] = 0.10000000000000001


    array("f") is an array of C floats while Python's float type is a double in
    C terms. That's why you lose some precision. Try array("d") instead:

    >>> from array import array
    >>> x = array("d")
    >>> x.append(0.1)
    >>> x[0]

    0.10000000000000001


    Peter
     
    Peter Otten, Aug 17, 2007
    #3
  4. Jonathan Shan

    Robert Kern Guest

    Jonathan Shan wrote:
    > Hello,
    >
    > I'm experiencing a problem where the float being appended to the array
    > is not the same as the result of the appending.
    >
    >>>> from array import *
    >>>> x = array('f')
    >>>> x.append(float("0.1"))
    >>>> x[0]

    > 0.10000000149011612
    >>>> float("0.1")

    > 0.10000000000000001
    >
    > I'm expecting x[0] = 0.10000000000000001


    'f' denotes a single-precision floating point number. Python's float objects are
    double-precision floating point numbers. Use 'd' instead.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Aug 17, 2007
    #4
    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. H aka N
    Replies:
    15
    Views:
    15,829
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,554
  3. Replies:
    4
    Views:
    1,335
    Default User
    Feb 22, 2006
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,417
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    294
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page