Go Back   AnandTech Forums > Software > Programming

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2014
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

Reply
 
Thread Tools
Old 10-28-2012, 10:48 PM   #1
ibex333
Diamond Member
 
ibex333's Avatar
 
Join Date: Mar 2005
Location: Ukraine, Odessa - New York City, Brooklyn ; )
Posts: 3,231
Default DeMorgan's laws. Insane... (JavaScript)

Let me give you two statements:

1)As long as the answer is not x OR not y, do something.
2)As long as the answer is not x AND y, do something.

In the first scenario, that something STILL gets done even if the answer is x or y...

But I just said "do it ONLY if answer is x or y" !!!! What gives? This is driving me nuts!



In the second case as long as the answer is x or y, something will be done as intended. But it SHOULDN'T, because the statement says "do it ONLY if BOTH x AND y are the answer TOGETHER, AT THE SAME TIME"!


My code: (wrong)


while (natBornCitizen !== 'Y'
|| natBornCitizen !== 'y'
|| natBornCitizen !== 'Yes'
|| natBornCitizen !=='yes'
|| natBornCitizen !== 'N'
|| natBornCitizen !== 'n'
|| natBornCitizen !== 'No'
|| natBornCitizen != 'no');
{ alert }


someone else's code that "fixed" mine: (correct)

while (natBornCitizen !== 'Y'
&& natBornCitizen !== 'y'
&& natBornCitizen !== 'Yes'
&& natBornCitizen !=='yes'
&& natBornCitizen !== 'N'
&& natBornCitizen !== 'n'
&& natBornCitizen !== 'No'
&& natBornCitizen != 'no');
{ alert }




And yet, I still dont understand this....



EDIT: Hmm... I think I just understood this by writing this post...

As long as the answer is not x OR not y, do something. this is the same as saying as long as the answer is x and y do something... But than again, who negated the OR to AND? We only negated not x, not y!
__________________
2500k Sandy Bridge @ 4.40GHz(Yay!)~GIGABYTE Z68A-D3H-B3~G.SKILL 8GB DDR3 1333~
3.14 Tb HD Space~2x PNY GTX 760 GTX SLi~HT Omega Claro~Cooler Master CM690~600w OCZ StealthXtream~ASUS 27" Monitor
HEATWARE
EBAY

Last edited by ibex333; 10-28-2012 at 11:11 PM.
ibex333 is offline   Reply With Quote
Old 10-28-2012, 11:41 PM   #2
iCyborg
Golden Member
 
Join Date: Aug 2008
Posts: 1,041
Default

Quote:
Originally Posted by ibex333 View Post
In the first scenario, that something STILL gets done even if the answer is x or y...
This is incorrect: if both x and y are true, then (x or y) is still true, but not x or not y is false, so we shouldn't be doing something.

1) not x or not y - this means either x is false, or y is false, or both are false
2) not (x and y) - this means it's not true that both x and y are true

If it isn't obvious to you that these say the same thing, then I don't know, perhaps just work out via truth tables and accept it as a fact.

Quote:
Originally Posted by ibex333 View Post
As long as the answer is not x OR not y, do something. this is the same as saying as long as the answer is not( x and y ) do something... But than again, who negated the OR to AND? We only negated not x, not y!
Fixed it for you. OR gets negated to AND when you pull not out, the above gives the common sense reason, and if you don't agree, then simply accept De Morgan's laws as rules of logic, which can be proven via truth tables to be identical


Look at your code: your condition can never be false, it's always true. In order for it to be false, natBornCitizen would have to be equal to 'Y', 'y', Yes' ..., all those at the same time, which it clearly can't be. E.g. if natBornCitizen is 'Y', then it's not equal to 'y', so the second statement is true, and since you're OR-ing, the whole condition is true. You're always alerting.
The correct code is false if all those are true, so natBornCitizen is not equal to any of those, which is probably the intended meaning.
iCyborg is offline   Reply With Quote
Old 10-28-2012, 11:53 PM   #3
fread2281
Member
 
Join Date: Sep 2012
Posts: 32
Default

Think of it like
x !== z || x !== q -> true || true -> false
x !== z && x !== q -> true && true -> true
fread2281 is offline   Reply With Quote
Old 10-29-2012, 07:10 AM   #4
Tweak155
Diamond Member
 
Tweak155's Avatar
 
Join Date: Sep 2003
Location: Michigan
Posts: 8,972
Default

2 is just the simple way of saying 1.

if not (x && y) then....

Or in otherwords, (x != 1) && (y != 1) assuming bits are only being used.
__________________
Organo Gold Coffee

Heatware 92-0-0
Tweak155 is offline   Reply With Quote
Old 10-29-2012, 11:43 PM   #5
Ken g6
Elite Member
 
Ken g6's Avatar
 
Join Date: Dec 1999
Location: Colorado
Posts: 8,823
Default

If you like logical statements in English, then you might want to look up the contrapositive. I think it's similar, though there's an inequality involved.

If you do not want to look up the contrapositive, then you might not like logical statements in English.
__________________
Look for really BIG primes with TeAm AnandTech PrimeGrid!
Next race: November 19-24
Ken g6 is offline   Reply With Quote
Old 10-30-2012, 04:51 PM   #6
uclabachelor
Senior Member
 
uclabachelor's Avatar
 
Join Date: Nov 2009
Posts: 361
Default

Quote:
Originally Posted by ibex333 View Post
Let me give you two statements:

1)As long as the answer is not x OR not y, do something.
2)As long as the answer is not x AND y, do something.

In the first scenario, that something STILL gets done even if the answer is x or y...

But I just said "do it ONLY if answer is x or y" !!!! What gives? This is driving me nuts!



In the second case as long as the answer is x or y, something will be done as intended. But it SHOULDN'T, because the statement says "do it ONLY if BOTH x AND y are the answer TOGETHER, AT THE SAME TIME"!


My code: (wrong)


while (natBornCitizen !== 'Y'
|| natBornCitizen !== 'y'
|| natBornCitizen !== 'Yes'
|| natBornCitizen !=='yes'
|| natBornCitizen !== 'N'
|| natBornCitizen !== 'n'
|| natBornCitizen !== 'No'
|| natBornCitizen != 'no');
{ alert }


someone else's code that "fixed" mine: (correct)

while (natBornCitizen !== 'Y'
&& natBornCitizen !== 'y'
&& natBornCitizen !== 'Yes'
&& natBornCitizen !=='yes'
&& natBornCitizen !== 'N'
&& natBornCitizen !== 'n'
&& natBornCitizen !== 'No'
&& natBornCitizen != 'no');
{ alert }




And yet, I still dont understand this....



EDIT: Hmm... I think I just understood this by writing this post...

As long as the answer is not x OR not y, do something. this is the same as saying as long as the answer is x and y do something... But than again, who negated the OR to AND? We only negated not x, not y!
Draw a Karnaugh map and the answer will be obvious.
uclabachelor is offline   Reply With Quote
Old 11-04-2012, 10:27 PM   #7
LumbergTech
Diamond Member
 
Join Date: Sep 2005
Posts: 3,623
Default

Maybe I didn't think heavily enough on this when I learned it..but

I always just flipped the signs and the and/or when negating

!(!x or !y) = x and y

also

char natBorn = natBornCitizen.toLower().charAt(0);

while (natBorn !== 'y' && natBorn !== 'n')
{ alert }

really isn't "better" but a lot less cluttered
LumbergTech is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 11:49 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.