0

This is compiled with GCC in CC mode.

I'm looking at some basic interpreter code, and this code is run when parsing the "AND" operator. int_one and int_two are the integer values of the arguments to AND like this in basic:

int_one AND int_two

This portion of the interpreter has already determined that the arguments are integer types (instead of real types), so why do we care how they compare when cast as reals?

i'm not 100% sure what this check does, and furthermore, why it almost always evaluates to false! What is the significance of this check, and why on earth is it false?

Here is some tester code that illustrates some probably false assumptions about buffering the and results, which gives a different result:

#include <stdio.h>
#include <stdlib.h>

int one, two;
int main()
{

    // Initialize
    one = 3;
    two = 3;

    // do a test with buffering the and result
    float int_and_cast_to_real = (float)(one && two);
    float int_cast_to_real_and = (float)one && (float)two;

    if (int_and_cast_to_real == int_cast_to_real_and) {
        // This message gets fired, so the buffered results are true.
        printf("oh they are equal buddy\n");
    } else {
        printf("you think they'd be equal, but they aint.\n");
    }

    // do it all at once
    if((float)(one && two) == (float)one && (float)two) {
        printf("int & real ands give equal result\n");
    } else {
        // This message gets fired, so the unbuffered results are false.
        printf("int & real ands give inequal result\n");
    }


    return 0;
}
1
  • 4
    && has lower precedence than ==.
    – Bakuriu
    Jun 15, 2014 at 14:56

2 Answers 2

4

Yes. Because Boolean values are either 0 or 1. (one && two); will return Boolean 1 which after casting to float will give 1.000000 while (float)one will return 3.000000.

So, (float)(one && two) == (float)one && (float)two will be evaluated as ( (float)(one && two) == (float)one ) && (float)two (because of higher precedence of == than &&) which gives 0.000000 == 1.000000 which finally evaluated to false.

0
1

The (float)(one && two) == (float)one && (float)two conditions is evaluated as ((float)(one && two) == (float)one) && (float)two, but your intention seems to be (float)(one && two) == ((float)one && (float)two).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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