Floating point precision

I've been reading up a bit on the standard representation of floats, and I kind of understood that numbers with a zero fractional part, e.g. 5.0, 9.0, can be exactly represented. Is this correct?
5 = 5*2^(0), 2.5 = 5*2^(-1) , 1.25 = 5*2^(-2), ...


In general, multiples of powers of two, given that the sign, significand and exponent can each be represented within the number of bits allotted to them, can be represented exactly.
Last edited on
numbers with a zero fractional part, e.g. 5.0, 9.0, can be exactly represented. Is this correct?


5.0 and 9.0 are exactly represented, but it's not true of all integers:

float f = 123456789; // f is 123456792
That's because 123456789 is too large to fit in the number of bits used as the significand for a 32 bit floating point number. It's also true of 16777217. Because the significand cannot store the number alone using an exponent of 0, you are forced to approximate it as a number with a smaller significand multiplied by some power of 2.
Last edited on
So any x/1, x/2, x/4, etc. can be represented exactly as long as x fits in the bits of the mantissa?
Assuming x is an integer, and the exponent is between -126 and 127, then yes. + or - x*2^exponent is the exact representation.
Alright, thanks for the help.
Topic archived. No new replies allowed.