9

Is something broken or I fail to understand what is happening?

static String getRealBinary(double val) {
    long tmp = Double.doubleToLongBits(val);
    StringBuilder sb = new StringBuilder();

    for (long n = 64; --n > 0; tmp >>= 1)
        if ((tmp & 1) == 0)
            sb.insert(0, ('0'));
        else
            sb.insert(0, ('1'));

    sb.insert(0, '[').insert(2, "] [").insert(16, "] [").append(']');
    return sb.toString();
}

public static void main(String[] argv) {
    for (int j = 3; --j >= 0;) {
        double d = j;
        for (int i = 3; --i >= 0;) {
            d += Double.MIN_VALUE;
            System.out.println(d +getRealBinary(d));
        }
    }
}

With output:

2.0[1] [00000000000] [000000000000000000000000000000000000000000000000000]
2.0[1] [00000000000] [000000000000000000000000000000000000000000000000000]
2.0[1] [00000000000] [000000000000000000000000000000000000000000000000000]
1.0[0] [11111111110] [000000000000000000000000000000000000000000000000000]
1.0[0] [11111111110] [000000000000000000000000000000000000000000000000000]
1.0[0] [11111111110] [000000000000000000000000000000000000000000000000000]
4.9E-324[0] [00000000000] [000000000000000000000000000000000000000000000000001]
1.0E-323[0] [00000000000] [000000000000000000000000000000000000000000000000010]
1.5E-323[0] [00000000000] [000000000000000000000000000000000000000000000000011]
Margus
  • 19,694
  • 14
  • 55
  • 103
  • 4
    What are you trying to do? What is your question? – Sjoerd Oct 10 '10 at 13:19
  • My question is: "How to alter double by its smallest increment", and this is my effort what failed. – Margus Oct 10 '10 at 13:21
  • 1
    why not just edit those bits if you want to do the smallest increment, it fails for 1 and 2 because MIN_VALUE is tiny (really small) so 0+really small = really small, but 2+ really small ~= 2 its because of FLOATING point the min_value is with the point as far left as possible whilst for two it is somewhere in the middle, left loses. you can see its approximately 300 digits after the dot that the difference should be, wheraes a double stores only about 15-20 significant digits. – flownt Oct 10 '10 at 13:23
  • Note to self: '--n > 0;' should have been '--n >= 0;'. – Margus Oct 10 '10 at 13:49

5 Answers5

9

The general idea is first convert the double to its long representation (using doubleToLongBits as you have done in getRealBinary), increment that long by 1, and finally convert the new long back to the double it represents via longBitsToDouble.

EDIT: Java (since 1.5) provides Math.ulp(double), which I'm guessing you can use to compute the next higher value directly thus: x + Math.ulp(x).

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • 5
    Since Java 1.6, there is [`Math.nextAfter(start, direction)`](http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#nextAfter(double,%20double)), which is far more reliable. It even handles special cases such as around zero. – z0r Feb 19 '15 at 01:08
7

Floating point numbers are not spread out uniformly over the number line like integer types are. They are more densely packed near 0 and very far apart as you approach infinity. Therefore there is no constant that you can add to a floating point number to get to the next floating point number.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • How can this be? We have a mantissa and an exponent. And neither favor 0. – Tony Ennis Oct 10 '10 at 14:00
  • @TonyEnnis: The exponent is just that -- an exponent. Oversimplified a bit, a floating-point number's value is something like `mantissa * 2^exponent`. That is, the amount by which changing the mantissa changes the resulting number depends entirely on the value of the exponent. The lower the exponent is, the smaller the change will be. – cHao Dec 27 '11 at 21:40
4

Your code is not well-formed. You try to add the minimum double value and expect the result to be different from the original value. The problem is that double.MinValue is so small that the result is rounded and doesn't get affected.

Suggested reading: http://en.wikipedia.org/wiki/Machine_epsilon

On the Wikipedia article there is the Java code too. Epsilon is by definition the smallest number such as (X + eps * X != X), and eps*X is called "relative-epsilon"

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305
4

Since Java 1.8 there is java.lang.Math.nextUp(double) doing exactly what you want. There is also opposite java.lang.Math.nextDown(double).

Maciej Mikosik
  • 355
  • 1
  • 5
  • 14
0

In case you want to use the BigDecimal class, there is the BigDecimal.ulp() method as well.

Matthias
  • 1,200
  • 2
  • 13
  • 33