Decimal arithmetic, with example code

Paul Boddie paul at boddie.net
Wed Oct 2 05:37:33 EDT 2002


Tim Peters <tim.one at comcast.net> wrote in message news:<mailman.1033498683.13204.python-list at python.org>...
> 

[Example results]

> I don't see what this shows beyond that FixedPoint implements "banker's
> rounding" (round the infinitely precise result to the nearest retained
> digit, but in case of tie round to the nearest even number), while you seem
> to prefer "add a half and chop" rounding.

Well, ScaledDecimal attempts to implement the kind of rounding that
everyone learns in primary school. I think this is intuitive to most
people, although I'm willing to accept that indoctrination in the
banking sector can distort some people's expectations. ;-)

> No single rounding discipline is suitable for all commercial applications,
> and I hope the FixedPoint project finds a way to let users specify the
> rounding *their* app needs. Unfortunately, I was never able to find a
> FixedPoint user who was able to articulate the rounding rules they were
> trying to emulate <wink/sigh>.

Clearly, the class has to be configurable enough. The debate around
what people expect from decimal arithmetic or decimal data types has
demonstrated the need for a certain amount of flexibility.

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

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.
"""

In other words, the use of the floating point arithmetic, the built-in
'round' function, and various formatting operators isn't a reliable
way of showing whether something (the floating point result) is
correct, let alone something else (the FixedPoint result). Indeed, not
having written the original test program, I can safely say that I
wouldn't have used floating point arithmetic to demonstrate anything
meaningful. <0.029999999999999999-wink>, as ActivePython 2.1.3 Build
214 on win32 says.

Paul



More information about the Python-list mailing list