314

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

0.0039215689

It seems to have something to do with color maybe?

Here is the first hit on Google:

void RDP_G_SETFOGCOLOR(void)
{
    Gfx.FogColor.R = _SHIFTR(w1, 24, 8) * 0.0039215689f;
    Gfx.FogColor.G = _SHIFTR(w1, 16, 8) * 0.0039215689f;
    Gfx.FogColor.B = _SHIFTR(w1, 8, 8) * 0.0039215689f;
    Gfx.FogColor.A = _SHIFTR(w1, 0, 8) * 0.0039215689f;
}

void RDP_G_SETBLENDCOLOR(void)
{
    Gfx.BlendColor.R = _SHIFTR(w1, 24, 8) * 0.0039215689f;
    Gfx.BlendColor.G = _SHIFTR(w1, 16, 8) * 0.0039215689f;
    Gfx.BlendColor.B = _SHIFTR(w1, 8, 8) * 0.0039215689f;
    Gfx.BlendColor.A = _SHIFTR(w1, 0, 8) * 0.0039215689f;

    if(OpenGL.Ext_FragmentProgram && (System.Options & BRDP_COMBINER)) {
        glProgramEnvParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 2, Gfx.BlendColor.R, Gfx.BlendColor.G, Gfx.BlendColor.B, Gfx.BlendColor.A);
    }
}

//...more like this

What does this number represent? Why does no one seem to declare it as a const?

I couldn't find anything on Google that explained it.

17
  • 16
    Is there any reason the source code would write this instead of (1.f/255) ?
    – M.M
    Mar 25, 2014 at 0:37
  • 57
    Mmmm...if only there were some way to avoid magic numbers.... Mar 25, 2014 at 1:29
  • 12
    1/255 == 0.00(3921568627450980) -- parens mean repetition.
    – jfs
    Mar 25, 2014 at 5:06
  • 85
    With your next magic number, try asking Wolfram Alpha
    – AakashM
    Mar 25, 2014 at 9:31
  • 15
    whatever the reason, using a magic number without documenting its purpose is very uncool Mar 27, 2014 at 19:13

2 Answers 2

387

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)?

20
  • 10
    I actually didn't know what it was when I first saw it. But seeing the way it was used, I suspected it was the multiply-by-reciprocal optimization. So I checked in my calculator and sure enough - I guessed right.
    – Mysticial
    Mar 24, 2014 at 22:08
  • 57
    I would've expected to see it written as a = b * (1.0f / 255); compilers still do constant folding, don't they? Mar 24, 2014 at 22:51
  • 9
    @IlmariKaronen Yes, they still do constant folding. It's actually required for some stuff like template resolutions and such. But I would've just pulled it out as a constant or a macro. But hey, not all code is perfectly written. :)
    – Mysticial
    Mar 24, 2014 at 22:54
  • 7
    And of course, to answer my own question, it's because the other two numbers can not be represented as standard C floats. The next float below 0.0039215689 is 0.0039215684. Mar 26, 2014 at 0:53
  • 6
    Of course, a better programmer writing the header file would have defined a constant and used it by name. Mar 26, 2014 at 13:38
79

This multiplication by 0.0039215689f converts an integer valued color intensity in the range 0 to 255 to a real valued color intensity in the range 0 to 1.

As Ilmari Karonen points out, even if this is an optimisation it's a rather badly expressed one. It would be so much clearer to multiply by (1.0f/255).

2
  • 5
    Or maybe better, defined as constant?
    – Johny
    Mar 25, 2014 at 21:54
  • 11
    @Johny Certainly defined as a constant. The point being not a magic value. Mar 25, 2014 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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