summation question
I do not understand how to use numbers over a Word's 16-bit size limit. I want to poll a sensor every 10th of a second, get a number like 3,000, and average the readings over lets say 5 minutes. The total of the averages exceeds a Word's size very quickly. Can someone help or give me a good link??? Thank you!!

Comments
1. Add your reading to the lower 16-bit word. If the result is lower than it was before the add, that means there was a carry out of the high bit, so increment the high-order 16-bit word by one.
2. Do this 16384 times (a power of two, or about 5.46 minutes worth of data.)
Your average will be the lower 14 bits of the high word, concatenated with the upper 2 bits of the low word. You can shift the high word left by two bits and the low word right by 14 bits, then bitwise "or" the two together to get the average in one 16-bit word.
-Phil
Simplest would be to use two words for double precision math. When word one has a carry increment word 2 by 1.
It would also be simpler if you use a power of two for the number of samples to average so you can use a shift instead of a divide to do the averaging.
What uC and language are you using?
-Phil
That was my guess as well. Looks like our responses crossed in the ether.
FOR x= 1 TO 16384
sensor=3000
IF lowword < sensor + lowword THEN highword = highword + 1
ELSE lowword=lowword + sensor
ENDIF
PAUSE 100
DEBUG, highword << 2 (OR???) lowword >> 14
NEXT x
END
sensor=3000
FOR x= 1 TO 16384
IF sensor + lowword < lowword THEN highword = highword + 1
ENDIF
lowword=lowword + sensor
PAUSE 100
NEXT x
DEBUG, highword = highword << 2 + lowword >> 14
END
' {$STAMP BS2} ' {$PBASIC 2.5} i VAR Word high_sum VAR Word low_sum VAR Word sensor VAR Word average VAR Word high_sum = 0 low_sum = 0 FOR i = 1 TO 16384 GOSUB read_sensor IF (low_sum + sensor < low_sum) THEN high_sum = high_sum + 1 low_sum = low_sum + sensor NEXT average = (HIGH_sum << 2) | (low_sum >> 14) DEBUG DEC average read_sensor: 'Read sensor value. RETURNBTW, guys:-Phil
There is something of a tutorial on double precision math with the Stamp at http://emesystems.com/BS2math6.htm.
It shows methods for using non-power-of-two divisors like 3000 (5 minutes worth of samples at 0.1 sec spacing).
Yes, of course you're right. 'Fixed.
Tracey,
Thanks for chiming in. It's always a relief to have your input on BASIC Stamp math questions!
-Phil
Average4: ' -- entry: ' wx = current sample ' wj = current number of samples ' wpe1=address of 2 contiguous words (32 bits) allocated in scram for accumulator ' -- exit: ' wx = current average value of wx * 10 ' -- uses for computation: ' wy, wz wx=wx*10 ' average computed on wx*10 for possible interpolation GET wpe1,WORD wy ' low word of accumulation wy=wy+wx PUT wpe1,WORD wy GET wpe1+2, WORD wz ' high word of accumulation IF wy<wx THEN wz=wz+1 PUT wpe1+2,WORD wz ENDIF ' compute double precision division, wz:wy / wj wx=65535//wj*wz//wj+wz+(wy//wj) wx=65535/wj*wz+(65535//wj*wz/wj)+(wy/wj)+(wx/wj) RETURN