Skip to main content

Balanced ternary arithmetic and multi-valued logic library

Project description

tritlib

Balanced ternary arithmetic and multi-valued logic library for Python.

What is balanced ternary?

Balanced ternary is a numeral system in base 3 where each digit (called a trit) takes values from {−1, 0, +1} instead of the conventional {0, 1, 2}. It allows representing negative numbers without a sign bit, and negation is as simple as inverting every trit. Donald Knuth called it "perhaps the prettiest number system of all."

Features

  • Trit — immutable single‑trit type with arithmetic and logic operators
  • Trits — arbitrary‑length balanced ternary integers
  • Tryte — fixed‑width ternary words
  • Multiple ternary logic systems: Kleene (K3), Łukasiewicz (L3), Heyting (HT), Bochvar and R-mingle 3 (RM3)
  • Step‑by‑step arithmetic (carry propagation, partial products) for educational use
  • Pure Python, no dependencies, type‑hinted

Installation

pip install tritlib

Quick start

from tritlib.trit import Trit, P, Z, N

# Create trits
t = Trit(1)   # +1
print(t)       # "+"
print(-t)      # "-"

# Predefined constants
assert P == Trit(1)
assert Z == Trit(0)
assert N == Trit(-1)

Roadmap

  • Trit type with immutability, arithmetic, hashing
  • Trit‑level logic (Kleene K3)
  • Trits arbitrary‑precision integers
  • Tryte fixed‑width words
  • Additional logic systems (L3, HT, Bochvar, RM3)
  • Conversion utilities (int, str, float, binary‑coded ternary)
  • Educational step‑by‑step computation output

Development

git clone https://codeberg.org/newick_2/tritlib.git
cd tritlib
python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
python -m pytest

License

MIT License — see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tritlib-0.5.0a0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tritlib-0.5.0a0-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file tritlib-0.5.0a0.tar.gz.

File metadata

  • Download URL: tritlib-0.5.0a0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for tritlib-0.5.0a0.tar.gz
Algorithm Hash digest
SHA256 18383c9dc4b678ac56a041f39a44ad7ea9b6c4e1232dfbd491d6b5b379c73b75
MD5 ed8a2b940edbdffc7b3016d037444a74
BLAKE2b-256 e85970326c331bb0e412e2e59e92e2331b374da8169074e6e06a9e2727798cea

See more details on using hashes here.

File details

Details for the file tritlib-0.5.0a0-py3-none-any.whl.

File metadata

  • Download URL: tritlib-0.5.0a0-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for tritlib-0.5.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 e283a69804363e9ed02c22966fcc5b5daa335ed37e8885ef9db8a7edf1c6ea23
MD5 43b29db17527e1e2a494b06e290f44fb
BLAKE2b-256 9b04577fca7d58cd9169aaa50744dd34a86f8ec7073e79781c42cf801e504bb9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page