What does the constant 0.0039215689 represent?

前端 未结 2 1787
死守一世寂寞
死守一世寂寞 2020-12-07 07:48

I keep seeing this constant pop up in various graphics header files

0.0039215689

It seems to have something to do with color maybe?

2条回答
  •  旧巷少年郎
    2020-12-07 08:40

    0.0039215689 is approximately equal to 1/255.

    Seeing that this is OpenGL, performance is probably important. So it's probably safe to guess that this was done for performance reasons.

    Multiplying by the reciprocal is faster than repeatedly dividing by 255.


    Side Note:

    If you're wondering why such a micro-optimization isn't left to the compiler, it's because it is an unsafe floating-point optimization. In other words:

    x / 255  !=  x * (1. / 255)
    

    due to floating-point round-off errors.

    So while modern compilers may be smart enough to do this optimization, they are not allowed to do it unless you explicitly tell them to via a compiler flag.

    Related: Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?

提交回复
热议问题