Balanced ternary is a ternary numeral system (i.e. base 3 with three digits) that uses a balanced signed-digit representation of the integers in which the digits have the values −1, 0, and 1. This stands in contrast to the standard (unbalanced) ternary system, in which digits have values 0, 1 and 2. The balanced ternary system can represent all integers without using a separate minus sign; the value of the leading non-zero digit of a number has the sign of the number itself. The balanced ternary system is an example of a non-standard positional numeral system. It was used in some early computers and has also been used to solve balance puzzles. Different sources use different glyphs to represent the three digits in balanced ternary. In this article, T (which resembles a ligature of the minus sign and 1) represents −1, while 0 and 1 represent themselves. Other conventions include using '−' and '+' to represent −1 and 1 respectively, or using Greek letter theta (Θ), which resembles a minus sign in a circle, to represent −1. In publications about the Setun computer, −1 is represented as overturned 1: "1". Balanced ternary makes an early appearance in Michael Stifel's book Arithmetica Integra (1544). It also occurs in the works of Johannes Kepler and Léon Lalanne. Related signed-digit schemes in other bases have been discussed by John Colson, John Leslie, Augustin-Louis Cauchy, and possibly even the ancient Indian Vedas.
Definition
Let D 3 := { T , 0 , 1 } {\displaystyle {\mathcal {D}}_{3}:=\lbrace \operatorname {T} ,0,1\rbrace } denote the set of symbols (also called glyphs or characters), where the symbol 1 ¯ {\displaystyle {\bar {1}}} is sometimes used in place of T . {\displaystyle \operatorname {T} .} Define an integer-valued function f = f D 3 : D 3 → Z {\displaystyle f=f_{{\mathcal {D}}_{3}}:{\mathcal {D}}_{3}\to \mathbb {Z} } by
f
( T ) = − 1 , f
( 0 ) = 0 , f
( 1 ) = 1 , {\displaystyle {\begin{aligned}f_{}(\operatorname {T} )&=-1,\\f_{}(0)&=0,\\f_{}(1)&=1,\end{aligned}}}
where the right hand sides are integers with their usual values. This function, f
, {\displaystyle f_{},} is what rigorously and formally establishes how integer values are assigned to the symbols/glyphs in D 3 . {\displaystyle {\mathcal {D}}_{3}.} One benefit of this formalism is that the definition of "the integers" (however they may be defined) is not conflated with any particular system for writing/representing them; in this way, these two distinct (albeit closely related) concepts are kept separate. The set D 3 {\displaystyle {\mathcal {D}}_{3}} together with the function f
{\displaystyle f_{}} forms a balanced signed-digit representation called the balanced ternary system. It can be used to represent integers and real numbers.
Ternary integer evaluation Let D 3 + {\displaystyle {\mathcal {D}}_{3}^{+}} be the Kleene plus of D 3 {\displaystyle {\mathcal {D}}_{3}} , which is the set of all finite length concatenated strings d n … d 0 {\displaystyle d_{n}\ldots d_{0}} of one or more symbols (called its digits) where n {\displaystyle n} is a non-negative integer and all n + 1 {\displaystyle n+1} digits d n , … , d 0 {\displaystyle d_{n},\ldots ,d_{0}} are taken from D 3 = { T , 0 , 1 } . {\displaystyle {\mathcal {D}}_{3}=\lbrace \operatorname {T} ,0,1\rbrace .} The start of d n … d 0 {\displaystyle d_{n}\ldots d_{0}} is the symbol d 0 {\displaystyle d_{0}} (at the right), its end is d n {\displaystyle d_{n}} (at the left), and its length is n + 1 {\displaystyle n+1} . The ternary evaluation is the function v = v 3 : D 3 + → Z {\displaystyle v=v_{3}~:~{\mathcal {D}}_{3}^{+}\to \mathbb {Z} } defined by assigning to every string d n … d 0 ∈ D 3 + {\displaystyle d_{n}\ldots d_{0}\in {\mathcal {D}}_{3}^{+}} the integer
v ( d n … d 0 ) = ∑ i = 0 n f
( d i ) 3 i . {\displaystyle v\left(d_{n}\ldots d_{0}\right)~=~\sum _{i=0}^{n}f_{}\left(d_{i}\right)3^{i}.}
The string d n … d 0 {\displaystyle d_{n}\ldots d_{0}} represents (with respect to v {\displaystyle v} ) the integer v ( d n … d 0 ) . {\displaystyle v\left(d_{n}\ldots d_{0}\right).} The value v ( d n … d 0 ) {\displaystyle v\left(d_{n}\ldots d_{0}\right)} may alternatively be denoted by d n … d 0 bal 3 . {\displaystyle {d_{n}\ldots d_{0}}_{\operatorname {bal} 3}.} The map v : D 3 + → Z {\displaystyle v:{\mathcal {D}}_{3}^{+}\to \mathbb {Z} } is surjective but not injective since, for example, 0 = v ( 0 ) = v ( 00 ) = v ( 000 ) = ⋯ . {\displaystyle 0=v(0)=v(00)=v(000)=\cdots .} However, every nonzero integer has exactly one representation under v {\displaystyle v} that does not end (on the left) with the symbol 0 , {\displaystyle 0,} i.e. d n = 0. {\displaystyle d_{n}=0.}
If d n … d 0 ∈ D 3 + {\displaystyle d_{n}\ldots d_{0}\in {\mathcal {D}}_{3}^{+}} and n > 0 {\displaystyle n>0} then v {\displaystyle v} satisfies:
v ( d n d n − 1 … d 0 ) = f
( d n ) 3 n + v ( d n − 1 … d 0 ) {\displaystyle v\left(d_{n}d_{n-1}\ldots d_{0}\right)~=~f_{}\left(d_{n}\right)3^{n}+v\left(d_{n-1}\ldots d_{0}\right)}
which shows that v {\displaystyle v} satisfies a sort of recurrence relation. This recurrence relation has the initial condition
v ( ε ) = 0 {\displaystyle v\left(\varepsilon \right)=0} where ε {\displaystyle \varepsilon } is the empty string. This implies that for every string d n … d 0 ∈ D 3 + , {\displaystyle d_{n}\ldots d_{0}\in {\mathcal {D}}_{3}^{+},}
v ( 0 d n … d 0 ) = v ( d n … d 0 ) {\displaystyle v\left(0d_{n}\ldots d_{0}\right)=v\left(d_{n}\ldots d_{0}\right)}
which in words says that leading 0 {\displaystyle 0} symbols (to the left in a string with 2 or more symbols) do not affect the resulting value. The following examples illustrate how some values of v {\displaystyle v} can be computed, where (as before) all integer are written in decimal (base 10) and all elements of D 3 + {\displaystyle {\mathcal {D}}_{3}^{+}} are just symbols.
v ( T T ) = f
( T ) 3 1 + f
( T ) 3 0 = ( − 1 ) 3 + ( − 1 ) 1 = − 4 v ( T 1 ) = f
( T ) 3 1 + f
( 1 ) 3 0 = ( − 1 ) 3 + ( 1 ) 1 = − 2 v ( 1 T ) = f
( 1 ) 3 1 + f
( T ) 3 0 = ( 1 ) 3 + ( − 1 ) 1 = 2 v ( 11 ) = f
( 1 ) 3 1 + f
( 1 ) 3 0 = ( 1 ) 3 + ( 1 ) 1 = 4 v ( 1 T 0 ) = f
( 1 ) 3 2 + f
( T ) 3 1 + f
( 0 ) 3 0 = ( 1 ) 9 + ( − 1 ) 3 + ( 0 ) 1 = 6 v ( 10 T ) = f
( 1 ) 3 2 + f
( 0 ) 3 1 + f
( T ) 3 0 = ( 1 ) 9 + ( 0 ) 3 + ( − 1 ) 1 = 8 {\displaystyle {\begin{alignedat}{10}v\left(\operatorname {T} \operatorname {T} \right)&=&&f_{}\left(\operatorname {T} \right)3^{1}+&&f_{}\left(\operatorname {T} \right)3^{0}&&=&&(-1)&&3&&\,+\,&&(-1)&&1&&=-4\\v\left(\operatorname {T} 1\right)&=&&f_{}\left(\operatorname {T} \right)3^{1}+&&f_{}\left(1\right)3^{0}&&=&&(-1)&&3&&\,+\,&&(1)&&1&&=-2\\v\left(1\operatorname {T} \right)&=&&f_{}\left(1\right)3^{1}+&&f_{}\left(\operatorname {T} \right)3^{0}&&=&&(1)&&3&&\,+\,&&(-1)&&1&&=2\\v\left(11\right)&=&&f_{}\left(1\right)3^{1}+&&f_{}\left(1\right)3^{0}&&=&&(1)&&3&&\,+\,&&(1)&&1&&=4\\v\left(1\operatorname {T} 0\right)&=f_{}\left(1\right)3^{2}+&&f_{}\left(\operatorname {T} \right)3^{1}+&&f_{}\left(0\right)3^{0}&&=(1)9\,+\,&&(-1)&&3&&\,+\,&&(0)&&1&&=6\\v\left(10\operatorname {T} \right)&=f_{}\left(1\right)3^{2}+&&f_{}\left(0\right)3^{1}+&&f_{}\left(\operatorname {T} \right)3^{0}&&=(1)9\,+\,&&(0)&&3&&\,+\,&&(-1)&&1&&=8\\\end{alignedat}}}
and using the above recurrence relation
v ( 101 T ) = f
( 1 ) 3 3 + v ( 01 T ) = ( 1 ) 27 + v ( 1 T ) = 27 + 2 = 29. {\displaystyle v\left(101\operatorname {T} \right)=f_{}\left(1\right)3^{3}+v\left(01\operatorname {T} \right)=(1)27+v\left(1\operatorname {T} \right)=27+2=29.}
Conversions to/from other representations
Conversion to decimal In the balanced ternary system the value of a digit n places left of the radix point is the product of the digit and 3n. This is useful when converting between decimal and balanced ternary. In the following the strings denoting balanced ternary carry the suffix, bal3. For instance,
10bal3 = 1 × 31 + 0 × 30 = 3dec 10𝖳bal3 = 1 × 32 + 0 × 31 + (−1) × 30 = 8dec −9dec = −1 × 32 + 0 × 31 + 0 × 30 = 𝖳00bal3 8dec = 1 × 32 + 0 × 31 + (−1) × 30 = 10𝖳bal3 Similarly, the first place to the right of the radix point holds 3−1 = 1/3, the second place holds 3−2 = 1/9, and so on. For instance,
−2/3dec = −1 + 1/3 = −1 × 30 + 1 × 3−1 = 𝖳.1bal3.
An integer is divisible by three if and only if the digit in the units place is zero. The parity of a balanced ternary integer may be checked by checking the parity of the sum of all trits. This sum has the same parity as the integer itself. Balanced ternary can also be extended to fractional numbers similar to how decimal numbers are written to the right of the radix point.
In decimal or binary, integer values and terminating fractions have multiple representations. For example, 1/10 = 0.1 = 0.10 = 0.09. And, 1/2 = 0.12 = 0.102 = 0.012. Some balanced ternary fractions have multiple representations too. For example, 1/6 = 0.1𝖳bal3 = 0.01bal3. In decimal and binary, the rightmost trailing infinite 0s after the radix point may be omitted to gain a representations of integer or terminating fraction; but in balanced ternary, the rightmost trailing infinite −1s after the radix point cannot be omitted in order to gain a representations of integer or terminating fraction. Donald Knuth has pointed out that truncation and rounding are the same operation in balanced ternary—they produce exactly the same result (a property shared with other balanced numeral systems). The number 1/2 is not exceptional; it has two equally valid representations, and two equally valid truncations: 0.1 (round to 0, and truncate to 0) and 1.𝖳 (round to 1, and truncate to 1). With an odd radix, double rounding is also equivalent to directly rounding to the final precision, unlike with an even radix. The basic operations—addition, subtraction, multiplication, and division—are done as in regular ternary. Multiplication by two can be done by adding a number to itself, or subtracting itself after a-trit-left-shifting. An arithmetic shift left of a balanced ternary number is the equivalent of multiplication by a (positive, integral) power of 3; and an arithmetic shift right of a balanced ternary number is the equivalent of division by a (positive, integral) power of 3.
Conversion to and from a fraction
The conversion of a repeating balanced ternary number to a fraction is analogous to converting a repeating decimal. For example (because of 111111bal3 = (36 − 1/3 − 1)dec):
0.1 110 T T 0 ¯ = 1110 T T 0 − 1 111111 × 1 T × 10 = 1110 T T T 111111 × 1 T 0 = 111 × 1000 T 111 × 1001 × 1 T 0 = 1111 × 1 T 1001 × 1 T 0 = 1111 10010 = 1 T 1 T 1 T T T 0 = 101 1 T 10 {\displaystyle 0.1{\overline {\mathrm {110TT0} }}={\tfrac {\mathrm {1110TT0-1} }{\mathrm {111111\times 1T\times 10} }}={\tfrac {\mathrm {1110TTT} }{\mathrm {111111\times 1T0} }}={\tfrac {\mathrm {111\times 1000T} }{\mathrm {111\times 1001\times 1T0} }}={\tfrac {\mathrm {1111\times 1T} }{\mathrm {1001\times 1T0} }}={\tfrac {1111}{10010}}={\tfrac {\mathrm {1T1T} }{\mathrm {1TTT0} }}={\tfrac {101}{\mathrm {1T10} }}}
Conversion from unbalanced ternary Unbalanced ternary can be converted to balanced ternary notation in two ways:
Add 1 trit-by-trit from the first non-zero trit with carry, and then subtract 1 trit-by-trit from the same trit without borrow. For example, 0213 + 113 = 1023, 1023 − 113 = 1T1bal3 = 7dec. If a 2 is present in ternary, turn it into 1T. For example, 02123 = 0010bal3 + 1T00bal3 + 001Tbal3 = 10TTbal3 = 23dec
If the three values of ternary logic are false, unknown and true, and these are mapped to balanced ternary as T, 0 and 1 and to conventional unsigned ternary values as 0, 1 and 2, then balanced ternary can be viewed as a biased number system analogous to the offset binary system. If the ternary number has n trits, then the bias b is
b = ⌊ 3 n 2 ⌋ {\displaystyle b=\left\lfloor {\frac {3^{n}}{2}}\right\rfloor }
which is represented as all ones in either conventional or biased form. As a result, if these two representations are used for balanced and unsigned ternary numbers, an unsigned n-trit positive ternary value can be converted to balanced form by adding the bias b and a positive balanced number can be converted to unsigned form by subtracting the bias b. Furthermore, if x and y are balanced numbers, their balanced sum is x + y − b when computed using conventional unsigned ternary arithmetic. Similarly, if x and y are conventional unsigned ternary numbers, their sum is x + y + b when computed using balanced ternary arithmetic.
Conversion from any integer base to balanced ternary Conversion to balanced ternary can be done with the following formula:
( a n a n − 1 ⋯ a 1 a 0 . c 1 c 2 c 3 ⋯ ) b = ∑ k = 0 n a k b k + ∑ k = 1 ∞ c k b − k . {\displaystyle \left(a_{n}a_{n-1}\cdots a_{1}a_{0}.c_{1}c_{2}c_{3}\cdots \right)_{b}=\sum _{k=0}^{n}a_{k}b^{k}+\sum _{k=1}^{\infty }c_{k}b^{-k}.}
where,
anan−1...a1a0.c1c2c3... is the original representation in the original numeral system. b is the original radix. b is 10 if converting from decimal. ak and ck are the digits k places to the left and right of the radix point respectively. For instance,
−25.4dec = −(1T×1011 + 1TT×1010 + 11×101−1) = −(1T×1011 + 1TT×1010 + 11×101T)
= −(1T×101 + 1TT + 11×0.010T)
= −(1T1T + 1TT + 0.11TT)
= −10T1.11TT
= T01T.TT11
1010.12 = 1T10 + 1T1 + 1T−1 = 1T10 + 1T1 + 1TT
= 10T + 1T + 0.1
= 101.1
Addition, subtraction and multiplication and division The single-trit addition, subtraction, multiplication and division tables are shown below. For subtraction and division, which are not commutative, the first operand is given to the left of the table, while the second is given at the top. For instance, the answer to 1 − T = 1T is found in the bottom left corner of the subtraction table.
Multi-trit addition and subtraction Multi-trit addition and subtraction is analogous to that of binary and decimal. Add and subtract trit by trit, and add the carry appropriately. For example:
1TT1TT.1TT1 1TT1TT.1TT1 1TT1TT.1TT1 1TT1TT.1TT1 + 11T1.T − 11T1.T − 11T1.T → + TT1T.1 ______________ ______________ _______________ 1T0T10.0TT1 1T1001.TTT1 1T1001.TTT1 + 1T + T T1 + T T1 ______________ ________________ ________________ 1T1110.0TT1 1110TT.TTT1 1110TT.TTT1 + T + T 1 + T 1 ______________ ________________ ________________ 1T0110.0TT1 1100T.TTT1 1100T.TTT1
Multi-trit multiplication Multi-trit multiplication is analogous to that of binary and decimal.
1TT1.TT × T11T.1 _____________ 1TT.1TT multiply 1 T11T.11 multiply T 1TT1T.T multiply 1 1TT1TT multiply 1 T11T11 multiply T _____________ 0T0000T.10T
Multi-trit division Balanced ternary division is analogous to that of binary and decimal. However, 0.5dec = 0.1111...bal3 or 1.TTTT...bal3. If the dividend is over the plus or minus half divisor, the trit of the quotient must be 1 or T. If the dividend is between the plus and minus of half the divisor, the trit of the quotient is 0. The magnitude of the dividend must be compared with that of half the di
