| Dangerous Logic - De Morgan & Programming |
| Written by Mike James | ||||||
| Tuesday, 28 October 2025 | ||||||
Page 2 of 2
Negation and De Morgan's LawThe big problem with conditions and logical expressions in general is that we often want them the other way around. For example, if you have a condition that is true if there is an error you might want to write:
or perhaps
If you want to be safe then just write things exactly like this. But for strange reasons some programmers hate writing the NOT of an expression. I've even seen programmers write things like:
to avoid it. It is even more tempting to try to convert the logic when you have an expression like:
This is where the problems begin and the errors get introduced. What is the equivalent of the above? Some programmers will write:
and simplify this to:
Many just go straight to the final expression without the intermediate step but it doesn't matter how you get there - it's wrong. To see this, just consider the way the intervals in the two expressions intersect. The key to this sort of manipulation are De Morgan's laws. The first law is that
You can see that this gives you a way of getting rid of the overall NOT in front of the expression but notice that you have two NOTS and the AND has changed to an OR. The second law you can probably guess:
and this has the same form, only it swaps an OR for and AND. Sometimes De Morgan's laws are used for this very purpose, i.e. to swap an AND for an OR. Sometimes they are used to get rid of lots of NOTs inside the expression and replace it with a single NOT. Whatever the reason, De Morgan's laws are really useful. For example to convert:
into something that doesn't have NOTs, you would first apply De Morgan;s first law:
Then you could use the Venn diagram approach to convert the two intervals into:
and
Putting this all together gives:
Again if you compare the interval diagrams you will see that the two ways of writing the expressions are the same. Yes, I know that you would never make such silly mistakes, but the real life situations are always more complicated. Many programmers find that the idea of switching a NOT around also involves swapping an AND for an OR or an OR for an AND just doesn't seem right. There are many simple looking simplifications of logic that we are all tempted to do "in our heads". How many hours have been wasted trying to debug such trivia? My advice is that if you have a logical expression in a natural understandable form, then leave it as it is and if you need it's negation use a NOT. If you really want to optimize then check the algebra carefully. If you are wondering about the man who is known for De Margan's Law, he was a British mathematician and logician who tutored Ada Lovelace in the nineteenth century.
Augustus De Morgan (June 27, 1806 - March 18, 1871)
Related ArticlesThe Greeks, George Boole and Prolog To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info <ASIN:1871962439> |
||||||
| Last Updated ( Sunday, 02 November 2025 ) |

