2
$\begingroup$

Could someone please provide a method for inverting random fixed point numbers. I have as an input both positive and negative floating point numbers that should be converted into fixed point and then be inverted.

An example could be 1/0.2453 where floating-point 0.2453 is converted into a Q18 fixed point format and then inverted to 4.07664. Thanks a lot!!

$\endgroup$

closed as off-topic by Paul R, hotpaw2, jojek, lennon310, Naresh May 3 '14 at 6:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "General programming questions are off-topic here, but can be asked on Stack Overflow." – Paul R, hotpaw2, jojek, lennon310, Naresh
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ why the negative vote for? $\endgroup$ – Giwrgos Rizeakos Apr 22 '14 at 14:56
  • 1
    $\begingroup$ given your example, you know that the result would be greater than 1, correct? in fact, it would be just a little bit greater than 4. not quite what we might think is "Q18". so how are you defining your fixed-point numbers? that's the first thing you have to settle. $\endgroup$ – robert bristow-johnson Apr 23 '14 at 16:05
  • 1
    $\begingroup$ doesn't seem like you're taking me up on my suggestion. how many bits to the right of the binary point exist in your "Q18" number? 18? 17? something else? what is the range of value of your "Q18" numbers? 0 to just under 1 (that is $1-2^{-18}$)? or from -1 to just under 1 (or $1-2^{-17}$)? $\endgroup$ – robert bristow-johnson Apr 24 '14 at 3:07
  • 1
    $\begingroup$ your answer is, as best as i can tell, self-contradictory. if your "Q18" number, $x$, is how i would dub it, a "Q0.18" number, which means 0 bits to the left of the binary point and 18 bits to the right, then the range of values is $0 \le x \le 1-2^{-18} < 1$. 5 is not in that range. you need to settle this before you start multiplying or dividing numbers. $\endgroup$ – robert bristow-johnson Apr 24 '14 at 15:54
  • 1
    $\begingroup$ @GiwrgosRizeakos Here's a practical example: blog.voltampmedia.com/2011/09/27/… $\endgroup$ – endolith Jun 26 '14 at 19:19
5
$\begingroup$

the first thing that one must understand when doing fixed-point arithmetic is that, at the basic level, it is integer arithmetic with some scaling factors applied. perhaps just one scaling factor applied. this scaling factor is directly related to the position of the binary point. unless the chip is a fixed-point DSP or similar, the binary point is implied, not explicit. it, and the associated scaling factor, exists only in the mind of the programmer. the CPU only thinks it is working on integers.

as an example an unsigned, fixed-point, 16-bit format, that i would call "1.15" or, perhaps "Q1.15" (i think the "Q" is superfluous and notation like "Q16" does not supply enough information) has 1 bit to the left of the binary point and 15 bits to the right. but the 16 bits, sitting all by themselves, is an integer. the range of that unsigned integer, $n$, is $0 \le n \le 2^{16}-1$. but the range of the unsigned fixed-point number implied $\hat{n}$ is $0 \le \hat{n} \le 2 - 2^{-15}$ = 1.99996948242188 .

the relationship between the two is: $$ \hat{n} = n \cdot 2^{-15} $$ or $$ n = \hat{n} \cdot 2^{15} $$ .

note that the "15"(or "-15") in the exponents is the same as the "15" in the "Q1.15" notation. the scaling factor is directly related to the number of implied bits to the right of the binary point.

if, instead it was a 16-bit signed, we might use "s.15" or "Qs.15" notation to refer to the format, the range of the signed integer $n$ is $-2^{15} \le n \le +2^{15} - 1$. and the range of the implied fixed-point value, $\hat{n}$, is $-1 \le \hat{n} \le 1 - 2^{-15}$ = 0.99996948242188 .

and the mapping between the two is the same as above: $$ \hat{n} = n \cdot 2^{-15} $$ or $$ n = \hat{n} \cdot 2^{15} $$ .

the only difference is the use of unsigned int vs. int in your C code. they both have the same number of bits to the right of the binary point and they both have the same mapping.

now when C does addition or subtraction or multiplication or division of these values, it is operating on the integer representation of those values. but the integer representation is the same as your implied value, except for the scaling factors.

so if you want to divide some numerator $\hat{n}$ by some denominator $\hat{d}$ to result in some quotient $\hat{q}$, you must first establish what the ranges of all three numbers are. then establish how many bits are to the right of the binary point for each number. then map those numbers $\hat{n}$ and $\hat{d}$ to their integer values $n$ and $d$, and determine what the range of the integer quotient is before it is adjusted to represent $\hat{q}$. there will be a scaling factor that will pop out at you (a power of 2) which will imply shifting of either $n$ or $d$ (most likely $n$) before the integer division is done.

$\endgroup$

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