Iterate over all possible double values

Consider the case when you want to check all possible input values. Creating a case where you can iterate over all possible ints is quite simple, since you can simply increase the value by 1 and repeat.

How would you make the same idea for all possible double values?

+5
source share
3 answers

You can iterate over all possible values longand then use Double.longBitsToDouble()to get doublefor every possible 64-bit combination.

, . 100 double, ( - , NaN) 2 ^ 64 * 1-7/86400/365 , 16e11/86400/365 = 50700 . , , .

float - , : 10 , 2 ^ 32 * 1e-2/86400 = 497,1 CPU. Float.intBitsToFloat().

+11

Java Double . . , , .

0

run a loop like:

for (double v = Double.MIN_VALUE; v <= Double.MAX_VALUE; v = Math.nextUp(v)) {
    // ...
}

but, as already explained in Adam’s answer, it will take a long time. (this will not create NaN and Infinity)

0
source

All Articles