Hi all,
I am using a Picaxe 20x2 and am trying to add multiple 16 bit values and have come up with the following code. I'd be interested to know if what I have come up with is a reasonable way of doing it or not, and if there is anyway I can improve this code to reduce program space consumed?
Aim:
1. Add multiple 16 bit values up to a approximate maximum of 65,535,000
2. Retrieve that value later on in a format I can read back into a single 16 bit variable by losing 3 digits, as accurately as possible, with the last digit rounded. For Example: 65,123,900 would be returned as 65,124
Notes:
Each value is initially calculated and stored in the variable 'Reading' and could be anywhere from 0-65535. (code excluded in this example as not pertinent)
Assume all variables are declared as word variables.
'The following code can store multiple 16 bit values up to a total of 4,294,901,760 using two word variables (ValueLow & ValueHigh) for storage. ValueHigh value of 1 would equal = 65535, ValueLow is simply the base value.
ValueLow = ValueLow + Reading
If ValueLow < Reading then {ValueHigh = ValueHigh + 1} ENDIF
'The following code restores a number up to approx 65,535,000, removing the last 3 digits and rounds the last digit for as much accuracy as possible.
FinalValue = 5 * ValueHigh / 10
FinalValue = ValueLow / 10 + FinalValue
FinalValue = 3 * ValueHigh + FinalValue / 10
TempValue = 5 * ValueHigh + FinalValue // 10 / 5
FinalValue = 5 * ValueHigh + FinalValue / 10 + TempValue
FinalValue = 5 * ValueHigh + FinalValue
FinalValue = 6 * ValueHigh * 10 + FinalValue
I am using a Picaxe 20x2 and am trying to add multiple 16 bit values and have come up with the following code. I'd be interested to know if what I have come up with is a reasonable way of doing it or not, and if there is anyway I can improve this code to reduce program space consumed?
Aim:
1. Add multiple 16 bit values up to a approximate maximum of 65,535,000
2. Retrieve that value later on in a format I can read back into a single 16 bit variable by losing 3 digits, as accurately as possible, with the last digit rounded. For Example: 65,123,900 would be returned as 65,124
Notes:
Each value is initially calculated and stored in the variable 'Reading' and could be anywhere from 0-65535. (code excluded in this example as not pertinent)
Assume all variables are declared as word variables.
'The following code can store multiple 16 bit values up to a total of 4,294,901,760 using two word variables (ValueLow & ValueHigh) for storage. ValueHigh value of 1 would equal = 65535, ValueLow is simply the base value.
ValueLow = ValueLow + Reading
If ValueLow < Reading then {ValueHigh = ValueHigh + 1} ENDIF
'The following code restores a number up to approx 65,535,000, removing the last 3 digits and rounds the last digit for as much accuracy as possible.
FinalValue = 5 * ValueHigh / 10
FinalValue = ValueLow / 10 + FinalValue
FinalValue = 3 * ValueHigh + FinalValue / 10
TempValue = 5 * ValueHigh + FinalValue // 10 / 5
FinalValue = 5 * ValueHigh + FinalValue / 10 + TempValue
FinalValue = 5 * ValueHigh + FinalValue
FinalValue = 6 * ValueHigh * 10 + FinalValue
Last edited: