Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Floating point -> string conversions

Reply
Thread Tools

Floating point -> string conversions

 
 
John Fouhy
Guest
Posts: n/a
 
      11-08-2004
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.
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      11-08-2004
John Fouhy <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
John Fouhy
Guest
Posts: n/a
 
      11-10-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Alex Martelli) wrote in message news:<1gmxb89.r6ndtk17sltbgN%(E-Mail Removed)>...
> John Fouhy <(E-Mail Removed)> 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.
 
Reply With Quote
 
John Fouhy
Guest
Posts: n/a
 
      11-11-2004
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.
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      11-11-2004
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
 
Reply With Quote
 
John Fouhy
Guest
Posts: n/a
 
      11-19-2004
Steve Holden <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
floating point conversions && how to read standards Stanley Rice C++ 4 10-08-2011 07:30 AM
Integer to Floating-Point conversions Kai Koehne Java 10 05-15-2006 03:21 PM
Re: floating point error in file conversions oberon@oby.net Computer Support 1 07-15-2004 12:50 AM
floating point error in file conversions oberon@oby.net Computer Support 1 07-14-2004 10:43 PM



Advertisments