What Can I Do When My Word Variable Overflows???

johnmobley

New Member
I have a problem with some math operations. I understand what is going on, but I dont know how to overcome the problem.

I have an ADC value that I need to convert to a corresponding weight. In the real world the formula for this is:

ADC / 1.023 = Weight

I know that I can not work with decimals with the PICAXE so I multilpied the formula by 1000 to get rid of the decimal for a formula of:

(ADC * 1000) / 1023 = Weight

This works great upto an adc value of 65. At an adc value of 66 I get an overflow issue. I know that this happens because the "word" range is 0-65535 and my formula creates a value of 66000. I am using readadc10 and so my adc range is going to go to 1023 and this is going to leave most of my range unreadable/unusable.

What can I do to overcome this issue other than sacrificing accuracy? :confused:
 

womai

Senior Member
One way:

1000/1023 = (1023 - 23) / 1023 = (1023/1023) - (23/1023) = 1 - 23/1023

so your formula becomes

weight = ADC - ADC * 23 / 1023

In Picaxe code (untested!):

weight = ADC * 23 / 1023
weight = ADC - weight

No more overflows! If you rather round the value (as opposed to just cutting off decimals), make that

weight = ADC * 23 + 512 / 1023
weight = ADC - weight

Wolfgang
 

johnmobley

New Member
Ok...thank you for the solution to the problem. This does indeed fix the issue.

However, I do not understand the math here. Can you explain what you are doing here so that if I need to I can apply it in other projects...
 

MPep

Senior Member
Maths are strictly Left to Right.
Therefore
weight = ADC - ADC * 23 / 1023

weight = ADC * 23 / 1023
weight = ADC - weight
These first calculation MUST be used as per the 2nd set of lines.Otherwise you would end up with
weight = 0 (ADC-ADC = 0, 0*23 = 0, 0/1023 = 0)
Hope this makes sense to you.
 

johnmobley

New Member
I understand that maths are strictly left to right. What I am confused about is where/why did womai come up with:

1000/1023 = (1023 - 23) / 1023 = (1023/1023) - (23/1023) = 1 - 23/1023

so your formula becomes

weight = ADC - ADC * 23 / 1023
I am not following the reasoning behind it. I am sure it is simple but I cant figure it out.

Secondly how does adding 512 to the final formula round the numbers instead of just dropping off the decimals.

If you rather round the value (as opposed to just cutting off decimals), make that

weight = ADC * 23 + 512 / 1023
weight = ADC - weight
 

MPep

Senior Member
The initial equation is:
(ADC * 1000) / 1023 = Weight
.

The equation:
1000/1023 = (1023 - 23) / 1023 = (1023/1023) - (23/1023) = 1 - 23/1023

so your formula becomes

weight = ADC - ADC * 23 / 1023
, to help you understand should become
ADC*1000/1023 = (ADC*1023 - ADC*23) / 1023 = (ADC*1023/1023) - (ADC*23/1023) = ADC - ADC*23/1023
1000/1023 => the first step simply creates 1000 in an other way: 1023 - 23.
Because we have an initial 1000 / 1023, we now need to divide the (1023 - 23) by 1023. This then becomes 1023/1023 - 23/1023. 1023/1023 = 1. Therefore 1 - 23/1023 = 1000/1023.

Because the initial equation you gave was we now add the ADC back into it. But because we multiply the ADC by 1000, then divide by 1023, remembering our basic maths rules, it does not matter when we divide and when we multiply. (I am talking about mathematics, not PICAXE maths!!).
So a simplification was made by working out 1000/1023 first.
Therefore the end equation becomes ADC - ADC*23/1023.

May I suggest that you work this out with simpler numbers, such as 5/6.
Following on as per the above equation: (6-1)/6; => 6/6 - 1/6; => 1 - 1/6. Which is indeed = to 5/6.

Hope this helps. :)
 
Top