Saving Averages
#1

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?
Reply
#2

average = total / count

store average? thats one number lol
Reply
#3

In that case, the average IS the total, so no. The count is 1 anyway.
Reply
#4

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.
Reply
#5

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.
Reply
#6

If you don't need 32 bits for one value use this to store the two values into one integer: https://sampforum.blast.hk/showthread.php?tid=275142
Reply
#7

I'd much rather trade memory for speed. Operators are bound to be slower than plain variable calls.
Reply
#8

I don't need to, I was just wondering if it were possible.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)