7

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.

1 Answer 1

15

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

1
  • Thanks! I am now glad that I asked. Commented Jul 6, 2013 at 20:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.