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
BTW, 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