How to resolve the algorithm Ternary logic step by step in the V (Vlang) programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ternary logic step by step in the V (Vlang) 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 V (Vlang) programming language

Source code in the v programming language

import math

fn main() {
    aa := [1.0,0.5,0]
    bb := [1.0,0.5,0]
    mut res :=''
    
    for a in aa {
        res += '\tTernary_Not\t' + a.str() + '\t=\t' + ternary_not(a) + '\n'
    }
    res += '-------------\n'
    
    for a in aa {
        for b in bb {
            res += a.str() + '\tTernary_And\t' + b.str() + '\t=\t' + ternary_and(a,b) + '\n'
        }
    }
    res += '-------------\n'
    
    for a in aa {
        for b in bb {
            res += a.str() + '\tTernary_or\t' + b.str() + '\t=\t' + ternary_or(a,b) + '\n'
        }
    }
    res += '-------------\n'
 
    for a in aa {
        for b in bb {
            res += a.str() + '\tTernary_then\t' + b.str() + '\t=\t' + ternary_if_then(a,b) + '\n'
        }
    }
    res += '-------------\n'
 
    for a in aa {
        for b in bb {
            res += a.str() + '\tTernary_equiv\t' + b.str() + '\t=\t' + ternary_equiv(a,b) + '\n'
        }
    }            
    res = res.replace('1.', 'true')
    res = res.replace('0.5', 'maybe')
    res = res.replace('0', 'false')
    println(res)
}

fn ternary_not(a f64) string {
    return math.abs(a-1).str()
}

fn ternary_and(a f64, b f64) string {
    return if a < b {a.str()} else {b.str()}
}

fn ternary_or(a f64, b f64) string {
    return if a > b {a.str()} else {b.str()}
}
 
fn ternary_if_then(a f64, b f64) string {
    return if a == 1 {b.str()} else if a == 0 {'1.'} else if a + b > 1 {'1.'} else {'0.5'}
}
 
fn ternary_equiv(a f64, b f64) string {
    return if a == b {'1.'} else if a == 1 {b.str()} else if b == 1 {a.str()} else {'0.5'}
}

  

You may also check:How to resolve the algorithm LZW compression step by step in the Raku programming language
You may also check:How to resolve the algorithm User input/Text step by step in the Phix programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the Prolog programming language
You may also check:How to resolve the algorithm Factorial step by step in the COBOL programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the Go programming language