5 ms·
The correct answer is 1,065,353,217. You have an off-by-one error in the following line of argument: > The number of values between the two is 0x3f800000 - 0x
by vog 10y ago
The correct answer is 1,065,353,217. You have an off-by-one error in the following line of argument:
> The number of values between the two is 0x3f800000 - 0x00000000 == 0x3f800000
This should be corrected to:
> The number of values between the two is 0x3f800000 - 0x00000000 == 0x3f800001
In general, the number of numbers in the closed integral interval [a, b] is not b-a, but 1+b-a. For example, the closed interval [0,10] contains 11 integers, not 10.
(The above assumes that the original question assumes the floating point number -0 to be outside of [0,1]. Otherwise, you'll have to add 1 more.)
- lightedman 10y agoI was under the impression (after several thousand attempts) that the correct answer is infinity, limited only by your addressable memory and bitspace.
- afandian 10y agoI think people are assuming IEEE 754 floating point (as per the article). Your memory is finite, 32 bits. https://en.wikipedia.org/wiki/Floating_point https://en.wikipedia.org/wiki/Floating_point
- AimHere 10y agoI'll not endorse your actual line of argument, but without knowing much about the internals of a 32-bit float, I can confirm that your final answer is correct. Source: brute force counting in a small C program. Naturally, this might not scale to 64-bits, unless you have lots of time and computing power to spare.
- Lanzaa 10y agoThis type of off-by-one error is so common it has its own name: fencepost error[1]. One little note, is that I would disagree with your correction. > This should be corrected to: > > The number of values between the two is 0x3f800000 - 0x00000000 == 0x3f800001 I think the value originally computed was correct. It would make more sense to correct the wording rather than just the computed value. i.e. > The number of values in the range [0x00000000, 0x3f800000] is 0x3f800000 - 0x00000000 + 1 == 0x3f800001 [1]: https://en.wikipedia.org/wiki/Off-by-one_error#Fencepost_error https://en.wikipedia.org/wiki/Off-by-one_error#Fencepost_err...