Re: Rounding errors

From: Richard (riplin_at_Azonic.co.nz)
Date: 09/02/04


Date: 2 Sep 2004 13:21:14 -0700

Robert Wagner <robert@wagner.net.yourmammaharvests> wrote

> >Rounding, as used in Cobol, and in many other languages and in
> >mathematics works on numbers that are assumed to have an infinite
> >number of random digits following the point of rounding, as is usually
> >the case when doing calculations.
>
> Only if the calculation is division. Multiplication does not produce
> an infinite number of digits right of decimal.

Rounding neither knows nor cares whether it is used for division or
multiplication nor what the number of digits in the calculations was.
It is completely irrelevant whether there were actually an infinity of
digits or just the one used for rounding.

> For example, an
> interest calculation multiplies nv99 (balance) by nv9 (rate). The
> answer has precision nv999 and an average, as you point out, of
> n.4995. If we round to cents with Cobol, the average creeps up to
> .5000; if we use Bankers' Rounding, it remains n.4995.

That is not the fault of rounding which neither knows nor cares
whether there are only 3 digits or an infinity of them. Getting the
'right' answer is the responsibility of an informed programmer who
_does_ know and should care how many digits there are of significance.

Whether one method of rounding (or truncation) is used or another is
actually the responibility of the _business rules_.

Regardless of the digits it may be that a Payroll deliberately uses
ceil() or that an interest calculation uses floor() to calculate the
cents.

 
> If the account started with an even dollar amount, say $100.00, all
> digits right of decimal are the result of interest calculation. This
> causes a cascade effect. When we compute interest next month, THIS
> month's interest will round up to .5005.

It is the responsibity of the programmer to ensure that the program
gives the right answer.

Instead of just plucking '.5005' out of ^H^H^H the air, why don't you
show some calculations on your $100.00 that illustrates this
'cascading' that magically changes this months figures next month ?

> > Programmers should be
> >skilled enough to know what their tools can and cannot do and how to
> >choose appropriate tools.
>
> Realistically, how many are? I didn't know the was lower than .500
> until you explained it.

It seemed common enough knowledge to me. I've never done any formal
courses on the subject, no degrees.

> This fallacy is closely related to the argumentum ad populum. It
> consists of asserting that the more people who support or believe a
> proposition, the more likely it is that that proposition is correct.

That was not my argument.

> There's the rub. Cobol programmers routinely round numbers that do not
> have infinite decimals .. things like financial numbers. Cobol is said
> to be a business language. Why is it optimized for scientific use?

It seems to me that you are using the exact 'argument by population'
and the 'Brooklyn Bridge'. '[all] Cobol programmers' do it that way.

   They all do it wrong, so its OK that you did it wrong too.

In fact most businesses use nearest rounding for calculating cents
values and have _always_ done so, with ledgers, with tabulators, with
computers, so the 'routine way' is correct. An alternative is called
'Bankers Rounding' because only the banks did it.



Relevant Pages

  • Re: XQ and ->Qpi bug on large X
    ... you shouldn't be rounding to only two digits ... I am trying to make a similar point here about rounding; ... and round it, ... prior to using the input values in calculations. ...
    (comp.sys.hp48)
  • Re: FAQ 4.2 Why is int() broken?
    ... To express 1/10 in binary you need an infinite ... number of digits, just like you need an infinite number of digits ... Even among different types of computers floating point calculations are ...
    (comp.lang.perl.misc)
  • Re: Rounding errors
    ... With enough digits the average is close enough to ... mathematics works on numbers that are assumed to have an infinite ... number of random digits following the point of rounding, ... What's the point of truncating to ...
    (comp.lang.cobol)
  • Re: FAQ 4.2 Why is int() broken?
    ... To express 1/10 in binary you need an infinite ... number of digits, just like you need an infinite number of digits ... Even among different types of computers floating point calculations are ... is essentially tiny computer with a 6 MHz cpu and yet it can do many ...
    (comp.lang.perl.misc)
  • Re: Rounding errors
    ... >number of random digits following the point of rounding, ... an infinite number of digits right of decimal. ... If we round to cents with Cobol, ...
    (comp.lang.cobol)