Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to alter a float by its smallest increment in Java?

Tags:

java

I have a double value d and would like a way to nudge it very slightly larger (or smaller) to get a new value that will be as close as possible to the original but still strictly greater than (or less than) the original.

It doesn't have to be close down to the last bit—it's more important that whatever change I make is guaranteed to produce a different value and not round back to the original.

(This question has been asked and answered for C, C++)

The reason I need this, is that I'm mapping from Double to (something), and I may have multiple items with the save double 'value', but they all need to go individually into the map.

My current code (which does the job) looks like this:


private void putUniqueScoreIntoMap(TreeMap map, Double score,
            A entry) {

        int exponent = 15;
        while (map.containsKey(score)) {
            Double newScore = score;
            while (newScore.equals(score) && exponent != 0) {
                newScore = score + (1.0d / (10 * exponent));
                exponent--;
            }
            if (exponent == 0) {
                throw new IllegalArgumentException("Failed to find unique new double value");
            }
            score = newScore;
        }
        map.put(score, entry);
    }

like image 881
barryred Avatar asked Sep 07 '10 11:09

barryred


People also ask

Can we increment float?

For float ++ does not increment by the smallest possble value, but by 1.0. 1.0f has no special meaning (unlike integer 1). It may confuse the reader causing him to think that the variable is int.

What is float Max_value in Java?

MAX_VALUE public final static float MAX_VALUE. The maximum value a float can have. The largest maximum value possible is 3.40282346638528860e+38. MIN_VALUE public final static float MIN_VALUE. The minimum value a float can have.


2 Answers

In Java 1.6 and later, the Math.nextAfter(double, double) method is the cleanest way to get the next double value after a given double value.

The second parameter is the direction that you want. Alternatively you can use Math.nextUp(double) (Java 1.6 and later) to get the next larger number and since Java 1.8 you can also use Math.nextDown(double) to get the next smaller number. These two methods are equivalent to using nextAfter with Positive or Negative infinity as the direction double.

Specifically, Math.nextAfter(score, Double.MAX_VALUE) will give you the answer in this case.

like image 114
Stephen C Avatar answered Sep 18 '22 13:09

Stephen C


Use Double.doubleToRawLongBits and Double.longBitsToDouble:

double d = // your existing value;
long bits = Double.doubleToLongBits(d);
bits++;
d = Double.longBitsToDouble(bits);

The way IEEE-754 works, that will give you exactly the next viable double, i.e. the smallest amount greater than the existing value.

(Eventually it'll hit NaN and probably stay there, but it should work for sensible values.)

like image 20
Jon Skeet Avatar answered Sep 19 '22 13:09

Jon Skeet