Posts: 6,236
Threads: 310
Joined: Jan 2011
Reputation:
0
The formula for an average is simple: Total divided by count. My question is, is it possible to store an average with just one value, or do both values (total and count) need to be stored?
Posts: 791
Threads: 65
Joined: Oct 2009
Reputation:
0
average = total / count
store average? thats one number lol
Posts: 2,929
Threads: 160
Joined: Feb 2009
Reputation:
0
In that case, the average IS the total, so no. The count is 1 anyway.
Posts: 6,236
Threads: 310
Joined: Jan 2011
Reputation:
0
But what about when I need to add to it?
If I have the following:
69
42
55
18
32
The average is 216/5 = 43.2
If I then add 64 to that list:
69
42
55
18
32
64
The average is 280/6 = 46.6
But to increase it, I had to store the count (5) and total (216). How do I do it WITHOUT storing the count and total, and just storing the average? Is that even possible? I think not.
Posts: 2,929
Threads: 160
Joined: Feb 2009
Reputation:
0
No, that's not possible. For that, it's enough just to save the count only (if you have the average).
Let's say you have that 43.2, just do ((43.2 * count) + valueToAdd) / (count + 1) and save (count + 1) again.
Posts: 10,066
Threads: 38
Joined: Sep 2007
Reputation:
0
I'd much rather trade memory for speed. Operators are bound to be slower than plain variable calls.
Posts: 6,236
Threads: 310
Joined: Jan 2011
Reputation:
0
I don't need to, I was just wondering if it were possible.