Tags: conversions, differences3, explain, float, floating, lttype, point, programming, python, string

Floating point -> string conversions

On Programmer » Python

5,347 words with 6 Comments; publish: Wed, 30 Apr 2008 10:12:00 GMT; (200109.38, « »)

Can someone explain these differences?

3.1415926535897931

<type 'float'>

'3.1415926535897931'

'3.14159265359'

'3.141593'

'3.1415926535897931'

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

(and is this documented anywhere?)

John.

All Comments

Leave a comment...

  • 6 Comments
    • John Fouhy <jfouhy.python.todaysummary.com.paradise.net.nz> 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

      #1; Wed, 30 Apr 2008 10:13:00 GMT
    • aleaxit.python.todaysummary.com.yahoo.com (Alex Martelli) wrote in message news:<1gmxb89.r6ndtk17sltbgN%aleaxit.python.todaysummary.com.yah

      oo.com>...

      > John Fouhy <jfouhy.python.todaysummary.com.paradise.net.nz> wrote:

      > ...

      > 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.

      #2; Wed, 30 Apr 2008 10:14:00 GMT
    • I have another related question...

      2147483648L

      '2147483647'

      Traceback (most recent call last):

      File "<stdin>", line 1, in ?

      TypeError: int argument required

      '2147483648'

      '2147483648'

      Is this a bug?

      (python 2.3.4)

      John.

      #3; Wed, 30 Apr 2008 10:15:00 GMT
    • John Fouhy wrote:

      > I have another related question...

      >

      > 2147483648L

      >

      > '2147483647'

      >

      > Traceback (most recent call last):

      > File "<stdin>", line 1, in ?

      > TypeError: int argument required

      >

      > '2147483648'

      >

      > '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

      #4; Wed, 30 Apr 2008 10:16:00 GMT
    • Steve Holden <steve.python.todaysummary.com.holdenweb.com> wrote in message news:<t9Skd.57$nj.56.python.todaysummary.com.lakeread01>...[col

      or=darkred]

      > 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?[/color]

      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.

      #5; Wed, 30 Apr 2008 10:17:00 GMT
    • I disagree with '%d' you have explicitly requested that the number be

      turned into an integer for use as a string, so you expect that as long

      as you feed it a number (be it int, long or float) that it changes to a

      string representation of an integer. Now it may be nice to have a flag

      that says how you want to round it.

      --Original Message--

      From: John Fouhy [mailto:jfouhy.python.todaysummary.com.paradise.net.nz]=20

      Sent: Friday, 19 November 2004 2:47 p.m.

      To: python-list.python.todaysummary.com.python.org

      Subject: Re: Floating point -> string conversions

      Steve Holden <steve.python.todaysummary.com.holdenweb.com> wrote in message

      news:<t9Skd.57$nj.56.python.todaysummary.com.lakeread01>...

      > The fact that %s coerces things makes us expect more of other format=20

      > 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') =3D=3D '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" :-) )

      --=20

      John.

      --=20

      http://mail.python.org/mailman/listinfo/python-list

      #6; Wed, 30 Apr 2008 10:18:00 GMT