Re: Rounding error in Stdev function result.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



joeu2004@xxxxxxxxxxx wrote...
....
>I understand. But the implementation of STDEV() could
>easily compute the mean internally, taking advantage of
>the 80-bit FP registers. It does not. Nor does AVERAGE(),
>as you point out. But the OP asked about STDEV(), not
>AVERAGE(). I tailored my comments to the OP's context.

80 bits wouldn't necessarily help in this case. If the mean is
calculated by summing all the values first then dividing by the count,
the sum itself could cause problems that dividing by the count wouldn't
necessarily solve. In an ideal world, cleverness would also be applied
to calculating the mean.

k = 0
n = 0
For Each v In d
n = n + 1
If n = 1 Then m = v Else m = m * (k / n) + v / n
k = n
Next v

Enter 1.4434 in A1:A3. Enter 1 in B1, 0 in C1 and =A1 in D1. Enter
=B1+1 in B2, =B1 in C2, =D1*(C2/B2)+A2/B2 in D2. Select B2:D2 and fill
down into B3:D3. Cell D3 contains the mean. Enter =AVERAGE(A1:A3) in
D4, and =(D3-D4) in D5. D5 evaluates to -2.22045E-16.

Enter =SUMSQ(A1-D4,A2-D4,A3-D4) in D6. It evaluates to 1.47911E-31. Now
enter =SUMSQ(A1-D3,A2-D3,A3-D3) in D7. It evaluates to 0.

Imagine that! Rounding error elimiated without using 80-bit FPU
registers!

Further, even calculating the mean as =(1.4434/3+1.4434/3+1.4434/3)
would eliminate the rounding error. Try
=SUMPRODUCT((A1:A3-SUMPRODUCT(A1:A3/3))^2).

The error is due *EXCLUSIVELY* to rounding error in the calculation of
the mean; however, it's possible to eliminate the rounding error
without using more bits of precision. For completeness, it's necessary
to understand that the order of operations is *ESSENTIAL* in floating
point arithmetic. While floating point addition and multiplication are
commutative, neither is associative, and the distributive law doesn't
hold (that is, all the time; there are fortuitous exceptions). In
floating point arithmetic it's usually best to perform multiplication
first, then addition if accuracy is more important than execution speed.

.



Relevant Pages

  • Re: problem with float
    ... i found the value stored is not the exact value which i gave. ... The floating point numbers are a subset of the real numbers. ... Usually the subset is based on a binary representation and so most non ... expect that a rounding error won't be introduced during the conversion to ...
    (comp.lang.c)
  • Re: Floating point rounding error
    ... Why does floating point have a rounding error? ... flaot f = 1234.12345678F; ... It doesn't have a rounding error. ... Posted via a free Usenet account from http://www.teranews.com ...
    (comp.lang.c)
  • Re: [PHP] =.= what wrong ? just simple code, however error.
    ... LKSunny wrote: ... It's a floating point rounding error. ... just round it to what you need. ...
    (php.general)
  • Re: ARGH! Access cant add up?!?
    ... > The error you're seeing is called floating point error. ... > there's some rounding error. ... Bugger. ... Andy ...
    (microsoft.public.access.forms)