Question about floating point

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Aug 30 09:16:01 EDT 2018


On Thu, 30 Aug 2018 19:22:29 +1200, Gregory Ewing wrote:

> Steven D'Aprano wrote:
>> Why in the name of all that's holy would anyone want to manually round
>> each and every intermediate calculation when they could use the Decimal
>> module and have it do it automatically?
> 
> I agree that Decimal is the safest and probably easiest way to go, but
> saying that it "does the rounding automatically" is a bit misleading.
> 
> If you're adding up dollars and cents in Decimal, no rounding is needed
> in the first place, because it represents whole numbers of cents exactly
> and adds them exactly.

"Round to exact" is still rounding :-P

I did already say that addition and subtraction was exact in Decimal. (I 
also mentioned multiplication, but that's wrong.)


> If you're doing something that doesn't result in a whole number of cents
> (e.g. calculating a unit price from a total price and a quantity) you'll
> need to think about how you want it rounded, and should probably include
> an explicit rounding step, if only for the benefit of someone else
> reading the code.

If you're not using Banker's Rounding for financial calculations, you're 
probably up to no good *wink*

Of course with Decimal you always have to option to round certain 
calculations by hand, if you have some specific need to. But in general, 
that's just annoying and error-prone book-keeping. The right way is to 
set the rounding mode at the start of your application, and then let the 
Decimal type round each calculation that needs rounding.

The whole point of Decimal, the reason it was invented, was to do this 
sort of thing. We have here a brilliant hammer specially designed for 
banging in just this sort of nail, and you're saying "Well, sure, but you 
probably want to bang it in with your elbow, if only for the benefit of 
onlookers..."

:-)


-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list