Why is:
if(x!=y!=z)
handled as:
x=1
y=1
z=2
??
I just noticed it today.
x != y and x == y return booleans.
You're comparing z to those booleans.
Neither of them will work they way you want them to.
It probably is parsed as if ((x!=y) !=z) which does not do what you think if (x!=y!=z) should do (but does not).
Likewise if (x==y==z) probably means if ((x==y)==z) to the compiler which is not what you want.
Enable the warnings given by your compiler. With GCC, that means gcc -Wall and it would tell you warning: suggest parentheses around comparison in operand of '=='
Recall that a boolean expression like x==y gives a zero (when false) or non-zero (when true) result. Writing ((x==y) + (z==t)) is very poor taste, but makes sense for the compiler.
x == y == z is equivalent to (x == y) == z. In this case, (1 == 1) == 2, or true == 2, which is false because true == 1, not 2.
x != y != z is equivalent to (x != y) != z. In this case, (1 != 1) != 2, or false != 2, which is true because false == 0, not 2.
C(++) relational operators aren't chained like in Python. If you want to check whether three numbers are all equal to each other, use (x == y) && (y == z).
if(x==y==z)
will not work untill the value u will use for z be 1 or 0 but u can take the value of x and y anything
As when u try with the value 1 or 0 then the if will take its parameters as
if((x==y)==z)
this is happening because it first evaluate whatever the value in x and y and the answer will be in boolean and then it checks with z which it expects to be boolean. so if (x==y) be and z is 1(true) then code will be executed else it wont.Same thing will happen with (x!=y!=z). try with z=1 or 0 and x,y be anything.
Related
OK so obviously this question might sound dumb for more experienced people, but, for the following lines the result I get is 0:
int x = 2,
y = -2;
cout << (x++ - y && (--x + y));
I understand it means that either one of these two expressions equals 0, but how? As far as I understand, this should be (3 && -1)?
Also, a little subquestion: when does x++ exactly take effect? On the next occurance of x within the same expression, after the left-shift operator within the same line, or in the next statement?
Thank you!
As far as I understand, this should be (3 && -1)?
you understand wrong:
first left side is fully evaluated, as it is necessary for short circuit evaluation with logical and (details can be found here)
x++ - y == 4 // as result of x++ == 2 so (2-(-2)), after that x == 3
result is true so right side is evaluated:
--x + y == 0 // as result of --x == 2 so (2+(-2)), after that x == 2
result on the right is false so result of and is false as well which printed by std::ostream as 0
Note: short circuit evaluation of logical or and and operations make such code valid (making them sequenced) but you better avoid such questionable expressions. For example simple replacing logical and to binary would make it UB.
I have a mixed integer linear program (MIP or MILP).
In the end I want a boolean variable im my linear program, that has the following properties.
I have two variables:
boolean b.
real x, with x being 0 or larger.
What I want to achieve is:
b == false if x == 0.
b == true if x > 0.
I found a way to depict if x is in specific range (e.g. between 2 and 3) via:
2*b <= x
x <= 3*b
The problem with the above testing formula is, that b will be true if x is in the given range and false if outside that range.
Does anybody know a way to set a boolean variable to false if x == 0 and to true if x is larger than 0?
If U is an upper bound of x then
if x > 0 ==> b == 1
can be made as
x <= U*b
The second part (x == 0 => b == 0) needs to be modified to
x < epsilon ==> b == 0
which can be made as
b <= 1 + x - epsilon
where epsilon is a small number. Other than good practice this is necessary, because solvers do not work in rational arithmetic (although there are some research efforts to make them do so), but with certain precision thresholds, and therefore quantities such as 10e-12 are treated as zero.
I hope this helps!
You could use the signum function http://en.wikipedia.org/wiki/Signum_function take the absolute value and negate it. Since you didn't name a specific programming language I keep it general.
Is this:
if(x == a || b){//do something}
same as:
if(x == a || x == b){//do something}
?
I think it is not, because in the first case we evaluate if x equals a and if b is true or false.
In the second case, we evaluate if x equals a and if x equals b. And I understand that in the lazy evaluation if x equals a than we are not evaluating further.
But somebody thinks that in the first case we ask if x equals a or b, so I wanna make sure.
No.
In C++, this:
x == a || b // Same as (x == a) || b
Is equivalent to this:
(x == a) || (bool)b
Which evaluates to true if x and a are equal OR if b evaluates to true when converted to bool. In C, on the other hand, it is equivalent to this:
(x == a) || (b != 0)
Which evaluate to true if x and a are equal OR if b is different from 0 (here we must make the implicit assumption that b is of integral type, otherwise this won't compile).
On the other hand, this:
(x == a || x == b) // Same as ((x == a) || (x == b))
Evaluates to true when either x and a are equal OR x and b are equal (i.e., if x is either equal to a or equal to b) both in C++ and in C.
The two expressions are not equivalent. This
if(x == a || b)
is the equivalent of
if( (x == a) || (b))
i.e an OR of x==a and b. In C++, if b evaluates to anything other than 0 or false, it is taken as true.
The second one tests whether x==b instead of simply testing b.
No. In C this is equivalent to:
if(x == a || b != 0)
The first reads as "if x is equal to a, or if b is truthy"
The second reads as "if x is equal to a, or if x is equal to b"
No.
if (x == a || b)
is equal to
if ((x == a) || (bool)b)
because operator == has higher precedence than operator ||. See Operator Precedence Table.
You are almost right, the first case means x equals a OR b is true.
Lazy evaluation means that the expression will be evaluated only until the result is obvious. In an OR expression, for example (x || y), the result will be known when x==true – then the whole expression must be true too. In the case of AND, like (x && y), the result will be evident when x==false. So you are right, if x==a, we know the answer already and no more work is needed.
Going through EASTL, I stumbled across a peculiar line of code. The following link shows the file with the line number of interest at 1870.
https://github.com/paulhodge/EASTL/blob/master/include/EASTL/algorithm.h
The code at that line is if(!(value < *i)). The comment says that "we always express value comparisons in terms of < or ==" without any explanation as to why this is so. There are also a few other areas where the same comment is placed but without any explanation.
Is there any benefit whatsoever to writing a comparison like that (maybe some context that I am overlooking)? If not, why did the author of EASTL deliberately wrote it in this particular fashion and even took the care to comment about it? Is consistency the only reason here?
It means you only need to provide < and == for container value types. It also means you reduce the amount of variability for those types (as all the algorithms use !(a<b) to mean a>=b and !(a==b) for a!=b); otherwise, you could have >= and != return inconsistent results.
In C++, you can overload the < operator so that it behaves differently than the opposite of >=, so they are not guaranteed to be equivalent.
Additionally, in any IEEE floating-point implementation, NaN < NaN is false, but so is NaN >= NaN, so !(NaN < NaN) is true even though NaN >= NaN is false.
I see at least one difference. If one of the numbers was QNAN (floating-point 0/0) then !(a < b) would've always return TRUE if any of a or b were QNAN, while it would've always returned false for a>=b
Using just the less-than operator, you can simulate all the other comparison operators. This makes it more consistent and allows you to use a single template parameter when you need to parameterize the comparison. The standard sorted containers and algorithms use std::less<T> as the default template comparator for example.
operation equivalent
x < y x < y
x > y y < x
x <= y !(y < x)
x >= y !(x < y)
x == y !(x < y) && !(y < x)
x != y (x < y) || (y < x)
For those operations where ordering is not important it's simpler and more efficient to use operator == instead.
Given that x = 2, y = 1, and z = 0, what will the following statement display?
printf("answer = %d\n", (x || !y && z));
It was on a quiz and I got it wrong, I don't remember my professor covering this, someone enlighten me please... I know the answer I get is 1, but why?
The expression is interpreted as x || (!y &&z)(check out the precedence of the operators ||, ! and &&.
|| is a short-circuiting operator. If the left operand is true (in case of ||) the right side operand need not be evaluated.
In your case x is true, so being a boolean expression the result would be 1.
EDIT.
The order of evaluation of && and || is guaranteed to be from left to right.
If I'm not mistaken, it will print 1. (Let's assume short circuiting is off)
(x || !y && z) or (true || !true && false) will first evaluate the ! operator giving (true || false && false)
Then the &&: (true || false)
Then || : true
Printf will interpret true in decimal as 1. So it will print answer = 1\n
Given that x = 2, y = 1, and z = 0,
what will the following statement
display?
printf("answer = %d\n", (x || !y && z));
Ok - feeling a bit guilty for the harsh quip re poor wording of the question, so I'll try to help you in a different way to the other answers... :-)
When you've a question like this, break it down into manageable chunks.
Try:
int x = 2, y = 1, z = 0;
printf("true == %d\n", 10 > 2); // prints "1"
printf("false == %d\n", 1 == 2); // prints "0"
printf("!y == %d\n", !y); // prints "0"
printf("(x || !y) == %d\n", x || !y); // "1" - SEE COMMENTS BELOW
printf("(!y || z) == %d\n", !y || z); // "0"
printf("(x || !y && z) == %d\n", x || !y && z); // "1"
In the output there, you've got everything you need to deduce what's happening:
true == 1 reveals how C/C++ convert truthful boolean expressions to the integral value 1 for printf, irrespective of the values appearing in the boolean expression
false == 0 reveals how C/C++ converts false expressions to "0"
(!y) == 0 because ! is the logical not operator, and C/C++ consider 0 to be the only integral value corresponding to false, while all others are true, so !1 == !true == false == 0
(x || !y) == 1, and you know !y is 0, so substituting known values and simplifying: (2 || 0) == 1 is equivalent to (true or false) == true... that's understandable as a logical rule
(!y || z) == 0 - substituting known values: (0 || 0) == (false or false) == false == 0
(x || !y && z) == 1: here's the crunch! From above, we know:
x || !y is 1/true, which if relevant would imply 1/true && z/0/false == 1/true <- this clearly doesn't make any sense, so it must not be the way C/C++ are calculating the answer!
(!y && z) is false, which if relevant would imply x/2/true || false == 1/true <- this is true, so it must be the implicit order.
In this way, we've derived the operator precedence - the order of evaluation of the || and && operators, from the results that the compiler is displaying, and seen that if and only if && is valuated before || then we can make some sense of the results.
answer = 1
or maybe:
answer = -27
2 || !1 && 0
2 || 0 && 0
2 || 0
true
true = non-zero value
printf("answer = %d",*true*); -> who knows
Most compilers will output answer = 1. I wouldn't confidently state that all compilers will do that though, but I am confident all compilers would return non-zero.
I'm not going to give you the outright answer because you could just compile it and run it, but the question is just testing to see if you know operator precedence.