More random python observations from a perl programmer

Christian Tismer tismer at appliedbiometrics.com
Thu Aug 19 11:12:20 EDT 1999


Tom Christiansen wrote:

[snipped a long list which I commented via private emails already]

> GOTCHA: (low)
>     Python has a round() function, but it seems to ignore the
>     IEEE rules of rounding.  However, its sprintf operator doesn't:
>         >>> print "int of %f is %.0f" % (1.5, 1.5)
>         int of 1.500000 is 2
>         >>> print round(1.5,0)
>         2.0
>         >>> print "int of %f is %.0f" % (2.5, 2.5)
>         int of 2.500000 is 2
>         >>> print round(2.5,0)
>         3.0

I had a look at this again. Despite the fact that the text in
the above example is a bit misleading by "int of" where
a round() takes place, which version of Python/architecture
did you use? I get the expected result, see below.

>>> print "round of %f is %.0f" % (1.5, 1.5) 
round of 1.500000 is 2
>>> print round(1.5,0)
2.0
>>> print "round of %f is %.0f" % (2.5, 2.5)
round of 2.500000 is 3
>>> print round(2.5,0)
3.0
>>> 

>     And I'd jolly well like to know why I wasn't allowed to use
>         print "int of %f is %.0f" % (2.5) * 2
>     or if needed,
>         print "int of %f is %.0f" % ( (2.5) * 2 )

This is just a matter of commas needed for singleton tuples,
and operator precedence, yielding

>>> print "round of %f is %.0f" % ((2.5,) * 2)
round of 2.500000 is 3
>>> 

ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home




More information about the Python-list mailing list