1
votes

What is the cardinality of the set of rational numbers, which have an exact representation in floating point format compatible with single-precision IEEE-754?

1
2^32 minus the sizes of the ranges assigned to NaNs and infinities.Sneftel

1 Answers

5
votes

There are 2139095039 finite positive floats. There are as many finite negative floats.

Do you want to include +0.0 and -0.0 as two items or as one? Depending on the answer the total is 2 * 2139095039 + 2 or 2 * 2139095039 + 1, that is, respectively, 4278190080 or 4278190079.

Source for the 2139095039 number:

#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(void) {
  float f = FLT_MAX;
  unsigned int i;
  memcpy(&i, &f, 4);
  printf("%u\n", i);
}