Question about floating point

Gregory Ewing greg.ewing at canterbury.ac.nz
Fri Aug 31 02:45:16 EDT 2018


Steven D'Aprano wrote:
> 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.

It's not clear what you mean by "rounding mode" here. If you
mean whether it's up/down/even/whatever, then yes, you can
probably set that as a default and leave it.

However, as far as I can see, Decimal doesn't provide a
way of setting a default number of decimal places to
which results are rounded. You can set a default *precision*,
but that's not the same thing. (Precision is the total
number of significant digits, not the number of digits
after the decimal point.)

So if you're working with dollars and cents and want all
your divisions rounded to 2 places, you're going to have
to do that explicitly each time.

I don't think this is a bad thing, because often you
don't want to use the same number of places for everything,
For example, people dealing with high-volume low-value goods
often calculate with unit prices having more than 2 decimal
places. In those kinds of situations, you need to know
exactly what you're doing every step of the way.

> We have here a brilliant hammer specially designed for 
> banging in just this sort of nail,

Except that we don't, we actually have an impact screwdriver,
so you've going to have to bring your hammer to deal with
nails properly. And a single size of hammer isn't going to
suit all kinds of nail.

-- 
Greg



More information about the Python-list mailing list