I would like to do hypot2 computation on a 16 bit processor.
The standard formula is c = sqrt((a * a) + (b * b)) . The problem with this is with large inputs. For example. 200 and 250, multiply 200 * 200 to get 90,000, which is higher than the maximum signed value of 32,767, so it overflows like b, numbers are added, and the result may also be useless; it may even signal an error due to negative sqrt.
In my case, I am dealing with 32-bit numbers, but 32-bit multiplication on my processor is very fast, about 4 cycles. I am using the dsPIC microcontroller. I would prefer not to multiply with 64-bit numbers, because spending precious memory will certainly be slower. In addition, I only have sqrt for 32-bit numbers, so for 64-bit numbers, another function will be required. So, how can I calculate the hypothesis when the values โโcan be large?
Please note that I can only use whole math for this. Using some type of floating point math leads to a quick hit, which I would rather avoid. My processor has a fast atan2 integer / fixed point, about 130 cycles; can i use this to calculate the length of the hypotenuse?
math
Thomas o
source share