IEEE-754: cardinality of the set of rational numbers

后端 未结 1 1126
夕颜
夕颜 2021-01-24 22:06

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条回答
  • 2021-01-24 22:51

    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);
    }
    
    0 讨论(0)
提交回复
热议问题