Suppose I have two floating point numbers, x
and y
, with their values being very close.
There\'s a discrete number of floating point number
You do not have to examine the binary representation directly, but you do have to rely on it to get an exact answer, I think.
Start by using frexp() to break x into exponent exp
and mantissa. I believe the next float bigger than x is x + eps * 2^(exp-1)
. (The "-1" is because frexp returns a mantissa in the range [1/2, 1) and not [1, 2).)
If x and y have the same exponent, you are basically done. Otherwise you need to count how many steps there are per power of 2, which is just 1.0/eps
. In other words, the number of steps between 2^n and 2^(n+1) is 1.0/eps
.
So, for y > x, count how many steps there are from x to the next power of two; then count how many more steps it takes to get to the largest power of 2 less than y; then count how many more steps it takes to get from there up to y. All of these are pretty easily expressible in terms of eps
, I think.