Re: Computer Statement Usage

From: Robert Wagner (robert_at_wagner.net.yourmammaharvests)
Date: 10/07/04


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 <robert@wagner.net.yourmammaharvests> 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
divide second.



Relevant Pages

  • Re: C vs. Fortran
    ... for example, say, 25 binary bits, or, say, 8 decimal digits ... the only way to specify precision is through the number ... of digits, decimal or binary, that you want. ... With the Fortran inquiry functions, such as DIGITS and PRECISION, ...
    (comp.lang.fortran)
  • Re: Numerical accuracy of C++ and Fortran programs on 32 bit machines
    ... > low-order binary digits are set to zero which results in the case as ... DOUBLE PRECISION D ... to X, but when it is printed out to 15 decimal digits, the inexactness ... compiler should issue a warning, but in my opinion it should not be so ...
    (comp.programming)
  • Re: question on precision
    ... Fortran garantees that 6 digits are correct. ... One is what exactly does a precision() value of 6 mean, ... The precisionvalue tell you how many decimal digits can be ... the internal representation is rounded correctly, ...
    (comp.lang.fortran)
  • Re: Unwanted rounding
    ... might tease you to believe you have precision to 40+ digits. ... can hold a value precise to approximately 17 decimal digits. ... Nothing infinite about it. ...
    (comp.lang.c)
  • Re: Computer Statement Usage
    ... > rather than the calculation. ... > there was a loss in the precision that was specified, ... > the number of decimal digits in 'percentage'. ... > It requires proper numeric analysis to show which is incorrect. ...
    (comp.lang.cobol)