Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the next Double in Java or Groovy?

When having a Double d, I'd like to know which Double' is the smalles possible value bigger than d.

When d==0 I know the answer, that will be Double.MIN_VALUE :

> 0d + Double.MIN_VALUE
4.9E-324

But what about all ohter numbers like for instance 1d ?

> 1d + Double.MIN_VALUE
1.0

It has to do wit significant numbers I guess, but in short : I'm looking for a method that gives me the next double

nextDouble(0)==4.9E-324
like image 539
Peter Avatar asked Dec 01 '22 16:12

Peter


1 Answers

Take a look at these functions.

public static double nextAfter(double start, double direction)

Returns the floating-point number adjacent to the first argument in the direction of the second argument. If both arguments compare as equal the second argument is returned.

public static double nextUp(double d)

Returns the floating-point value adjacent to d in the direction of positive infinity. This method is semantically equivalent to nextAfter(d, Double.POSITIVE_INFINITY); however, a nextUp implementation may run faster than its equivalent nextAfter call.

like image 122
Anthony Smith Avatar answered Dec 09 '22 10:12

Anthony Smith