6

Consider the case where you want to test every possible input value. Creating a case where you can iterate over all the possible ints is fairly easy, as you can just increment the value by 1 and repeat.

How would you go about doing this same idea for all the possible double values?

Dalmas
  • 26,409
  • 9
  • 67
  • 80
user321605
  • 846
  • 2
  • 7
  • 20
  • 1
    Well since you already can iterate every possible int, just iterate a set that consists of every possible int / every possible int. – asawyer Dec 27 '11 at 21:28
  • 2
    Of course you realize that there are too many distinct doubles ( or longs in the case of integers) to actually try them all. – President James K. Polk Dec 27 '11 at 21:28
  • 3
    Where exactly do you need to implement such a concept? – Lion Dec 27 '11 at 21:32
  • 1
    ...that's one way to get into a near-infinite loop... – Chris Cashwell Dec 27 '11 at 21:34
  • I don't really need it for doubles, a float is sufficient. I was talking with someone about testing, and decreed that for a lot of cases, full exhaustion is possible (obviously doubles are not), and that you can completely guarantee something works with the idea in a decent amount of cases. – user321605 Dec 27 '11 at 22:39

3 Answers3

11

You can iterate over all possible long values and then use Double.longBitsToDouble() to get a double for each possible 64-bit combination.

Note however that this will take a while. If you require 100 nanoseconds of processing for each double value it will take roughly (not all bit combinations are different double numbers, e.g. NaN) 2^64*1e-7/86400/365 years which is more than 16e11/86400/365 = 50700 years on a single CPU. Unless you have a datacenter to do the computation, it is a better idea to go over possible range of all input values sampling the interval at a configurable number of points.

Analogous feat for float is still difficult but doable: assuming you need 10 milliseconds of processing for each input value you need roughly 2^32*1e-2/86400 = 497.1 days on a single CPU. You would use Float.intBitsToFloat() in this case.

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92
  • +1 but note that `Double.longBitsToDouble()` will also create Infinity and (a lot of) NaN - probably desired to test – user85421 Dec 27 '11 at 21:42
0

Java's Double class lets you construct and take apart Double values into its constituent pieces. This, and an understanding of double representation, will allow you at least conceptually to enumerate all possible doubles. You will likely find that there are too many though.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
0

do 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 long to run.
(this will neither create NaN nor Infinity)

user85421
  • 28,957
  • 10
  • 64
  • 87
  • `Double.MIN_VALUE` is actually the smallest double **greater than 0**, so this will only give about half of the possible values. – Michael McGowan Oct 17 '12 at 19:00