Mapping logical expressions to arithmetical expressions

47 Views Asked by At

I wrote a program that is able to evaluate arithmetical expressions, like

1 + 3.0 * 4 / (1 - 1 * 3.2)

What I want is to use the same program to evaluate logical expressions. For that, suppose $1$ means true and $0$ means false. For example, let's consider the logical operation AND

0 ^ 0 = 0
0 ^ 1 = 0
1 ^ 0 = 0
1 ^ 1 = 1

We could easily represent the AND operator using multiplication

0 * 0 = 0
0 * 1 = 0
1 * 0 = 0
1 * 1 = 1

My question is: given a logical expression (e.g.: $1 \lor \lnot1\land(0\rightarrow 1)$), is there a mapping to turn it into an arithmetical expression such that its evaluation would give the same value than the logical?

2

There are 2 best solutions below

0
On BEST ANSWER

Yes, this is possible:

  • NOT(A) = 1-A
  • AND(A,B) = A*B = min(A,B)
  • OR(A,B) = A+B-A*B = max(A,B)
  • IMPLIES(A,B) = 1-A+A*B
  • BIIMPLIES(A,B) = 1-|A-B|
  • XOR(A,B) = |A-B|
0
On

it depends on the programming language you use, but the answer is yes for most of languages:

for (A AND B) use A * B

for (A OR B) use (A + B) - (A + B) / 2 where "/" is integer division, not real division

for (NOT A) use (A + 1) mod 2

for all other - the answer is yes as soon as you can express them with AND, OR and NOT operations

for example (A => B) = (NOT A) OR B, etc.