for / while else doesn't make sense

Jon Ribbens jon+usenet at unequivocal.co.uk
Tue May 24 16:10:38 EDT 2016


On 2016-05-24, Steven D'Aprano <steve at pearwood.info> wrote:
> On Tue, 24 May 2016 08:54 pm, Jon Ribbens wrote:
>> On 2016-05-24, Steven D'Aprano <steve at pearwood.info> wrote:
>>> On Tue, 24 May 2016 03:09 am, Jon Ribbens wrote:
>>>> On 2016-05-23, Steven D'Aprano <steve at pearwood.info> wrote:
> [...]
>>> In Australia, we have an 11% consumption tax, the GST. I cannot tell you
>>> how many times I've needed to add a 1 cent "Rounding" amount on invoices
>>> to get the results to work out correctly.
>> 
>> Indeed. Using floats for currency calculations is one of the many
>> traps they present, and an excellent example of why their use should
>> not be encouraged.
>
> How do you know the software uses floats?

What software?

> But worse, you ignored my example where I showed that using integer
> arithmetic also generates the same kind of off by one cent errors.

Yes, you showed that you can write broken code. Well done.

>> I very much doubt that you have any such choice - it will usually
>> be specified by the tax regulations.
>
> This is not a matter about the GST legislation. It is a matter of
> mathematics that using integer or fixed point arithmetic is vulnerable to
> the same sorts of rounding errors as floating point arithmetic.

Except it isn't - floats are far more complicated.

>> The correct way to do currency stuff is either integers or Decimal.
>
> You're repeating my words back at me. I already said that.

No, you said the quote that I left in which is still here below:

>>> Using integers in this case is not only *harder* than using floats, but
>>> it's more likely to go wrong. Because integer division always rounds
>>> down, errors accumulate faster than with floats, where calculations will
>>> be correctly rounded to minimize the error.

If that isn't you saying that currency stuff should be done using
floats, then please explain what you did mean, because that's what
it looks like you're saying to me.

>> You are falling into the float trap again. This is not how you do
>> accounting.
>
> Did you look at the code I showed? I'm not using floats. I'm using
> integers, counting in cents.

You didn't say you were using anything. You showed some broken
sample code that you had deliberately written to give the wrong
result, and then said people should use float instead.

> If you're just going to automatically gainsay everything I say
> without even reading what I say first, well, you're no John Cleese.

I did read what you said. Perhaps you should try the same.



More information about the Python-list mailing list