In general, it is not a good idea to compare floats. But in the special case of opacity tests for fully-opaque, comparing with 1.0f
is probably fine. This is because:
1.0f
is an exact value in IEEE single precision binary floating point. (That's what you'll be using here.)
- The cases where you want to detect an exact equality of this kind will be ones where the value that you are comparing against was (almost certainly) specified exactly in the first place. In particular, solid colors have their opacity set to exactly
1.0f
.
If your use case is not detecting solid colors, but rather working out if things are “solid enough” then you need a more complex check, testing whether the value is “within ε” (epsilon). In particular, bearing in mind the special features of the alpha values (hard limit of 1.0, typically mapped to 256 discrete values in implementations so a good estimate for ε is easy to work out), you'll find that this simplified version is likely to do the right thing:
if (1.0f - sender.alpha < 1.0f/256) …