With 32 bit floats I think there is something like 2^31 – 1 representable floats. In java you can take an existing float and find the “next float” using the Math library. But lets say you don’t have a starting float, is there a way to compute the nth float? I don’t care what language, if there is a language with a library function I’ll take it.
Of course I could simply put all the floats in an array and index into that, but that is space inefficient.
Here is some further clarification. I could start at Float.MIN and increment N times using nextFloat, but this seems inefficient because I need to perform this operation many times.
Depends on how you want them ordered. Keep in mind that not all floats are ordered at all; for instance, a pair of distinct NaNs are unordered (i.e, they are not equal, but neither one is greater than the other).
If you don’t mind ending up with those, you can just reinterpret an integer as a float. The way you’d do this varies from language to language; here’s a C implementation:
This has the convenient property of giving you mostly ordered results — passing in zero gets you 0.0, one gets you 1.4e-45, 2 gets you 2.8e-45, and so on. The results will start getting crazy once you get into NaN/Inf values, and will eventually start decreasing once you hit 0x80000000 (-0.0), but that should be good enough for now.