I am trying to simplify the following using DeMorgan's Law: ! (x!=0 || y !=0)
Does x!=0 simplify to x>0? Or am I wrong in the following:
!(x>0 || y>0)
!(x>0) && !(y>0)
((x<=0) && (y<=0))
Thanks.
Does x!=0 simplify to x>0?
No that's not true. Because integers are signed.
How to simplify :
!(x!=0 || y !=0)
?
Consider this rules :
- (second De Morgan's laws )
By 1., it implies
!(x!=0 || y !=0) <=> (!(x!=0)) && (!(y != 0))
By 2., it implies
(!(x!=0)) && (!(y != 0)) <=> (x == 0) && (y == 0)
To test you can write the following loop :
for(int x = -5; x < 5; x++){
for(int y = -5; y < 5; y++){
if(!(x!=0 || y !=0))
System.out.println("True : ("+x+","+y+")");
}
}
In java integers are always signed so it is not necessarily true that x!=0 is the same as x>0
DeMorgans Law states the following:
!(A & B) = !A | !B (I)
!(A | B) = !A & !B (II)
In your case (II)
applies: !(x>0 || y>0) = !(x>0) && !(y>0) = (x<=0) && (y<=0)
PS: I don't understand what you mean by the following. Can you specify?
"Does x!=0 simplify to x>0?"
Does x!=0 simplify to x>0? Or am I wrong in the following:
x != 0 // reads x does not equal 0; any number BUT 0
x > 0 // reads x is greater than 0; only numbers greater than 0
Do these two look the same, when you write it out like this?
Combined
(x != 0 && x > 0) // any number above 0
(x != 0 || x > 0) // any number BUT 0
The conversion of the first two comparisons according to De' Morgan's law is the following.
!(x>0 || y>0) ---> x <= 0 && y <= 0
!(x>0) && !(y>0) ---> !(x <=0 || y <=0)
When I teach how to write Java do-while loops, I explain how to write the condition which terminates the loop.
For example, if I want to ask the user to enter a value which must be 0, 1, 2, or 3, I want the while condition to continue if the input value is not (value >= 0 and value <= 3).
This translates into while (!(value >= 0) or !(value <= 3)).
But !(value >= 0) means (value < 0), and !(value <= 3) means (value > 3), so the while loop is written as while (value < 0 || value > 3).
Hope this helps.
来源:https://stackoverflow.com/questions/20043664/de-morgans-law