I guess another way of phrasing this question is what decimal place can you go to using a float
that will only be between 0 and 1?
I've tried to work it out by looking at the MSDN. Which says the precision is 7 digits. I thought that meant it could only track changes of 0.0000001
.
However if I do:
float test = 0.00000000000000000000000000000000000000000001f;
Console.WriteLine(test);
It writes out 9.949219E-44
If I add any more zeroes, it will output 0
.
I'm pretty sure I'm missing something here as that degree of accuracy seems massively wrong. Mainly as a float is 32bits in size, and just from 0-1 at that level of accuracy contains 1e+44 possible numbers...
This is not really the question you want an answer for, but the answer is, not including
0
and1
themselves, that there are2**23 - 1
subnormal numbers and126 * 2**23
normal numbers in this range, for a total of127 * 2**23 - 1
, or1,065,353,215
.But note that these numbers are not evenly distributed on the interval between
0
and1
. Using a "delta" of1f / 1065353215f
in a loop from0f
to1f
will not work for you.If you want to step from 0.0 to 1.0 with eqally long steps of the (decimal) form 0.00...01, maybe you should use
decimal
instead offloat
. It will represent numbers like that exactly.If you stick to
float
, try with0.000001
(ten times greater than your proposed value), but note that errors can build up when performing very many additions with a non-representable number.Also note: There are a few "domains" where you can't even count on the first seven significant decimal digits of a
float
. Try for example saving the value0.000986f
or0.000987f
to afloat
variable (be sure the optimization doesn't hold the value in a "wider" storage location) and write out that variable. The first seven digits are not identical to0.0009860000
resp.0.0009870000
. Again you can usedecimal
if you want to work with numbers whose decimal expansions are "short".Edit: If you can use a "binary" step for your loop, try with:
or equivalently as a literal:
The good thing about this delta is that all values you encouter through the loop are exactly representable in your
float
. Just before (under)1f
, you will be taking the shortest possible steps possible for that magnitude.It's 7 significant digits, that is, when writing it in exponential notation you ignore the exponent.
0.0000000000000000001234567 has the same number of significant digits as 12345670000000000000, just with a different exponent. That's the magic that allows floating-point numbers to store really small and really large numbers.
As for exactly how many possible numbers there are for a
float
in (0, 1) I cannot say exactly right now. You have a 23-bit mantissa, so 223 possible states of it. Then there is an 8-bit exponent and if I'm not terribly mistaken about half of its possible values will result in a number between 0 and 1. Which should leave you with about 223 + 7 = 230 possible values in that range. If anything that's perhaps an upper bound and not the exact value. I would need to consult documentation about the finer details to know exactly (and probably rethink the math above which might miss a few points).Positive floating-point values are ordered the same as their encodings.
0.0f
is0x00000000
.1.0f
is0x3f800000
. So there are0x3f800000 - 1
floating point values that lie strictly in between, or 1,065,353,215.If you want to include the endpoints in your count, keep in mind that there are two encodings of zero.
Keep in mind, too, that floating-point values are not uniformly spaced. The difference between
1.0f
and the next smaller number is2**-24
, whereas the difference between0.0f
and the next larger number is2**-149
. If you want to increment a float from 0 to 1 with uniform steps, the smallest step size you can use is2**-24
.I wrote this very silly program, and it gives the answer
1065353217
, which is indeed just shy of 230 (1073741824). Subtract 2 from that number if you were looking for all the numbers not including 0 and 1. By the way, the smallest non-zero number appears to be 1.401298E-45.