PEP proposal for round(x,n) enhancement
Christopher Smith
csmith at blakeschool.org
Wed Sep 12 23:01:05 CEST 2001
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). (I mentioned in a
previous
message of this thread how you can get Python to print 0.2 for you in the
case
where you just type 0.2 at the IDE prompt.)
>
>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.
/c
More information about the Python-list
mailing list