views:

258

answers:

4

I am doing high precision scientific computations. In looking for the best representation of various effects, I keep coming up with reasons to want to get the next higher (or lower) double precision number available. Essentially, what I want to do is add one to the least significant bit in the internal representation of a double.

The difficulty is that the IEEE format is not totally uniform. If one were to use low-level code and actually add one to the least significant bit, the resulting format might not be the next available double. It might, for instance, be a special case number such as PositiveInfinity or NaN. There are also the sub-normal values, which I don't claim to understand, but which seem to have specific bit patterns different from the "normal" pattern.

An "epsilon" value is available, but I have never understood its definition. Since double values are not evenly spaced, no single value can be added to a double to result in the next higher value.

I really don't understand why IEEE hasn't specified a function to get the next higher or lower value. I can't be the only one who needs it.

Is there a way to get the next value (without some sort of a loop which tries to add smaller and smaller values).

+1  A: 

I'm not sure I'm following your problem. Surely the IEEE standard is totally uniform? For example, look at this excerpt from the wikipedia article for double precision numbers.

3ff0 0000 0000 0000   = 1
3ff0 0000 0000 0001   = 1.0000000000000002, the next higher number > 1
3ff0 0000 0000 0002   = 1.0000000000000004

What's wrong with just incrementing the least significant bit, in a binary or hex representation?

As far as the special numbers go (infinity, NaN,etc.), they're well defined, and there aren't very many of them. The limits are similarly defined.

Since you've obviously looked into this, I expect I've got the wrong end of the stick. If this isn't sufficient for your problem, could you try and clarify what you're wanting to achieve? What is your aim here?

ire_and_curses
Would that work in cases where the exponent would have to increase?
T.E.D.
My aim is to do this cleanly, preferably from C#, but I'll stoop to bit level if I have to. The problem is that the IEEE standard is not in the public domain, and I can't afford to purchase it. The standard defines the bit patterns for the case you show, but also for all the unusual numbers (such as the sub-normals). One shouldn't have to know the full details of all the number formats to do this task. But if you flip bits yourself, you would have to. What if the 'next' number is a subnormal? Unless you know all the rules, you CAN'T get there!
Mark T
@Mark T:Ok, I understand your problem now. I hadn't realised the standard wasn't freely available (amazing)! Here are implementations of a number of functions, including dnxtaft.f, which returns the next floating point value in the direction of x. Perhaps this will help? http://www.math.utah.edu/~beebe/software/ieee/
ire_and_curses
@Mark T: I also note there is the fp_class() function, which tells you what type of floating point number you are dealing with: http://www.intel.com/software/products/compilers/docs/flin/main_for/lref_for/source_files/rffpclas.htm, example usage here: http://www.johndcook.com/IEEE_exceptions_in_cpp.html
ire_and_curses
+8  A: 

There are functions available for doing exactly that, but they can depend on what language you use. Two examples:

  • if you have access to a decent C99 math library, you can use nextafter (and its float and long double variants, nextafterf and nextafterl); or the nexttoward family (which take a long double as second argument).

  • if you write Fortran, you have the nearest intrinsic available

If you can't access these directly from your language, you can also look at how they're implemented in freely available, such as this one.

FX
+2  A: 

Yes, there is a way. In C#:

       public static double getInc (double d)
        {
                // Check for special values
                if (double.IsPositiveInfinity(d) || double.IsNegativeInfinity(d))
                    return d;
                if (double.IsNaN(d))
                    return d;

                // Translate the double into binary representation
                ulong bits = (ulong)BitConverter.DoubleToInt64Bits(d);
                // Mask out the mantissa bits
                bits &= 0xfff0000000000000L;
                // Reduce exponent by 52 bits, so subtract 52 from the mantissa.
                // First check if number is great enough.
                ulong testWithoutSign = bits & 0x7ff0000000000000L;
                if (testWithoutSign > 0x0350000000000000L)
                  bits -= 0x0350000000000000L;
                else
                  bits = 0x0000000000000001L;
                return BitConverter.Int64BitsToDouble((long)bits);
}

The increase can be added and subtracted.

Thorsten S.
This doesn't compile, and I don't think that you are properly using the `BitConverter.DoubleToInt64Bits` method properly anyway. If you want to get the byte representation of a number, you should use `BitConverter.GetBytes` (but then you need to make sure you increment or decrement the exponent, if needed).
Henry Jackson
It does not compile because C# does not allow to mix ulong/long constants and variables (which is stupid for bit operators).And you thought wrong, the BitConverter method does indeed return the internal byte structure of the double in the IEEE format.
Thorsten S.
+3  A: 

As Thorsten S. says, this can be done with the BitConverter class, but his method assumes that the DoubleToInt64Bits method returns the internal byte structure of the double, which it does not. The integer returned by that method actually returns the number of representable doubles between 0 and yours. I.e. the smallest positive double is represented by 1, the next largest double is 2, etc. etc. Negative numbers start at long.MinValue and go away from 0d.

So you can do something like this:

public static double NextDouble(double value) {

    // Get the long representation of value:
    var longRep = BitConverter.DoubleToInt64Bits(value);

    long nextLong;
    if (longRep >= 0) // number is positive, so increment to go "up"
        nextLong = longRep + 1L;
    else if (longRep == long.MinValue) // number is -0
        nextLong = 1L;
    else  // number is negative, so decrement to go "up"
        nextLong = longRep - 1L;

    return BitConverter.Int64BitsToDouble(nextLong);
}

This doesn't deal with Infinity and NaN, but you can check for those and deal with them however you like, if you're worried about it.

Henry Jackson
I see that you are using my code because the argument is value, butBitConverter.DoubleToInt64Bits gets "d" as argument.I had reservations about simply adding one because the IEEE formatseparates exponent and significand, but because it has a hidden bityour function is in fact ok as far as I can see.
Thorsten S.