1

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!

2
  • 3
    Check De Morgan's law, for starters: en.wikipedia.org/wiki/De_Morgan%27s_laws Commented Oct 5, 2019 at 8:20
  • Everything else aside, your x > 0 && y > 0 should use >=, not just >. Commented Oct 5, 2019 at 8:22

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 : enter image description here

Comments

0

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.