If I have a varying float in my shader program:
varying highp float someFloat;
and in the vertex shader, I set it to something.
someFloat = 1.0;
why in my fragment shader does this comparison seem to return false?
someFloat == 1.0 // false
but this returns true?
someFloat > .0 // true
testing on openGL ES in an iPad mini.
It happens on any IEEE 754 floating point number. It is because of the nature of floating point representation. Any language that use the IEEE 754 format will encounter the same problem.
Since 1.0
may not be represented exactly in floating point system as 1.000000000...
, hence it is considered dangerous to compare them using ==
.
Floating point numbers should always be compared with an epsilon value .
Since floating point calculations involve a bit of uncertainty we can try to allow for this by seeing if two numbers are ‘close’ to each other. If you decide – based on error analysis, testing, or a wild guess – that the result should always be within 0.00001 of the expected result then you can change your comparison to this:
if (fabs(someFloat - 1.0)) < 0.00001)
The maximum error value is typically called epsilon.
Probably you should read What Every Computer Scientist Should Know About Floating-Point Arithmetic
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With