Re: Avoiding Overflow
- From: "Jason Doucette" <www.jasondoucette.com>
- Date: Fri, 30 Sep 2005 09:39:42 -0300
Igor,
I can see what you are trying to accomplish. How about this formula:
average of first 2:
avg2 = (x1+x2)/2
average of first 3, using the above:
avg3 = (avg2 + x3/2) * 2 / 3
the divide by 2 is required to give x3 the same weight as x1 and x2, but
this leaves the result: (x1+x2+x3)/2, and we wish to divide this by 3, not
2, thus multiple by 2, then divide by 3.
average of first 4 (same logic):
avg4 = (avg3 + x4/3) * 3 / 4
average of first 5:
avg5 = (avg4 + x5/4) * 4 / 5
etc.
Thus:
((((((x1+x2)/2) + x3/2) * 2/3) + x4/3) * 3/4) + ... + xn/(n-1)) * (n-1)/n
Charles, you could use this, which helps keep the total down during the
calculations, but note that you are still doing x1+x2, which could result in
an overflow. Some people will do this instead: x1/2 + x2/2 to get the
average without having the chance of overflow. The above formula could be
modified to include this precaution, as well.
--
Jason Doucette / Xona.com
www.jasondoucette.com / www.xona.com
.
- Follow-Ups:
- Re: Avoiding Overflow
- From: Igor Tandetnik
- Re: Avoiding Overflow
- References:
- Re: Avoiding Overflow
- From: Jason Doucette
- Re: Avoiding Overflow
- Prev by Date: Re: Avoiding Overflow
- Next by Date: Re: Avoiding Overflow
- Previous by thread: Re: Avoiding Overflow
- Next by thread: Re: Avoiding Overflow
- Index(es):
Relevant Pages
|
|