PEP proposal for round(x,n) enhancement

Markus Schaber markus at schabi.de
Wed Sep 12 18:27:04 EDT 2001


Hi,

Christopher Smith <csmith at blakeschool.org> schrub:

> Markus writes:
>>The problem is that you can't store this information in a floating
>>point number. I just tried the following:
>>
>>>>> 0.2
>>0.20000000000000001
>>
>>This, I give the number with 1 significant digit, and end up having
>>displayed 17 "significant" digits.
> 
> Yes and no.  You told Python to show you in decimal format how '0.2'
> is
> represented as a binary number.  If you had said "print 0.2" you would
> have
> obtained 0.2 and if you had said "print 0.2000" you would have
> obtained the same
> thing (even though you wanted 4 significant digits).

And this is what I meant - the float doesn't store how many digits are 
significant, so when you round to n digits, you often get an 
approximation to the rounded number, and this way don't have any 
information about the accuracy afterwards.

>>So you may only round (or whatever we call it) to powers of two, but
>>not to powers of 10 (digits) without having rationals or decimals.
> 
> Yes, you are right in that there is no way to exactly represent some
> numbers
> exactly as floating point numbers but this has nothing to do with
> rounding a
> number (to the closest approximation that you can) to a specific
> digit. This is
> the task of the round() function.  My proposal only has to do with
> enhancing the
> syntax used by the round() function.

And enhancing the semantics :-)

markus
-- 
"The strength of the Constitution lies entirely in the determination of 
each citizen to defend it. Only if every single citizen feels duty 
bound to do his share in this defense are the constitutional rights 
secure." -- Albert Einstein



More information about the Python-list mailing list