Python is DOOMED! Again!

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jan 24 01:35:11 EST 2015


Chris Angelico wrote:

> On Fri, Jan 23, 2015 at 9:59 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:

>> (Don't use binary floating point numbers for anything related to money.
>> Just don't.)
> 
> While I generally agree, it's not quite as hard-and-fast as that. It's
> just important to know that binary floating point can't accurately
> represent many commonly-used real numbers... the same consideration
> that they always need.

I think it is that hard-and-fast.

If you sometimes use floats and sometimes don't use floats, you're forever
worrying about coercing your values from one type to another, worrying
whether the representation error of this value is sufficiently small to use
a float or whether you have to use something else... the whole thing gets
messy and ugly. Or worse, you're *not* worrying about these things, and
when your users complain that their pay is sometimes a cent off and their
account doesn't balance at the end of the year, you "fix the bug" by
arbitrarily adjusting the printing routines.

Better to just have a hard rule: never use binary floats for anything to do
with money, if you care about the result.

Obviously I'm not a fanatic. If I'm working out my share of a $37 meal split
three ways, I just do 37/3 the same as anyone else :-)



-- 
Steven




More information about the Python-list mailing list