How to resolve the algorithm Ternary logic step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Ternary logic step by step in the C programming language

Table of Contents

Problem Statement

In logic, a three-valued logic (also trivalent, ternary, or trinary logic, sometimes abbreviated 3VL) is any of several many-valued logic systems in which there are three truth values indicating true, false and some indeterminate third value.
This is contrasted with the more commonly known bivalent logics (such as classical sentential or boolean logic) which provide only for true and false. Conceptual form and basic ideas were initially created by Łukasiewicz, Lewis and Sulski. These were then re-formulated by Grigore Moisil in an axiomatic algebraic form, and also extended to n-valued logics in 1945.

Note:   Setun   (Сетунь) was a   balanced ternary   computer developed in 1958 at   Moscow State University.   The device was built under the lead of   Sergei Sobolev   and   Nikolay Brusentsov.   It was the only modern   ternary computer,   using three-valued ternary logic

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Ternary logic step by step in the C programming language

Source Code Overview

The provided source code demonstrates operations on trinary (three-valued) logic using different programming language implementations. Trinary logic extends binary logic (true/false) by introducing a third value, "maybe."

Implementation Details

C Version Using Enumerations:

  • Defines an enumeration trit with three values: TRITTRUE, TRITMAYBE, and TRITFALSE (equivalent to 0, 1, and 2, respectively).
  • Implements logical operators (not, and, or, then, equiv) using lookup tables.
  • Provides a simple demonstration of these operators.

C Version Using Functions:

  • Defines a trit enum with values t_F, t_M, t_T (equivalent to -1, 0, and 1, respectively).
  • Implements logical operators as functions: t_not, t_and, t_or, t_eq, t_imply.
  • Demonstrates these operators using a function show_op that prints a truth table.

C Version Using Double Precision Floating-Point Numbers:

  • Defines a type half_truth (alias for double) to represent the truth values.
  • Implements logical operators using formulas instead of lookup tables.
  • Demonstrates these operators using random values.

Core Concepts

  • Trinary Logic: A system of logic that has three truth values instead of two.
  • Truth Values: The basic elements of logic, representing truth, falsehood, or a state of uncertainty.
  • Logical Operators: Functions that operate on truth values to produce a new truth value. Common operators include negation (NOT), conjunction (AND), disjunction (OR), implication (THEN), and equivalence (EQUIV).
  • Lookup Tables: Data structures that store the results of operations for all possible combinations of input values.

Example Usage

The main function in each implementation demonstrates the trinary logical operators by printing truth tables or random values.

Advantages of Using Trinary Logic

  • Modeling Uncertainty: The "maybe" value allows for representing states of uncertainty or partial knowledge.
  • Extending Binary Logic: Trinary logic can be used to extend the capabilities of binary logic by introducing an additional truth value.
  • Applications: Trinary logic has been used in various fields such as artificial intelligence, decision-making, and fuzzy systems.

Source code in the c programming language

#include <stdio.h>
 
typedef enum {
  TRITTRUE,  /* In this enum, equivalent to integer value 0 */
  TRITMAYBE, /* In this enum, equivalent to integer value 1 */
  TRITFALSE  /* In this enum, equivalent to integer value 2 */
} trit;
 
/* We can trivially find the result of the operation by passing
   the trinary values as indeces into the lookup tables' arrays. */
trit tritNot[3] = {TRITFALSE , TRITMAYBE, TRITTRUE};
trit tritAnd[3][3] = { {TRITTRUE, TRITMAYBE, TRITFALSE},
                       {TRITMAYBE, TRITMAYBE, TRITFALSE},
                       {TRITFALSE, TRITFALSE, TRITFALSE} };
 
trit tritOr[3][3] = { {TRITTRUE, TRITTRUE, TRITTRUE},
                      {TRITTRUE, TRITMAYBE, TRITMAYBE},
                      {TRITTRUE, TRITMAYBE, TRITFALSE} };
 
trit tritThen[3][3] = { { TRITTRUE, TRITMAYBE, TRITFALSE},
                        { TRITTRUE, TRITMAYBE, TRITMAYBE},
                        { TRITTRUE, TRITTRUE, TRITTRUE } };
 
trit tritEquiv[3][3] = { { TRITTRUE, TRITMAYBE, TRITFALSE},
                         { TRITMAYBE, TRITMAYBE, TRITMAYBE},
                         { TRITFALSE, TRITMAYBE, TRITTRUE } };

/* Everything beyond here is just demonstration */

const char* tritString[3] = {"T", "?", "F"};

