Normally, when we want to test fractional numbers for equality, we do so with some amount of uncertainty, because of approximate nature of IEEE754.
if (fabs(one_float - other_float) < SUFFICIENTLY_SMALL) {
consider them equal;
}
Other approach might be to cast the floats to integers of specific magnitude, and compare resulting integers instead.
if ((uint)one_float == (uint)other_float) {
consider them equal;
}
But consider the situation where our floats never undergo any arithmetics, and the only thing we do with them is assignment.
// Might be called at any moment
void resize(float new_width, float new_height)
{
if (current_width == new_width && current_height == new_height) {
return;
}
accomodate framebuffers;
update uniforms;
do stuff;
current_width = new_width;
current_height = new_height;
}
In the example above, we have an event handler that may fire up spontaneously, and we want to reallocate resources only if real resize occurred. We might take the usual path with approximate comparison, but that seems like a waste, because that handler will fire up pretty often. As far as I know, floats are assigned just as everything else, using memory move; and equality operator performs just the memory comparison. So it looks like we are in a safe harbour. When checked on x86 and ARM, this assumption holds, but I just wanted to be sure. Maybe it's backed by some specs?
So, is there something that might change floats when only assigning them? Is the described approach viable?