I want to compare three values if they're unique or not, and here's my first statement, which I want to simplify and make it more readable using the DeMorgan's Law.
product1Id != product2Id && product1Id != product3Id
CodePudding user response:
I want to compare three values if they're unique or not,
Your code doesn't ensure that. C(3,2) = 3!/2!/(3-2)! = 3 comparisons are needed, but you only have two. The following is the correct check:
product1Id != product2Id &&
product1Id != product3Id &&
product2Id != product3Id
I want to simplify and make it more readable
DeMorgan's laws can be used to obtain
!( product1Id == product2Id || product1Id == product3Id )
(See a few ways this is done below.)
It's debatable whether this is more readable or not.
But maybe the !( ) can be left out by exchanging the "then" and "else" clauses of an if statement, simplifying the expression to just
product1Id == product2Id || product1Id == product3Id
Also, say you had to repeatedly make this check with different values for product1Id, and say you had a large number of product ids against with to check. Then one could use
// Setup
ProductSet *set = ProductSet_new();
ProductSet_add( set, product2Id );
ProductSet_add( set, product3Id );
!( ProductSet_has( set, product1Id ) )
How to apply DeMorgan's law in this C logical operation?
Approach 1: Starting from !( A && B ) or !( A || B )
Let's start by synthesizing this form by introducing a double-negation.
!( !( product1Id != product2Id && product1Id != product3Id ) )
|----------A-----------| |----------B-----------|
|----------------------!( A && B )----------------------|
Now, we can apply DeMorgan's second law (!( A && B ) ⇔ !A || !B).
|--------------------------!A || !B--------------------------|
|------------!A-------------| |------------!B-------------|
|----------A-----------| |----------B-----------|
!( !( product1Id != product2Id ) || !( product1Id != product3Id ) )
Let's simplify using A == B ⇔ !( A != B ).
!( product1Id == product2Id || product1Id == product3Id )
Approach 2a: Starting from !A && !B or !A || !B
Let's start by synthesizing this form by introducing double-negations.
!( !( product1Id != product2Id ) ) && !( !( product1Id != product3Id ) )
|-------------A-------------| |-------------B-------------|
|---------------!A---------------| |---------------!B---------------|
|-------------------------------!A && !B-------------------------------|
Now, we can apply DeMorgan's first law (!( A || B ) ⇔ !A && !B).
|---------------------------!( A || B )---------------------------|
|---------------------------A || B---------------------------|
|-------------A-------------| |-------------B-------------|
!( !( product1Id != product2Id ) || !( product1Id != product3Id ) )
Let's simplify using !( A != B ) ⇔ A == B.
!( product1Id == product2Id || product1Id == product3Id )
Approach 2b: Starting from !A && !B or !A || !B
Let's start by synthesizing this form using A != B ⇔ !( A == B ).
!( product1Id == product2Id ) && !( product1Id == product3Id )
|----------A-----------| |----------B-----------|
|------------!A-------------| |------------!B-------------|
|--------------------------!A && !B --------------------------|
Now, we can apply DeMorgan's first law (!( A || B ) ⇔ !A && !B).
|----------------------!( A || B )----------------------|
|----------------------A || B----------------------|
|----------A-----------| |----------B-----------|
!( product1Id == product2Id || product1Id == product3Id )
