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.
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:
FLT_RADIX
is odd and FLT_MANT_DIG <= FLT_MAX_EXP
, the largest representable odd integer is FLT_MAX
if FLT_MANT_DIG
is odd and FLT_MAX - scalbnf(FLT_EPSILON, FLT_MAX_EXP+1)
otherwise.FLT_RADIX
is even and FLT_MANT_DIG > FLT_MAX_EXP
, then: If FLT_MAX_EXP > 0
, the largest representable odd integer is floorf(FLT_MAX)
. Otherwise, no odd integers are representable.FLT_RADIX
is odd and FLT_MANT_DIG > FLT_MAX_EXP
, then: If FLT_MAX_EXP > 0
, the largest representable odd integer is floorf(FLT_MAX)
if FLT_MANT_DIG - FLT_MAX_EXP
is odd or floorf(FLT_MAX)-1
otherwise. Otherwise, no odd integers are representable.