cfloating-pointbitieee-754data-representation

Largest odd integer that can be represented as a float


I was wondering what largest odd integer that can be represented exactly as a float? And why there is a difference between the largest even integer represented as a float in this case.

I believe it would have to do with the base 2 exponents 2^n-1, however I am not familiar enough with data representation in C to see the distinction.


Solution

  • For IEEE-754 basic 32-bit binary floating-point, the largest representable odd integer is 224−1.

    For IEEE-754 basic 64-bit binary floating-point, the largest representable odd integer is 253−1.

    This is due to the fact that the formats have 24-bit and 53-bit significands. (The significand is the fraction part of a floating-point number.)

    The values represented by the bits in the significand are scaled according to the exponent of the floating-point number. In order to represent an odd number, the floating-point number must have a bit in the significand that represents 20. With a 24-bit significand, if the lowest bit represents 20, then the highest bit represents 223. The largest value is obtained when all the bits are on, which makes the value 20 + 21 + 22 + … 223, which equals 224−1.

    More generally, the largest representable odd integer is normally scalbnf(1, FLT_MANT_DIG) - 1. This can also be computed as (2 - FLT_EPSILON) / FLT_EPSILON. (This assumes a normal case in which FLT_RADIX is even and FLT_MANT_DIG <= FLT_MAX_EXP. Note that if FLT_MANT_DIG == FLT_MAX_EXP, the latter expression, with FLT_EPSILON, should be used, because the former overflows.)

    The abnormal cases, just for completeness: