Question about floating point
Gregory Ewing
greg.ewing at canterbury.ac.nz
Thu Aug 30 03:22:29 EDT 2018
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.
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.
--
Greg
More information about the Python-list
mailing list