Continue to Site

# Anyone any good at maths???

Status
Not open for further replies.

#### olly_k

##### Member
Ok, I am writing a program that deals with entering numbers. The program amongst other things also works out the average of the last 5 or 6 numbers entered or so - I havent decided how many yet... this will depend on how much free memory I have. Anyway, what I want to do is this - from the final 'average', I would like to work out some sort of value that I can enter into the pic the next time I power up, which will continue working out the entire average of the previous numbers plus any new numbers entered. Sort of like a sead value. Is this mathamatically possible or can averages only be worked out sort of 'number by number' so-to speak?
Do you all understand what I am going on about and if so can you help?

Hey, yeh doing devision in PICs can be quite hard as there is no floating point support nativly, thou on www.piclist.com there are lots of mathematical functions.

If u need to work out the average of lots of numbers, u will enounter a few problems, (in ASM most C compilers take care of these for u!)

I would recomend if u could making your needs so that u only average say 2s or 4s, that way u could devide with a simple rotate right (ie, one rotate right file devides by 2, 2 RRF devides by 4.) That would give you a nice rounded down awnser.

:idea:
if u say wanted to know the mean average value of say 256 pecies of data you could add them together like so
Code:
MOVF   INCOMINGDATA, W
BTFSS   STATUS, C
GOTO   RESTOFCODE
INCF   MEANLSB, F
BTFSC   STATUS, C
INCF  MEANMSB, F
RESTOFCODE:
;ur code!
once u'r done there, u will have the mean average of 256 bits of data, in MEANLSB and MEANMSB. The MEANTEMP would have the data after the decimal point, alough u prolbably would discard it, u can Bit Test bit 7 of MEANTEMP to determine how u should round it, ie, round up if it was .5

Hope that helps.

I had thought about the 2's and 4's bit and yes I am concerened about the decimal point as I can see errors creaping in their. The only reason I havent used the pic list is I hate it's layout and navigation lol! I guess that's the lazy side of me!
Thanks all the same I will have to do some more homework on this... Maybe even post a q on the pic list.... Why can't they have a simple forum type layout - make life a lot easier!

I don't know how to do this on a PIC yet, but to save a running average (i.e. before power down), you need to save the average AND the number of entries that make up that average.

example:

; clear average
sum=0 ; n= 0

; now take data
sum+= item0 ; n++
average = sum/n
sum+= item1 ; n++
average = sum/n
sum+= item2 ; n++
average = sum/n

; power down
write average to flash
write n to flash

let's say you enter the values 2,4,6. then sum==12, n==3, and average==4. When you power down, you will save the average and n into non volatile memory (ie flash or eeprom). when you start up again, you will preload the sum with the old average multiplied by the old n.

; restore old average
sum= average * n

; now take more data
sum+= item0 ; n++
average = sum/n
sum+= item1 ; n++
average = sum/n
sum+= item2 ; n++
average = sum/n

----

of course, this will suffer from the problem that sum will eventually overflow. ... there are other methods to deal with this. one way is to "mix" the new values into the old average:

average = 87.5% average + 12.5% new value

you can do this forever. on the PIC, you could probably do something like

temp = average*7
temp += new value
temp += 4; /* this forces the answer to be rounded off properly */
average = temp >> 3; /* divide by 8 */

Thanks for help guys, what I was originally wondering was if you could work out a seed value it has become obvious you can't do this without saving total and dividend. What I mean't by this if you got an average, wrote it on a peice of paper and then entered it to the computer next time it would continue working out the total average of first and consequtive visits. I realise now averaging doesn't work like that!
The approach I have took instaed of dividing I am using subracting (total - dividend) until zero or negative answer then a small routine to round up or down. Real simple and it worked first time! That is very unusual for me lol!

qromodyn said:
I don't know how to do this on a PIC yet, but to save a running average (i.e. before power down), you need to save the average AND the number of entries that make up that average.
You don't need to save the average of last numbers. All you have to do is recalculate the average each time there is a new entry and save this new entry. Whenever required, recalculate the average and display it or do whatever manipulations required. This will save some memory.

For floating point calculations, use PIC C compilers.

Status
Not open for further replies.

Replies
10
Views
3K
Replies
4
Views
2K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
31
Views
4K