Ternary logic: Difference between revisions

Content added Content deleted
(Switch from custom values to Javascript native values (false, undefined, true) which leads to a more usable library. Fix an error with the test output for the NOT function.)
m (More consistent logical symbols in the test.)
Line 3,099: Line 3,099:
nand = (a, b) => (a == false || b == false) ? true : (a == undefined || b == undefined) ? undefined : false
nand = (a, b) => (a == false || b == false) ? true : (a == undefined || b == undefined) ? undefined : false
not = (a) => nand(a, a)
not = (a) => nand(a, a)
same = (a, b) => (a == undefined || b == undefined) ? undefined : a === b
and = (a, b) => not(nand(a, b))
and = (a, b) => not(nand(a, b))
or = (a, b) => nand(not(a), not(b))
or = (a, b) => nand(not(a), not(b))
Line 3,110: Line 3,109:
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
trit = [false, undefined, true]
trit = [false, undefined, true]
functor = {same, nand, and, or, nor, implies, iff, xor}
functor = {nand, and, or, nor, implies, iff, xor}
display = {same: '≡', nand: '⊼', and: '∧', or: '∨', nor: '⊽', implies: '', iff: '', xor: '⊻', not: '¬', false: 'F', undefined: 'U', true: 'T'}
display = {nand: '⊼', and: '∧', or: '∨', nor: '⊽', implies: '', iff: '', xor: '⊻', not: '¬', false: 'F', undefined: '?', true: 'T'}


log = 'NOT\n';
log = 'NOT\n';
for (let a of trit) log += `${display.not}${display[a]} = ${display[not(a)]}\n`
for (let a of trit) log += `${display.not}${display[a]} = ${display[not(a)]}\n`


log += '\nSAME NAND AND OR NOR IMPLIES IFF XOR'
log += '\nNAND AND OR NOR IMPLIES IFF XOR'
for (let a of trit) {
for (let a of trit) {
for (let b of trit) {
for (let b of trit) {