Is it safe to compare floats strictly, given that we do not conduct any operations with them?

Usually, when we want to check fractional numbers for equality, we do this with some uncertainty due to the approximate nature of IEEE754.

if (fabs(one_float - other_float) < SUFFICIENTLY_SMALL) {
    consider them equal;
}

Another approach may be to distinguish floats with integers of a certain size and instead compare the resulting integers.

if ((uint)one_float == (uint)other_float) {
    consider them equal;
}

But consider the situation when our floats are never subjected to any arithmetic, and the only thing we do with them is the purpose.

//  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;
}

, , . , , . , , , ; . , . x86 ARM , . , ?

, -, float ? ?

+4
3

"".

, -, , float float (current_width = new_width) , , . , .

new_width new_height , , . , , , . .

C 2011 , , , , . . " " (6.5.16.1) :

(=) , , .

, , .

, : , . , , ?

+2

, , == - :

current_width = new_width;
current_height = new_height;

new_width new_height if, .

abs() , float, float, . - :

bool isEqual(float first, float second)
{
   if(fabs(first-second)<0.0001)
      return true;
   return false;
}

int main()
{
   float x = (float) 1 / 3;
   if(isEqual(x,0.3333))
      printf("It is equal\n");
   else
      printf("It is not equal\n");
}
+1

, .

https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

"ULP, - ". float ints, , , .. ( , , , , , )

bool AlmostEqualUlps(float A, float B, int maxUlpsDiff){
    Float_t uA(A);
    Float_t uB(B);
    // Different signs means they do not match.
    if (uA.Negative() != uB.Negative()) {
        // Check for equality to make sure +0==-0
        if (A == B) {
            return true;
        }
        return false;
    }

    // Find the difference in ULPs.
    int ulpsDiff = abs(uA.i - uB.i);
    if (ulpsDiff <= maxUlpsDiff) {
        return true;
    }
    return false;
}

, .

-1
source

All Articles