Decimal arithmetic, with example code

Tim Peters tim.one at comcast.net
Wed Oct 2 14:46:55 EDT 2002


[Tim]
>> BTW, note that the result of expressions like
>>
>>     %0.2f" % float_res
>>
>> is platform-dependent!

[Paul Boddie]
> That's why I wrote...
>
> """
> When the floating point result is only a "wafer thin mint" below 0.35
> (on this Pentium III laptop running Windows 2000), the rounded,
> truncated result is given as 0.3. Such conditions could be seen to be
> a flaw when attempting to highlight problems with the FixedPoint
> implementation.
> """

That's a different issue.  The results of the given expression are
platform-dependent even if float_res is exactly representable in binary fp.
For example,

>>> '%0.2f' % 1.125
'1.13'
>>>

on Windows but produces '1.12' on most other platforms.  This doesn't have
to do with representation error (decimal 1.125 is exactly representable in
binary fp), it has to do with tbe platform sprintf's *decimal* rounding
policy in "exactly halfway" cases.  That varies across platforms, even with
identical FP HW.





More information about the Python-list mailing list