SA-MP Forums Archive
Saving Averages - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Saving Averages (/showthread.php?tid=447661)



Saving Averages - MP2 - 01.07.2013

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?


Re: Saving Averages - jamesbond007 - 01.07.2013

average = total / count

store average? thats one number lol


Re: Saving Averages - RyDeR` - 01.07.2013

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


Re: Saving Averages - MP2 - 01.07.2013

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.


Re: Saving Averages - RyDeR` - 01.07.2013

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.


Re: Saving Averages - PeppeAC - 01.07.2013

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


Re: Saving Averages - Vince - 01.07.2013

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


Re: Saving Averages - MP2 - 01.07.2013

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