1
$\begingroup$

My calculation: consider an adjacent pair of IEEE single precision floating point numbers (known as "floats" in C++ for example). Then the gap between those numbers is the corresponding machine epsilon $2^{-24}$. But the gap between doubles is $2^{-53}$. Assuming that each float is a double, the relative interval between two floats could be written as the sequence

$$2^{-24}+ 2^{-53}, 2^{-24}+ 2\cdot2^{-53}, 2^{-24}+ 3\cdot2^{-53}, \dots$$

Anyways... $2^{-24}/2^{-53} = 2^{29}$ so to exclude the endpoints there should be

$$2^{29} -1$$

doubles in each single gap. Seems too large, that's why I'm posting.


Edit: Didn't account for the gaps properly: $2^{29} -2 \to 2^{29} -1 $

$\endgroup$
  • $\begingroup$ If you are unsure you can convince yourself by explicitly counting the numbers: ` #include <iostream> #include <cmath> int main(){ float next_float = std::nextafter(1.f,2.f); double x = 1.0; int n = 0; while(1){ x = std::nextafter(x, 2.); if(x >= next_float) break; n++; } std::cout << n << std::endl; }` This will confirm your result $536870911 = 2^{29}-1$ $\endgroup$ – Winther Jun 4 '18 at 20:29
  • $\begingroup$ The absolute spacing/gap is $\epsilon$ at 1, it is larger/smaller for larger/smaller numbers. $\endgroup$ – gammatester Jun 4 '18 at 20:29
6
$\begingroup$

Note that "single" means $1$ sign bit, $8$ bit exponent, $23$ bit mantissa; and "double means $1$ sign bit, $11$ bit exponent, $52$ bit mantissa.

Given two consecutive normalized and non-zero single precision floats with same exponent (and automatically of same sign), we can simply copy the signs, exponent (with bias adjustments) and mantissa from the floats to the corresponding parts of doubles. This determines only 23 of the 52 mantissa bits (the rest being zero for the converted float values themselves), hence leaves room to play with 29 bits (based off the number with lower mantissa), hence in this case there are $2^{29}-1$ doubles strictly between the floats.

Upon closer inspection, the special case of different exponents, turns out to not be special at all: We still can play with the 29 lowest bits of the number with lower exponent.

The case when non-normalized near zero are involved is a bit more complicated as the wider range of allowed exponents produces a lot more doubles then.


Remark: As IEEE floats are defined in such a way that the lexical order (or the order imposed by interpreting the bit patterns as 32 or 64 bit integers) matches numerical order (at least for non-negatives), a simple way to count the number of floats or doubles between two bit patterns is to just interpret them as integers and subtract (and subtract one more). This may be helpful in understanding the above and in particular in counting the numbers when denormalized numbers are involved.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.