schubakah - 8 months ago 53

Java Question

I have values between 0 and 1 stored as floats.

I know that delta between distinguishable floats is smaller the closer the floats are to 0. Thus, I think the precision is very high in my case (as the highest possible float is 1..). How big is my epsilon that I have to consider when comparing 2 of these floats? i.e. how big is the gap between 1.0f and the biggest representable float that is smaller than 1.0f? I am sorry if this question seems too broad/general but I could not find an answer :-(.

Thanks

Answer

You can use the function `java.lang.Math.nextAfter(float start, double direction)`

to help you.

Call it using, in your case, `nextAfter(1.0f, 0.0)`

. Here we're setting the `direction`

to be less than your starting number, so it will search "backwards". The resulting `float`

is your answer.

Because this function also has an overload taking a `double`

for `start`

, be careful to use `1.0f`

to denote a `float`

literal.

Source (Stackoverflow)