1
$\begingroup$

I came across to the code that convert 32-bit signed fixed-point number (16.16) to a float and it looks like (pseudocode)

floating = fixed / 65536.0

Could you please explain me what's the essence of dividing by this? Why does this dividing works when fixed-point and floating-point numbers have different internal structures?

$\endgroup$
1
$\begingroup$

Your code converts a fixed-point number into its value. It also works for converting a fixed-point number to a rational number, for example.

A fixed-point number of the form $16.16$ consists of 32 binary digits, the first 16 to the left of the decimal dot, the second 16 to its right. When you insert the decimal dot, you are dividing by $2^{16} = 65536$.

Here is a decimal example. Consider a number stored in decimal fixed-point $2.2$. What is the value of the number stored as $1234$? It is $12.34 = 1234/100$.

$\endgroup$
  • $\begingroup$ Excuse me, what do you mean by inserting the decimal dot? I'm confused a little, I thought the decimal/binary dot is kind of imaginary thing. $\endgroup$ – ntrsBIG Oct 15 '17 at 13:45
  • $\begingroup$ It's not imaginary at all. It's implicitly there. Check out my decimal example. $\endgroup$ – Yuval Filmus Oct 15 '17 at 13:47
  • $\begingroup$ Well, we have 10^2 instead 2^16 in your decimal example. It seems like this value (10^2 or 2^16) looks like a scaling factor, am I right? $\endgroup$ – ntrsBIG Oct 15 '17 at 13:51
  • $\begingroup$ Right - fixed point stores fractional values by scaling and rounding them. $\endgroup$ – Yuval Filmus Oct 15 '17 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.