GS Balanced Ternary is a number system in base 3. Instead of using (0,1,2), this system use (-1,0,1) or (-,0,+). These values are called trits (TRInary DigiT).
Numbers are power of 3. For example :
89 = +81 +9 -1 = +3**4 +3**2 -3**0 = +0+0-
-78 = -81 +3 = -3**4 +3**1 = -00+0
Here is a simple clock with balanced ternary display. The first line for hours, the second for minutes and the third for seconds.
# clock-trit.tcl
# Author: Gerard Sookahet
# Date: 15 March 2017
# Description: Balanced ternary clock
# References: https://en.wikipedia.org/wiki/Balanced_ternary
package require Tk
bind all <Escape> exit
font create myDefaultFont -family Consolas -size 32 -weight bold
option add *font myDefaultFont
proc every {ms body} {
eval $body
after $ms [list every $ms $body]
}
proc dec2trit n {
if {$n == 0} {return "0"}
while {$n != 0} {
lappend trit [lindex {0 + -} [expr {$n % 3}]]
set n [expr {$n / 3 + ($n % 3 == 2)}]
}
return [join [lreverse $trit] ""]
}
pack [label .clock -textvar time -bg darkblue -fg orange]
every 1000 {
set ::time [join [lmap x {%H %M %S} {dec2trit [scan [clock format [clock sec] -format $x] %d]}] "\n"]
}
Balanced ternary has interesting properties :
- The sign of a number is given by its most significant nonzero trit
- To negate a number, you swap - and +, leaving 0 alone
- Comparing any two numbers is performed by reading left-to-right and by using lexicographic order