Intermediate math question

The problem is with w1 = 339 and delta = 122 the intermediate total is .28 and in integer math 180 * .28 = 0

I don't believe this is true... The equation:

180 * (w1 - delta) / 1024

will result in the multiplication being carried out before the division, so with w1 = 339 and delta = 122, you will get

180 * (w1 - delta) / 1024
= 180 * 217 / 1024
= 39060 / 1024
= 38

The reason why this wasn't working for you is that you were using signed integers, which means that 39060 evalutes to a -6293, which probably throws off your results. I assume you weren't expecting 180 * (w1 - delta) / 1024 to evaluate to -6.

Using your original formulation with unsigned integers and without scale factors should work for you, assuming you're happy with the resolution provided by integer math. If it's possible for 180 * (w1 - delta) to be greater than 65535, you will need to convert one of the quantities to a long:

180UL * (w1 - delta)

  • Ben