Search code examples
mathfixed-point

How can we calculate 1/3 by using fixed-point arithmetic?


I'm interested in fixed-point arithmetic especially division operation. However many sites explain the way to do it only using power-of-two division, namely a bit shift.

What if the denominator is not power-of-two? How can we compute 1/3 in for example unsigned 16-bit arithmetic? Assuming the 4-right bits are fractional part.

1 is 0000 0000 0001 0000

3 is 0000 0000 0011 0000


Solution

  • If the denominator is not a power of two then the result is most likely to be an infinite series. For example:

    enter image description here

    Since you only have 4 fractional bits, the result will be:

    enter image description here

    You can calculate this by the following:

    • Take a value with only the MSB set: 1000 0000 0000 0000
    • Divide by the denominator
    • Right shift by the number of integer bits minus 1. This is equivalent to converting the initial value to 1 in the fixed point representation

    For the example above:

    • [1000 0000 0000 0000] / 3 = 0010 1010 1010 1010
    • [0010 1010 1010 1010] >> (12 - 1) = 0000 0000 0000 0101