AnandTech Forums DeMorgan's laws. Insane... (JavaScript)
 Register FAQ Calendar Mark Forums Read

Forums
 · Hardware and Technology · CPUs and Overclocking · Motherboards · Video Cards and Graphics · AMD Video Cards · Nvidia · Displays · 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 · Home and Garden · Merchandise and Shopping · For Sale/Trade · Hot Deals with Free Stuff/Contests · Black Friday 2015 · Forum Issues · Technical Forum Issues · Personal Forum Issues · Suggestion Box · Moderator Resources · Moderator Discussions
10-28-2012, 11:41 PM   #2
iCyborg
Golden Member

Join Date: Aug 2008
Posts: 1,146

Quote:
 Originally Posted by ibex333 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 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.

 10-28-2012, 11:53 PM #3 fread2281 Member   Join Date: Sep 2012 Posts: 32 Think of it like x !== z || x !== q -> true || true -> false x !== z && x !== q -> true && true -> true
 10-29-2012, 07:10 AM #4 Tweak155 Lifer     Join Date: Sep 2003 Location: Michigan Posts: 10,109 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. __________________ Heatware 98-0-0 I5-6600K @ 4.2ghz, GTX970, 2x8gb 2400mhz DDR4, 500gb Samsung 850 Evo, 40" 4K Seiki Pro
 10-29-2012, 11:43 PM #5 Ken g6 Programming Moderator Elite Member     Join Date: Dec 1999 Location: 🌲 Colorado 🌲 Posts: 11,601 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: August 2-5 Serial Peacemaker: i3-6100, 32GB DDR4-2666, GA-Z170N, .5TB MX200, 9TB HDDs .!
10-30-2012, 04:51 PM   #6
uclabachelor
Senior Member

Join Date: Nov 2009
Posts: 447

Quote:
 Originally Posted by ibex333 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.

 11-04-2012, 11:27 PM #7 LumbergTech Diamond Member   Join Date: Sep 2005 Posts: 3,624 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