Forum Discussion
Altera_Forum
Honored Contributor
9 years agoHow to square a fixed point number?
Assuming I have a 16 bit fixed point number that needs to be squared, how should this be implemented?
The number format is Q2.14. Squaring it shall produce a result that is 32 bits instead and seems to have format of Q4.28 (right?). Now since I am squaring this fixed point number, I shall have to shift it right to become Q4.14 and then feed it back in. But how can I feed in a result of format Q4.14 when the input is of format Q2.14. What is wrong in my understanding?13 Replies
- Altera_Forum
Honored Contributor
--- Quote Start --- just multiply 16 bits x 16 bits = 32 bits If you are asked to also scale down then truncate LSBs --- Quote End --- Truncating always floors the values (ie. rounds down). If you need to round to nearest, you add 1 to the highest LSB you are going to discard, then discard the LSBs. - Altera_Forum
Honored Contributor
Are the values signed or unsigned?
As you indicate that format is Q2.14 I guess they are signed and that the value is between -1.0 and 1.0 (both inclusive) In that case you can safely discard the two upper bits in the (scaled) Q4.14 and feed the remaining Q2.14 further. - Altera_Forum
Honored Contributor
--- Quote Start --- Assuming I have a 16 bit fixed point number that needs to be squared, how should this be implemented? The number format is Q2.14. Squaring it shall produce a result that is 32 bits instead and seems to have format of Q4.28 (right?). Now since I am squaring this fixed point number, I shall have to shift it right to become Q4.14 and then feed it back in. But how can I feed in a result of format Q4.14 when the input is of format Q2.14. What is wrong in my understanding? --- Quote End --- just multiply 16 bits x 16 bits = 32 bits If you are asked to also scale down then truncate LSBs