I'm developing a math function and would like to test its output at every float value within a range. I have already done this in C++ but now I want to compare the performance to Java. How do I iterate through all float values in Java?
In C++, I'm simply iterating through the necessary range with an unsigned int, then reinterpreting it as a float pointer
float *x = reinterpret_cast<float*>(&i);
However, how can this be done in Java? Preferably quickly, as I am testing the performance (no String solutions thank you :D ). If there's no fast way, I guess I could just pre-calculate a million of them into an array and iterate through them. But that would mess up the cache performance, so I think using random numbers would then be better for my case, although it won't quite hit all values.
You can use Math.nextUp(float)
to get the next float number.
Example to print the next 100 floats starting from 1:
float n = 1f;
System.out.println(n);
for (int i = 0; i < 100; i++) {
n = Math.nextUp(n);
System.out.println(n);
}