void demo_binary_op(trit operator[3][3], const char* name)
{
  trit operand1 = TRITTRUE; /* Declare. Initialize for CYA */
  trit operand2 = TRITTRUE; /* Declare. Initialize for CYA */

  /* Blank line */
  printf("\n");

  /* Demo this operator */
  for( operand1 = TRITTRUE; operand1 <= TRITFALSE; ++operand1 )
  {
    for( operand2 = TRITTRUE; operand2 <= TRITFALSE; ++operand2 )
    {
      printf("%s %s %s: %s\n", tritString[operand1],
                               name,
                               tritString[operand2],
                               tritString[operator[operand1][operand2]]);
    }
  }

}

int main()
{
  trit op1 = TRITTRUE; /* Declare. Initialize for CYA */
  trit op2 = TRITTRUE; /* Declare. Initialize for CYA */
 
  /* Demo 'not' */
  for( op1 = TRITTRUE; op1 <= TRITFALSE; ++op1 )
  {
    printf("Not %s: %s\n", tritString[op1], tritString[tritNot[op1]]);
  }
  demo_binary_op(tritAnd, "And");
  demo_binary_op(tritOr, "Or");
  demo_binary_op(tritThen, "Then");
  demo_binary_op(tritEquiv, "Equiv");

 
  return 0;
}


#include <stdio.h>

typedef enum { t_F = -1, t_M, t_T } trit;

trit t_not  (trit a) { return -a; }
trit t_and  (trit a, trit b) { return a < b ? a : b; }
trit t_or   (trit a, trit b) { return a > b ? a : b; }
trit t_eq   (trit a, trit b) { return a * b; }
trit t_imply(trit a, trit b) { return -a > b ? -a : b; }
char t_s(trit a) { return "F?T"[a + 1]; }

#define forall(a) for(a = t_F; a <= t_T; a++)
void show_op(trit (*f)(trit, trit), const char *name) {
	trit a, b;
	printf("\n[%s]\n    F ? T\n  -------", name);
	forall(a) {
		printf("\n%c |", t_s(a));
		forall(b) printf(" %c", t_s(f(a, b)));
	}
	puts("");
}

int main(void)
{
	trit a;

	puts("[Not]");
	forall(a) printf("%c | %c\n", t_s(a), t_s(t_not(a)));

	show_op(t_and,   "And");
	show_op(t_or,    "Or");
	show_op(t_eq,    "Equiv");
	show_op(t_imply, "Imply");

	return 0;
}


#include <stdio.h>
#include <stdlib.h>

typedef double half_truth, maybe;

inline maybe not3(maybe a) { return 1 - a; }

inline maybe
and3(maybe a, maybe b) { return a * b; }

inline maybe
or3(maybe a, maybe b) { return a + b - a * b; }

inline maybe
eq3(maybe a, maybe b) { return 1 - a - b + 2 * a * b; }

inline maybe
imply3(maybe a, maybe b) { return or3(not3(a), b); }

#define true3(x) ((x) * RAND_MAX > rand())
#define if3(x) if (true3(x))

int main()
{
	maybe roses_are_red = 0.25; /* they can be white or black, too */
	maybe violets_are_blue = 1; /* aren't they just */
	int i;

	puts("Verifying flowery truth for 40 times:\n");

	puts("Rose is NOT red:"); /* chance: .75 */
	for (i = 0; i < 40 || !puts("\n"); i++)
		printf( true3( not3(roses_are_red) ) ? "T" : "_");

	/* pick a rose and a violet; */
	puts("Rose is red AND violet is blue:");
	/* chance of rose being red AND violet being blue is .25 */
	for (i = 0; i < 40 || !puts("\n"); i++)
		printf( true3( and3(roses_are_red, violets_are_blue) )
			? "T" : "_");

	/* chance of rose being red OR violet being blue is 1 */
	puts("Rose is red OR violet is blue:");
	for (i = 0; i < 40 || !puts("\n"); i++)
		printf( true3( or3(roses_are_red, violets_are_blue) )
			? "T" : "_");

	/* pick two roses; chance of em being both red or both not red is .625 */
	puts("This rose is as red as that rose:");
	for (i = 0; i < 40 || !puts("\n"); i++)
		if3(eq3(roses_are_red, roses_are_red)) putchar('T');
		else putchar('_');

	return 0;
}


  

You may also check:How to resolve the algorithm Extreme floating point values step by step in the C programming language
You may also check:How to resolve the algorithm Number names step by step in the C programming language
You may also check:How to resolve the algorithm Temperature conversion step by step in the C programming language
You may also check:How to resolve the algorithm Kronecker product step by step in the C programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the C programming language