I'm trying to figure out how many numbers between 0-1 can a 32bit floating point represent? is it just the same as in an interval? (2**24)-1
Asked
Active
Viewed 471 times
0
-
Hint: all positive numbers with a negative exponent will be between 0 and 1, regardless of the mantissa. – Nate Eldredge Nov 19 '21 at 18:53
-
1What floating-point format? IEEE 754 binary32 is (overwhelmingly) the most common 32-bit binary floating-point format, but it's not the only one. – Mark Dickinson Nov 19 '21 at 19:43
-
1About 25% of all `float`. – chux - Reinstate Monica Nov 19 '21 at 19:43
-
" is it just the same as in an interval? (2**24)-1" --> No. – chux - Reinstate Monica Nov 19 '21 at 19:44
-
Niv Peretz, "I'm trying to figure out how many ...." --> Post your try. So far, post does not show any attempt. – chux - Reinstate Monica Nov 19 '21 at 19:45
-
I'm not sure why you mean by "in an interval", since the number of floats in an interval would depend on the endpoints of that interval. But in a single *binade* (e.g., `[1.0, 2.0)`, or `[2.0, 4.0)`, etc.), and assuming IEEE 754 binary32, the number of values would be `2^23` rather than `2^24`. – Mark Dickinson Nov 19 '21 at 19:56
-
[This answer](https://stackoverflow.com/a/48392678/298225) has C++ code to return the floating-point values in an interval, assuming IEEE-754 binary. – Eric Postpischil Nov 19 '21 at 21:26
1 Answers
3
That number happens to be 1,056,964,609. (Excluding -0). This only accounts for normal numbers. If you also include subnormals, then the number is 1,065,353,217. You can count them with a program like this:
#include <math.h>
#include <stdio.h>
int main(void) {
float x = 0.0f;
int normals = 0;
int all = 0;
while (x <= 1.0) {
all++;
if(isnormal(x)) normals++;
x = nextafterf(x, 1.1);
}
printf("normals: %d\n", normals);
printf("all : %d\n", all);
return 0;
}
When run, this prints:
normals: 1056964609
all : 1065353217
It's interesting to note that a quarter of all normal floats live in this range. This is usually quite a surprise and can have implications in uniform random-number generation.
See https://lemire.me/blog/2017/02/28/how-many-floating-point-numbers-are-in-the-interval-01/ for details.

alias
- 28,120
- 2
- 23
- 40
-
1That's excluding subnormals, right? (But incuding `0.0` and `1.0` (and excluding `-0.0`).) – Mark Dickinson Nov 19 '21 at 19:41
-
1@MarkDickinson Quite right; updated the answer to include denormals as well. – alias Nov 19 '21 at 19:46
-
-
1The preferred term for numbers below the normal range is “subnormal”: These are numbers that are below the normal range, so they can only have denormalized representations. “Denormal” refers to a floating-point representation that is not normal. In IEEE-754 binary formats, the only denormalized representations are those for subnormal numbers. In decimal formats, numbers in the normal range can have normal or denormal representations. – Eric Postpischil Nov 19 '21 at 21:14
-
@EricPostpischil Quite right. Fixed the naming and also added a simple program to count the values. – alias Nov 19 '21 at 21:20