Crates.io | balanced-ternary |
lib.rs | balanced-ternary |
version | |
source | src |
created_at | 2025-02-19 02:48:22.544149+00 |
updated_at | 2025-03-03 06:52:53.692395+00 |
description | A library to manipulate balanced ternary values. |
homepage | |
repository | https://github.com/Trehinos/balanced-ternary |
max_upload_size | |
id | 1560864 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Balanced Ternary is a Rust library for manipulating
balanced ternary
numbers, a numeral system with digits -1
, 0
, and +1
.
This system is particularly useful in specialized computing applications such as reversible computing, digital signal processing, and three-valued logic modeling.
#![no_std]
environments.+
, 0
, and -
symbols by default, or custom ones.Digit
(Neg
, Zero
or Pos
),Ternary
(heap allocated variable-length balanced-ternary number),Tryte<S>
(S characters long copy-type ternary number).The library supports numerous three-valued logic operations, each of them having its own specificities:
The library provides a variety of operations that can be performed on individual balanced ternary digits. These operations include logical operations, arithmetic operations, and utility functions that are useful for manipulating ternary numbers at the digit level. Below are some examples of how these operations can be used:
fn test_ternary_eq(a: Ternary, b: &str) {
let repr = Ternary::parse(b);
assert_eq!(a.to_string(), repr.to_string());
}
fn test_binary_op(a: &Ternary, f: impl Fn(Digit, Digit) -> Digit, b: &Ternary, c: &str) {
test_ternary_eq(a.each_zip(f, b.clone()), c);
}
fn test_operations() {
use core::ops::{BitAnd, BitOr, BitXor, Mul, Not};
let short = Ternary::parse("-0+");
let long = Ternary::parse("---000+++");
let other = Ternary::parse("-0+-0+-0+");
// K3
test_ternary_eq(short.each(Digit::not), "+0-");
test_binary_op(&long, Digit::bitand, &other, "----00-0+");
test_binary_op(&long, Digit::bitor, &other, "-0+00++++");
test_binary_op(&long, Digit::bitxor, &other, "-0+000+0-");
test_binary_op(&long, Digit::k3_equiv, &other, "+0-000-0+");
test_binary_op(&long, Digit::k3_imply, &other, "+++00+-0+");
// HT
test_ternary_eq(short.each(Digit::ht_not), "+--");
test_binary_op(&long, Digit::ht_imply, &other, "+++-++-0+");
// BI3
test_binary_op(&long, Digit::bi3_and, &other, "-0-000-0+");
test_binary_op(&long, Digit::bi3_or, &other, "-0+000+0+");
test_binary_op(&long, Digit::bi3_imply, &other, "+0+000-0+");
// L3
test_ternary_eq(short.each(Digit::possibly), "-++");
test_ternary_eq(short.each(Digit::necessary), "--+");
test_ternary_eq(short.each(Digit::contingently), "-+-");
test_binary_op(&long, Digit::l3_imply, &other, "+++0++-0+");
// PARA / RM3
test_binary_op(&long, Digit::rm3_imply, &other, "+++-0+--+");
test_binary_op(&long, Digit::para_imply, &other, "+++-0+-0+");
// Other operations
test_ternary_eq(short.each(Digit::post), "0+-");
test_ternary_eq(short.each(Digit::pre), "+-0");
test_ternary_eq(short.each(Digit::absolute_positive), "+0+");
test_ternary_eq(short.each(Digit::positive), "00+");
test_ternary_eq(short.each(Digit::not_negative), "0++");
test_ternary_eq(short.each(Digit::not_positive), "--0");
test_ternary_eq(short.each(Digit::negative), "-00");
test_ternary_eq(short.each(Digit::absolute_negative), "-0-");
test_binary_op(&long, Digit::mul, &other, "+0-000-0+");
}
use balanced_ternary::*;
fn test() {
let ternary = Ternary::from_dec(5);
assert_eq!(ternary.to_string(), "+--");
let ternary = Ternary::parse("+--");
assert_eq!(ternary.to_dec(), 5);
}
use balanced_ternary::*;
fn test() {
let a = Ternary::from_dec(9);
let b = Ternary::from_dec(4);
let sum = &a + &b;
assert_eq!(sum.to_string(), "+++");
assert_eq!(sum.to_dec(), 13);
let bitwise = &Ternary::parse("++00") & &Ternary::parse("0000");
assert_eq!(bitwise.to_string(), "0000");
let bitwise = &Ternary::parse("++00") & &Ternary::parse("0+00");
assert_eq!(bitwise.to_string(), "0+00");
let bitwise = &Ternary::parse("+000") | &Ternary::parse("000-");
assert_eq!(bitwise.to_string(), "+000");
let bitwise = &Ternary::parse("+000") & &Ternary::parse("000-");
assert_eq!(bitwise.to_string(), "000-");
let bitwise = &Ternary::parse("+000") | &Ternary::parse("000+");
assert_eq!(bitwise.to_string(), "+00+");
}
use balanced_ternary::*;
fn test() {
let negative = Ternary::from_dec(-5);
assert_eq!(negative.to_string(), "-++");
}
Add the following to your Cargo.toml
:
[dependencies]
balanced-ternary = "^1"
The complete API documentation can be found on docs.rs. There you can find descriptions and examples of available types and methods.
Copyright (c) 2025 Sébastien GELDREICH
Balanced Ternary
is licensed under the MIT License.