Floating point -> string conversions

Discussion in 'Python' started by John Fouhy, Nov 8, 2004.

  1. John Fouhy

    John Fouhy Guest

    Can someone explain these differences?

    >>> from math import pi
    >>> pi

    3.1415926535897931
    >>> type(pi)

    <type 'float'>
    >>> repr(pi)

    '3.1415926535897931'
    >>> str(pi)

    '3.14159265359'
    >>> '%f' % pi

    '3.141593'
    >>> '%.16f' % pi

    '3.1415926535897931'

    Why do the string and default floating point representations get rounded?

    (and is this documented anywhere?)

    --
    John.
    John Fouhy, Nov 8, 2004
    #1
    1. Advertising

  2. Alex Martelli, Nov 8, 2004
    #2
    1. Advertising

  3. John Fouhy

    John Fouhy Guest

    (Alex Martelli) wrote in message news:<1gmxb89.r6ndtk17sltbgN%>...
    > John Fouhy <> wrote:
    > ...
    > > Why do the string and default floating point representations get rounded?
    > >
    > > (and is this documented anywhere?)

    > Sure, right in the "Tutorial (start here)" for example.
    > http://www.python.org/dev/doc/devel/tut/node16.html
    > Alex


    I knew I should have looked in the tutorial, rather than just the
    "numeric types" section of the library reference :-/

    Actually, that page doesn't mention that '%f' will give you a
    different rounding again...

    --
    John.
    John Fouhy, Nov 10, 2004
    #3
  4. John Fouhy

    John Fouhy Guest

    I have another related question...

    >>> pow(2, 31)

    2147483648L
    >>> '%d' % 2147483647.0 # python will convert to int

    '2147483647'
    >>> '%d' % 2147483648.0 # too big for an int, so error.

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: int argument required
    >>> '%d' % long(2147483648.0) # but yet, no trouble accepting a long.

    '2147483648'
    >>> '%d' % int(2147483648.0) # and int() converts to long anyway

    '2147483648'

    Is this a bug?

    (python 2.3.4)

    --
    John.
    John Fouhy, Nov 11, 2004
    #4
  5. John Fouhy

    Steve Holden Guest

    John Fouhy wrote:

    > I have another related question...
    >
    >
    >>>>pow(2, 31)

    >
    > 2147483648L
    >
    >>>>'%d' % 2147483647.0 # python will convert to int

    >
    > '2147483647'
    >
    >>>>'%d' % 2147483648.0 # too big for an int, so error.

    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > TypeError: int argument required
    >
    >>>>'%d' % long(2147483648.0) # but yet, no trouble accepting a long.

    >
    > '2147483648'
    >
    >>>>'%d' % int(2147483648.0) # and int() converts to long anyway

    >
    > '2147483648'
    >
    > Is this a bug?
    >
    > (python 2.3.4)
    >

    I don't think so. The fact of the matter is that a %d format token
    explicitly expects an integral value.

    The fact that %s coerces things makes us expect more of other format
    tokens, but what would you expect %d to do with 2147483648.5?

    regards
    Steve
    --
    http://www.holdenweb.com
    http://pydish.holdenweb.com
    Holden Web LLC +1 800 494 3119
    Steve Holden, Nov 11, 2004
    #5
  6. John Fouhy

    John Fouhy Guest

    Steve Holden <> wrote in message news:<t9Skd.57$nj.56@lakeread01>...
    > The fact that %s coerces things makes us expect more of other format
    > tokens, but what would you expect %d to do with 2147483648.5?


    I'd expect it to give me "2147483648", since that would be consistent
    with its other behaviour :)

    (eg: eval('"%d" % 1.5') == '1')

    I guess what annoys me is that python encourages you to not worry
    about the difference between ints and longs (which is great) (unless
    you're doing high performance computing), but then something like this
    can cause code to stop working just because you've passed a border
    which is almost invisible.

    (of course, this means that one solution which I would be happy with
    is for python to reject any non-integer/long arguments to "%d" :) )

    --
    John.
    John Fouhy, Nov 19, 2004
    #6
    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. Kai Koehne
    Replies:
    10
    Views:
    1,157
    Patricia Shanahan
    May 15, 2006
  2. Replies:
    10
    Views:
    706
    Jasen Betts
    Aug 5, 2005
  3. Stanley Rice
    Replies:
    4
    Views:
    377
    Stanley Rice
    Oct 8, 2011
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,322
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    255
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page