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
- Number Conversions: Convert between decimal and balanced ternary representations.
- Arithmetic Operations: Support for addition, subtraction, multiplication, and division.
- Logic Operations: Support for bitwise and, or, xor, and not.
- Advanced logic: Implementation of K3, BI3, L3, RM3 and HT imply operation.
- Custom Representation: Parse and display numbers using
+,0, and-symbols. - No Standard Library: Suitable for
#![no_std]environments. - Provides the types:
Digit(Neg,ZeroorPos),Ternary(heap allocated variable-length balanced-ternary number),Tryte(6 characters long copy-type ternary number).
Digits operators
These operators can be applied for Ternary with Ternary::each(operator):
| Unary operators | ➖ | 0️⃣ | ➕ |
|---|---|---|---|
| possibly | ➖ | ➕ | ➕ |
| necessary | ➖ | ➖ | ➕ |
| contingently | ➖ | ➕ | ➖ |
| ht_not | ➕ | ➖ | ➖ |
| post | 0️⃣ | ➕ | ➖ |
| pre | ➕️ | ➖ | 0️⃣ |
! (not) / - (neg) |
➕ | 0️⃣ | ➖ |
| absolute_positive | ➕ | 0️⃣ | ➕ |
| positive | 0️⃣ | 0️⃣ | ➕ |
| not_negative | 0️⃣ | ➕ | ➕ |
| not_positive | ➖ | ➖ | 0️⃣ |
| negative | ➖ | 0️⃣ | 0️⃣ |
| absolute_negative | ➖ | 0️⃣ | ➖ |
These operators can be applied for Ternary with:
Ternary::each_with(operator, with), or,Ternary::each_zip(operator, other):
| Binary operators | ➖➖ | ➖0️⃣ | ➖➕ | 0️⃣➖ | 0️⃣0️⃣ | 0️⃣➕ | ➕➖ | ➕0️⃣ | ➕➕ |
|---|---|---|---|---|---|---|---|---|---|
+ (add) |
➖➕ | ➖ | 0️⃣ | ➖ | 0️⃣ | ➕ | 0️⃣ | ➕ | ➕➖ |
- (sub) |
0️⃣ | ➖ | ➖➕ | ➕ | 0️⃣ | ➖ | ➕➖ | ➕ | 0️⃣ |
/ (div) |
➕ | 🚫 | ➖ | 0️⃣ | 🚫 | 0️⃣ | ➖ | 🚫 | ➕ |
* (mul) |
➕ | 0️⃣ | ➖ | 0️⃣ | 0️⃣ | 0️⃣ | ➖ | 0️⃣ | ➕ |
& (bitand) |
➖ | ➖ | ➖ | ➖ | 0️⃣ | 0️⃣ | ➖ | 0️⃣ | ➕ |
| bi3_and | ➖ | 0️⃣ | ➖ | 0️⃣ | 0️⃣ | 0️⃣ | ➖ | 0️⃣ | ➕ |
| (bitor) |
➖ | 0️⃣ | ➕ | 0️⃣ | 0️⃣ | ➕ | ➕ | ➕ | ➕ |
| bi3_or | ➖ | 0️⃣ | ➕ | 0️⃣ | 0️⃣ | 0️⃣ | ➕ | 0️⃣ | ➕ |
^ (bitxor) |
➖ | 0️⃣ | ➕ | 0️⃣ | 0️⃣ | 0️⃣ | ➕ | 0️⃣ | ➖ |
| k3_equiv | ➕ | 0️⃣ | ➖ | 0️⃣ | 0️⃣ | 0️⃣ | ➖ | 0️⃣ | ➕ |
| k3_imply | ➕ | ➕ | ➕ | 0️⃣ | 0️⃣ | ➕ | ➖ | 0️⃣ | ➕ |
| bi3_imply | ➕ | 0️⃣ | ➕ | 0️⃣ | 0️⃣ | 0️⃣ | ➖ | 0️⃣ | ➕ |
| l3_imply | ➕ | ➕ | ➕ | 0️⃣ | ➕ | ➕ | ➖ | 0️⃣ | ➕ |
| rm3_imply | ➕ | ➕ | ➕ | ➖ | 0️⃣ | ➕ | ➖ | ➖ | ➕ |
| ht_imply | ➕ | ➕ | ➕ | ➖ | ➕ | ➕ | ➖ | 0️⃣ | ➕ |
Examples
Convert between decimal and balanced ternary
use *;
Perform arithmetic or logic operations
use *;
Handle negative numbers
use *;
Installation
Add the following to your Cargo.toml:
[]
= "0.1.*"
License
Copyright (c) 2025 Sébastien GELDREICH
Balanced Ternary is licensed under the MIT License.