Forum Discussion
List Generate Average as record;
- 8 months ago
I'm not sure what you are asking here.
Clearly in the code z and y are always the same.
But what is this? It makes no sense. Where are you getting this from?
z / 2 <> z = y/2
You can't say a thing (z/2) is not equal (or equal) to another thing (z=y/2).
You can't have an assignment on one side of a logical test like this
Again not sure how esle to explain this other than how I have.
z / 2 <> z = y/2 makes no sense mathematically and I don't know what you are trying to do there.
Phil
The key point: [avg] is already an average, not a sum. So adding the next value to it and dividing by x again is mathematically wrong.
What each version does
Working version
avg = asum / xHere asum is the running sum of the first x numbers, so asum / x is the correct running average.
Non‑working version
avg = ( [avg] + alist{x-1} ) / xavg at step x-1 is already (sum of first x-1 values) / (x-1). When you do avg + alist{x-1}, you are adding a mean to a raw value, not to the sum, so the result is no longer proportional to the true sum. Dividing this by x cannot fix it, so the running average drifts away from the correct value.
To keep a running average with List.Generate, always maintain a running sum (like asum) and compute avg = asum / x or recompute avg from that sum each step.
- Dicken8 months agoPost Prodigy
so let me see if i'm coming clsose becasue if i take
= let alist = {1..10} in List.Generate( ()=> [ x = 1 , y = alist {0} , z = alist {0} ] , each [x] <= List.Count( alist ) , each [ x = [x] + 1, y = [y] + alist {x-1} , z = ( [z] + alist {x-1} ) ] )both x and y = 6 at the third step, but if id divide ( [z] + alist {x-1} ) / 2 = 2.25, ,
are you saying it is because it is part of an 'on going' accumulation and not an actual salcer value
than can be divded ?- jgeddes8 months agoSuper User
You are correct. It is an ongoing accumulation.
So when you write avg = ([avg] + alist{x-1})/x you would be storing the value you are calculating with that expression into the avg variable. That variable value is then used in the calculation in the next iteration in the list.