This script converts a number between base 10, base 3 and base ±3.

Methods

Convert base 10 to base 3
The remainder of division by 3 gives the last digit, then repeat with quotient
example : 19:3=6 remains 1, 6:3=2 remains 0, 2:3=0 remains 2, hence 19=201_{3}

Convert base 3 to base 10
Starting from 1st digit, multiply by three and add the next digit
Example : 2101_{3} 2*3+1=7 then 7*3+0=21 then 21*3+1=64

Convert base 3 to base ±3
Starting from last digit. 0 and 1 give 0 et +1, 2 give -1 adding 1 to next digit to the left.
If this were =2, it becomes 0 with carry propagation.
Example 1122 : 2 gives -1, carry 1. 2+1=3 gives 0, carry 1.1+1=2 gives -1, carry 1.
1+1=2 gives -1 carry 1.
(0)+1=1 gives +1 and finally +--0-

Convert base ±3 to base 3
Same method, but carry should be substracted (borrow). -1 gives 2 borrow 1, 0 gives 0, +1 gives 1 and -2 gives 1 borrow 1.
Example +--0- : - gives 2, borrow 1. 0-1=-1 gives 2 borrow 1. -1-1=-2 gives 1 borrow 1.
-1-1=-2 gives 1 borrow 1.
+1-1=0 gives 0 and finally (0)1122_{3} This process ends, as the first ternary digit is always + for positive numbers.

Convert base ±3 to base 10
Same method as for base 3 to base 10, with weighting +1, -1 and 0 instead of 0, 1 and 2
Example : +-0+ = +3^{3}-3^{2}+0*3^{1}+3^{0}=27-9+0+1=19
or 1*3-1=2, 2*3+0=6, 6*3+1=19