D Deng
D Deng

Reputation: 11

OpenGL ES 2.0 Shader Float Data Precision

Here is my whole fragment shader code, it's quite simple:

precision highp float;

void main( void )
{  
   float a = 66061311.0;
   if(a == 66061312.0)
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
   else
    gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}

Why the screen is clear to red. When I set a to 66061315.0, the screen is clear to green. That confuses me. Under my understanding, 66061311.0 is within the range of float type.

How can I fix or go around this?

Upvotes: 0

Views: 880

Answers (1)

Matic Oblak
Matic Oblak

Reputation: 16774

Even if the value is within the range of the type it does not mean that its precision is precise enough at such large values to see a difference between the two.

In your case for a standard 32-bit float the results are:

66061311.0 = 6.60613e+07
66061312.0 = 6.60613e+07

And the values are the same when comparing. This is not related or bound to openGL nor shaders, it is how a float is defined. A 64-bit float will detect a difference though.

To add a bit more info if you check the definition of a floating value you will see that the fraction only has 23 bits which means the precition is up to 8.4M but you have over 66M.

Upvotes: 1

Related Questions