I have the following expression in python:
if 0.85 < 0.81 / 0.83 < 1.15 :
//do something
When I put this in python there is no problem and it returns a boolean (true) but I don't understand what '/' is? because it looks like your dividing two booleans. What does this expression evaluation to in java?
In java,
if (0.85 < 0.81 / 0.83 && 0.81 / 0.83 < 1.15) {
//do something
}
// A better solution as mentioned by #Makoto
float f = 0.81/0.83
if (0.85<f && f< 1.15) {
//do something
}
In Python, all comparison operations have the same priority. It can be chained arbitrarily, e.g., x < y < z is equivalent to x < y and y < z. Refer to Python documentation: Expressions for the detailed description.
you might want to use and(&&) to get a True :
if (0.85 < 0.81 / 0.83 && 0.81 / 0.83 < 1.15) {
//do something
}
Going to the docs, the answer to your question is literally spelled out in these two sections (both on the same page):
https://docs.python.org/3/reference/expressions.html#operator-precedence
https://docs.python.org/3/reference/expressions.html#comparisons
The first section gives a table that states that / has higher precedence than <, so your expression is effectively 0.85 < (0.81 / 0.83) < 1.15, or 0.85 < 0.9759036144578315 < 1.15.
The second section states:
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).
This means that your statement translates exactly as
double x = 0.81 / 0.83;
if(0.85 < x && x < 1.15) {
// ...
}
The key here is that with comparison chaining, each expression is only evaluated once. In this case that means computing the division only once. Of course the Java compiler would probably have optimized that out for you anyway.
Related
Im pretty new to this plattform and well I have a trouble regarding boolean expression in my CS-class. I have to simplifly this boolean expression and have no clue how to do this.
double x ;
double y ;
boolean b = ( (y < -x) ^ (5 * x >= y) ) && ( (x < -y) != (x >= y * 0.2) )
Assuming pure math, y < -x is equivalent to x < -y, 5 * x >= y is equivalent to x >= y * 0.2 and whateverBoolean1 ^ whateverBoolean2 is equivalent to whateverBoolean1 != whateverBoolean2. Hence you can omit any side of && operator.
I am neglecting freaks of floating point arithmetics (0.2 is not precise number so some counterexample that original and simplified expression are not equivalent could be perhaps found after some effort.)
Can any kind soul please explain why
! (x < 0 && y < 0)
is not equivalent to the following two expressions
!(x < 0) && ! (y < 0) AND x > 0 && y > 0
In the first code doesn't it imply that, x is not less than 0 and y is not less than 0? and does it also not mean that x and y should be more than 0?
Any help is much appreciated!
In your two rewritten versions, you'd need OR (||) rather than AND (&&). This is true any time you invert an AND condition's component parts.
! (x < 0 && y < 0) is true if x is >= 0 and y is < 0. To get that same result in the other form, you'd need x >= 0 || y >= 0. (Note that it's >=, not just >, but the main point was the || rather than &&.)
As ernest_k points out, this is one specific application of De Morgan's laws.
Let's calculate the equivalent expression for ! (x < 0 && y < 0)
Note that if negation comes before &&, it becomes || and vice versa. so your expression would be equal to :
! (x < 0 && y < 0) ---> !(x<0) || !(y<0) ---> x>=0 || y>=0
it's like the figure bellow, colored area is the result of your expression :
In the first example both of the expressions in brackets ("(x < 0)" and "(y < 0)") have to be equal to "true" for the whole expression to become "false".
In the second example the first two expressions contain each one of the expressions, which are inside the brackets of the first example ("(x < 0)" and "(y < 0)"). So only one of these expressions being "true", can cause the whole expression to become "false" since everything is connected with AND operators.
You can set x=0 and y=-1 and try it out manually.
You also might be interested in having a look at https://en.wikipedia.org/wiki/De_Morgan's_laws
I have some problems with my java code here. When I try to compile I get the error:
"bad operand types for binary operator '&&'" for the if statement
which I usually get when I made an error in the boolean logic. For the past half hour I've tried to find the mistake in my code but I'm not able to find it.
Any pointers?
code below
public String toString(){
double x1 = this.ecke1.getX();
double y1 = this.ecke1.getY();
double x2 = this.ecke2.getX();
double y2 = this.ecke2.getY();
double[] Y = {y1,y2,0};
double[] X = {x1,x2,0};
for (int y = 0; y <= Utils.max(y1,y2,0); y++)
for(int x = 0; x <= Utils.max(x1,x2,0); x++)
if ( ((x=0) && (y=0) ) || ( ( (y/x) <= (Utils.min(X)/Utils.max(Y)) ) && ( (y/x) >= (Utils.min(Y)/Utils.max(X)) ) && ( (y/x) <= ((Utils.max(Y)-Utils.min(Y)) / (Utils.min(X) - Utils.max(X))) ) ) )
System.out.print("#");
else system.out.print("-");
}
Change
(x=0) && (y=0)
to
(x==0) && (y==0)
x=0 is an assignment, so it doesn't evaluate to a boolean. x==0 is a comparison, and returns a boolean.
Use double equal to sign(==) instead of single(=)
if (((x==0) && (y==0) )...)
You use single equal sign for assignment and double for comparison.
= is an assignment operator while == is a comparison operator.
So you need to use x == 0 && y == 0.
I'm working on a small game which has a graph. The idea is I do an action whilst the target location (designated by upper and lower bounds) has no been met (within 0.5). For example, if I target (7,7), the loop should stop when x and y are (both in this case) between 6.5 and 7.5.
However, having something such as the following condition has presented me with a problem when presented with negative numbers:
while ((X < tarX-0.5 || X > tarX+0.5) && (Y < tarY-0.5 || Y > tarY+0.5))
For example: if I have the target (-7,-7) then the loop will stop when ONE of the x or y values is in the range, not both.
Basically, I had the idea of having four different loops depending on whether x or y was positive. But I'm wondering if there's an easier way? (I did try to use Math.abs() to counteract the negative numbers, which worked but something destined for (-3,-3) could still then stop at (3,3))
loop should stop when x and y are (both in this case) between 6.5 and 7.5.
while (!(x >= 6.5 && x <= 7.5 && y >= 6.5 && y <= 7.5)) {
...
}
Applying De Morgan's Law, the above is equivalent to
while (x < 6.5 || x > 7.5 || y < 6.5 || y > 7.5) {
...
}
I've misplaced += with =+ one too many times, and I think I keep forgetting because I don't know the difference between these two, only that one gives me the value I expect it to, and the other does not.
Why is this?
a += b is short-hand for a = a + b (though note that the expression a will only be evaluated once.)
a =+ b is a = (+b), i.e. assigning the unary + of b to a.
Examples:
int a = 15;
int b = -5;
a += b; // a is now 10
a =+ b; // a is now -5
+= is a compound assignment operator - it adds the RHS operand to the existing value of the LHS operand.
=+ is just the assignment operator followed by the unary + operator. It sets the value of the LHS operand to the value of the RHS operand:
int x = 10;
x += 10; // x = x + 10; i.e. x = 20
x =+ 5; // Equivalent to x = +5, so x = 5.
+= → Add the right side to the left
=+ → Don't use this. Set the left to the right side.
a += b equals a = a + b. a =+ b equals a = (+b).
x += y
is the same as
x = x + y
and
x =+ y
is wrong but could be interpreted as
x = 0 + y
It's simple.
x += 1 is the same as x = x + 1 while
x =+ 1 will make x have the value of (positive) one
Some historical perspective: Java inherited the += and similar operators from C. In very early versions of C (mid 1970s), the compound assignment operators had the "=" on the left, so
x =- 3;
was equivalent to
x = x - 3;
(except that x is only evaluated once).
This caused confusion, because
x=-1;
would decrement x rather than assigning the value -1 to it, so the syntax was changed (avoiding the horror of having to surround operators with blanks: x = -1;).
(I used -= and =- in the examples because early C didn't have the unary + operator.)
Fortunately, Java was invented long after C changed to the current syntax, so it never had this particular problem.
Because =+ is not a Java operator.