This math scares me

Werner Schiendl ws-news at gmx.at
Tue Mar 13 16:11:53 EST 2001


Marcin 'Qrczak' Kowalczyk <qrczak at knm.org.pl> wrote in message
news:slrn9asf1d.ja3.qrczak at qrnik.zagroda...
> Tue, 13 Mar 2001 13:21:50 +0100, Werner Schiendl <ws-news at gmx.at> pisze:
>
> > The most useful approach is to round the numbers to 15 digits for
display,
> > because this is the precision double values can really correctly
represent.
> > The drawback is, that the rounded value does not discover all the
> > information actually stored in the variable.
>
> The most useful approach is to display as much as is needed to read
> the value back, but no more. I.e. repr(float("0.2")) == "0.2", and
> there is no other float value whose repr is "0.2".
>

15 to 16 digits are the precision double values will provide. So rounding to
15 digits will normally display the value you have entered. There is however
no guarantee that your value is displayed exactly. Even if is looks nice in
decimal writing. I did not try for 0.2 (nor any other, because that does not
matter, I guess) but there are numerous values that cannot be converted to
double exactly.

If such a number is converted to its representation and rounded once more,
it may not yield the same value again if it is converted back afterwards.
The use of __repr__, as to my knowledge, is to provide the state of the
object in a way that will evaluate to the same object value.

> This doesn't solve the problem from the beginning of this thread,
> because float("0.1") + float("0.2") needs not be equal to float("0.3").

True, but even 0.1 and 0.2 need not necessarily be exact values.

> But at least all short numbers entered at the prompt don't have those
> unnecessary 00000s or 99999s, and conversion to a string and back
> preserves the value.
>

Non fractional numbers that do not exceed the range of the mantissa will be
stored as exact values.
(At least I think this is so...)

regards
werner






More information about the Python-list mailing list