From a similar question we have:
Base 2: Twos complement 4 integer, 4 bit fractional
-2^3 2^2 2^1 2^0 . 2^-1 2^-2 2^-3 2^-4
-8 4 2 1 . 0.5 0.25 0.125 0.0625
With only 4 fractional bits the represented number only has an accuracy of 0.0625
3.575 could be 11.1001 = 2+ 1+ 0.5 + 0.0625 => 3.5625 to low
or 11.1010 = 2+ 1+ 0.5 + 0.125 => 3.625 to high
This should indicate that 4 bits is just not enough to represent "3.575" exactly.
To figure out how many bit you would need multiply by a power of 2 until you get an integer: For "3.575" it is rather a lot (50 fractional bits).
3.575 * 2^2 = 14.3 (not integer)
3.575 * 2^20 = 3748659.2
3.575 * 2^30 = 3838627020.8
3.575 * 2^40 = 3930754069299.2 (not integer)
3.575 * 2^50 = 4025092166962381.0 (INTEGER) we need 50 bits!
3.575 => 11.10010011001100110011001100110011001100110011001101
Multiplying by a power of two shift the word to the left (<<
) When there is no fractional bits left it means the number is fully represented, then number of shifts is the number of fractional bits required.
For fixed point you are better off thinking about the level of precision your application requires.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…