10 releases
new 0.3.0 | Feb 20, 2025 |
---|---|
0.2.3 | Feb 20, 2025 |
0.1.4 | Feb 19, 2025 |
#169 in Math
316 downloads per month
84KB
897 lines
Balanced Ternary
Balanced Ternary is a Rust library for manipulating
balanced ternary
numbers, a numeral system with digits -1
, 0
, and +1
.
This system is useful in areas like computer science and mathematics due to its symmetry and unique arithmetic properties.
Features
- No Standard Library: Suitable for
#![no_std]
environments. - Number Conversions: Convert between decimal and balanced ternary representations.
- Arithmetic Operations: Support for addition, subtraction, multiplication, and division.
- Three-value Logic Operations:
- Support for bitwise and, or, xor, and not (in Kleene algebra (K3)).
- Advanced logic: Implementation of K3, BI3, L3, RM3, paraconsistent-logic and HT imply operation, and some more HT, BI3, L3 and post-logic operations.
- Custom Representation: Parse and display numbers using
+
,0
, and-
symbols. - Provides the types:
Digit
(Neg
,Zero
orPos
),Ternary
(heap allocated variable-length balanced-ternary number),Tryte
(6 characters long copy-type ternary number).
Digits operations
Unary operations | - | 0 | + |
---|---|---|---|
possibly (L3) | - | + | + |
necessary (L3) | - | - | + |
contingently (L3) | - | + | - |
ht_not (HT) | + | - | - |
post (post logic) eqv. self.pre().pre() |
0 | + | - |
pre (inverse of post) eqv. self.post().post() |
+ | - | 0 |
! (not) / - (neg) (L3) |
+ | 0 | - |
absolute_positive | + | 0 | + |
positive | 0 | 0 | + |
not_negative | 0 | + | + |
not_positive | - | - | 0 |
negative | - | 0 | 0 |
absolute_negative | - | 0 | - |
inc | 0 | + | +- |
dec | -+ |
- | 0 |
Binary operations | - - |
- 0 |
- + |
0 - |
0 0 |
0 + |
+ - |
+ 0 |
+ + |
---|---|---|---|---|---|---|---|---|---|
+ (add) |
-+ |
- | 0 | - | 0 | + | 0 | + | +- |
- (sub) |
0 | - | -+ |
+ | 0 | - | +- |
+ | 0 |
/ (div) |
+ | - | 0 | 0 | - | + | |||
* (mul) |
+ | 0 | - | 0 | 0 | 0 | - | 0 | + |
& (bitand) (L3) |
- | - | - | - | 0 | 0 | - | 0 | + |
bi3_and (BI3) | - | 0 | - | 0 | 0 | 0 | - | 0 | + |
| (bitor) (L3) |
- | 0 | + | 0 | 0 | + | + | + | + |
bi3_or (BI3) | - | 0 | + | 0 | 0 | 0 | + | 0 | + |
^ (bitxor) (L3) |
- | 0 | + | 0 | 0 | 0 | + | 0 | - |
k3_equiv (K3) | + | 0 | - | 0 | 0 | 0 | - | 0 | + |
k3_imply (K3) | + | + | + | 0 | 0 | + | - | 0 | + |
bi3_imply (BI3) | + | 0 | + | 0 | 0 | 0 | - | 0 | + |
l3_imply (L3) | + | + | + | 0 | + | + | - | 0 | + |
rm3_imply (RM3) | + | + | + | - | 0 | + | - | - | + |
para_imply (HT) | + | + | + | - | 0 | + | - | 0 | + |
ht_imply (HT) | + | + | + | - | + | + | - | 0 | + |
Examples
Convert between decimal and balanced ternary
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);
}
Perform arithmetic or logic operations
use balanced_ternary::*;
fn test() {
let a = Ternary::from_dec(9);
let b = Ternary::from_dec(4);
let sum = &a + &b;
assert_eq!(ternary.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+");
}
Handle negative numbers
use balanced_ternary::*;
fn test() {
let negative = Ternary::from_dec(-5);
assert_eq!(negative.to_string(), "-++");
}
Installation
Add the following to your Cargo.toml
:
[dependencies]
balanced-ternary = "0.2.*"
License
Copyright (c) 2025 Sébastien GELDREICH
Balanced Ternary
is licensed under the MIT License.