Re: Computer Statement Usage
From: Robert Wagner (robert_at_wagner.net.yourmammaharvests)
Date: Thu, 07 Oct 2004 16:54:08 GMT
On 7 Oct 2004 00:01:35 -0700, riplin@Azonic.co.nz (Richard) wrote:
>Robert Wagner <firstname.lastname@example.org> wrote
>> >compute percentage = amount-one / (this + that) * 100.
>> That's incorrect. It should be compute percentage = (100*
>> amount-one)/(this + that). Your way loses precision.
>It is the assignment to percentage will most likely 'lose precision'
>rather than the calculation. Donald's would only be 'incorrect' if
>there was a loss in the precision that was specified, as indicated by
>the number of decimal digits in 'percentage'.
>It requires proper numeric analysis to show which is incorrect. For
>example if 'amount-one' is large enough then your way could exceed the
>value allowable in the intermediate result leading to the completely
>wrong answer, while Donald's may give a correct result always if
>'percentage' is, say, PIC S9(m)V99.
Assuming intermediates are fixed-point ..
The number of digits left of decimal is easy for the compiler to
determine, but the number of digits to the right is indeterminate. It
is often infinite. For this reason, it is better to multiply first and