fixed_point_math
fixed_point_math copied to clipboard
Fixed-point with sign
Hi @ElectricRCAircraftGuy,
thanks for sharing you experience with fixed-point math. I'm trying to understand and using it for my future project, but I want to ask you a question. Reading you code I've seen that you have used uint32_t to represent a fixed-point number. If I want to work even with negative fixed-point number with Q16.15 format, how can I modify your code?
Thanks a lot in advance!
Giulio
I don't know. :) I'd have to put a ton of study into this again. You'd probably have to reserve one of the 32 bits to indicate the negative sign, however. I think that is generally the most-significant bit in normal signed integer math.
That might require reading the most-significant bits of the two numbers to be worked on, bitwise XORing them to figure out the resultant sign, shifting the remaining bits left to cover up the sign bit, doing the operation, then shifting right to leave space for the sign bit, and writing the sign bit back in. Again, I'm not sure. I'd have to put in a ton of work to ensure I get it right.
Those are just some quick thoughts and ideas.
Note to self: Q format article: https://en.wikipedia.org/wiki/Q_(number_format)