I've been really clumsy and ran into a bug due to my own fault. Basically:
#include <iostream>
float a = 4.f;
float b = 6.f;
int main()
{
if (!a < b)
{
std::cout << "a is not less than b";
}
else std::cout << "a is less than b";
std::cin.ignore();
return 0;
}
So this conditional if statement always evaluates to true. If I wrap it in brackets as !(a < b) it works. So I understand I need the brackets.
My question is how is my wrong example parsed?
if (!a < b)
a has float value 4.f, does it turn 4.f from float value to boolean value, which is true, and then negate it to false, and then compare the false boolean value to b, which is 6.f float? How would it go about comparing a false boolean value to a float value of 6.f? If it converts 6.f float to boolean value the 6.f would turn into true, and the statement would be if (false < true)
, which doesn't make sense. Is this the way it's working?
I'm not sure if it's turned to if (false < true)
because:
float a = 4.f;
float b = -50.f;
int main()
{
if (!a < b)
{
std::cout << "a is not less than b";
}
else std::cout << "a is less than b";
std::cin.ignore();
return 0;
}
Always ends up false. I thought that -50.f should be turned to true. On the other hand, if I assign to an intermediary variable it's a different case:
float a = 4.f;
float b = -50.f;
int main()
{
bool temp = b;
if (!a < temp)
{
std::cout << "a is not less than b";
}
else std::cout << "a is less than b";
std::cin.ignore();
return 0;
}
Now it's always true.
Thanks to user apple apple for figuring this out, it turns out it parses as follows:
if (!a < b)
if (!4.f < 6.f) // Changes 4.f float to false boolean
if (false < 6.f)
if (0.f < 6.f) // Promotes false boolean to 0.f