results of division

Christopher A. Craig com-nospam at ccraig.org
Thu Dec 9 12:57:30 EST 2004


"Paul McGuire" <ptmcg at austin.rr._bogus_.com> writes:

> Errr?  How come round() is able to understand 1.775 correctly, whereas
> string interp is not?  I'm guessing that round() adds some small epsilon to
> the value to be rounded, or perhaps even does the brute force rounding I
> learned in FORTRAN back in the 70's:  add 0.5 and truncate.  But this would
> still truncate 1.779999999 to two places, so this theory fails also.  What
> magic is round() doing, and should this same be done in the string interp
> code?

Consulting bltinmodule.c would tell you.  round(x,n) in (Python 2.4):

multiplies x by 10**n
adds .5
truncates
divides by 10**n.

Don't confuse this trick with giving us the correct result though,
it's still floating point:

>>> round(1.77499999999999999, 2)
1.78

-- 
Christopher A. Craig <com-nospam at ccraig.org>
"[Windows NT is] not about 'Where do you want to go today?'; it's more like
'Where am I allowed to go today?'" -- Mike Mitchell, NT Systems Administrator




More information about the Python-list mailing list