Number systems using negative digits.

Discussion in 'Physics & Math' started by Dinosaur, Sep 2, 2005.

  1. Dinosaur Rational Skeptic Valued Senior Member

    Messages:
    4,885
    I made a post to another thread on this subject. I thought the subject might be of interest to others here who did not read that thread.

    There was a fleeting moment in prehistoric times when it was believed that electronic components with 3 stable states were on the verge of being designed. On a computer using such components, Radix 3 or trinary arithmetic is likely to be better than Radix 2 or binary arithmetic. I worked for a company which considered designing a trinary system.

    There were some disappointed designers when the tri-state components never became a reality. An odd radix makes a balanced number system possible (see below). One wise-a** suggested that a trinary radix could never be used because the digits would be called tits and that was an un-publishable word in prehistoric times.

    The digit weights for balanced radix 3 are powers of three: . . . 27, 9, 3, 1, 1/3, 1/9, 1/27, et cetera, while the digit values are minus one, zero, and plus one (referred to as minus, zero, plus or -, 0, + from here on).

    Notations analogous to the following can be easily devised for any odd radix (or number base), such as 5, 7, 9, et cetera. For example, radix 5 requires 5 digits values corresponding to -2, -1, 0, +1, & +2 There are some interesting properties associated with balanced radix notation, as indicated by the following description of a radix 3 system.

    Using 0, 1, & 2 as digits in a trinary system seems intuitively obvious. It is usable, but not likely be the best method. Simple counting in [/b]balanced radix 3[/b] notation works is as follows. Both positive numbers (on the left: zero through 40) and negative numbers (on the right: zero though minus 40) are shown.
    Code:
    00	0000	0000   
    01	000+	000-
    02	00+-	00-+
    03	00+0	00-0
    04	00++	00--
    05	0+--	0-++
    06	0+-0	0-+0
    07	0+-+	0-+-
    08	0+0-	0-0+
    09	0+00	0-00
    10	0+0+	0-0-
    11	0++-	0--+
    12	0++0	0--0
    13	0+++	0---
    14	+---	-+++
    15	+--0	-++0
    16	+--+	-++-
    17	+-0-	-+0+
    18	+-00	-+00
    19	+-0+	-+0-
    [b]. . . .[/b]
    38	+++-	---+
    39	+++0	---0
    40	++++	----
    I hope there are no typo’s in the above.

    The addition of digits is obvious, although the last two rules below might take some thought.
    • Adding zero is a null operation: The result is the other digit.

    • Plus added to minus or vice versa is zero

    • Plus added to plus is minus with a plus carry.

    • Minus added to minus is plus with a minus carry.
    Study the above values for the table of values provided above. Plus is added to each value on the left to get the next value. For the negative numbers, minus is added to each value on the right (or you can merely reverse the nonzero values on the left). It is easier to see the rules in a 3 X 3 matrix form, but a bit awkward to format them that way.

    Multiplication of digits is more obvious. It follows the ordinary rules for algebraic multiplication among the values plus one, minus one, and zero.

    The notation has various interesting properties.
    • There is no position reserved for the sign. The highest order digit (plus or minus) determines whether the number is positive or negative.

    • There is no special logic for adding a negative number to a positive number. You merely add the digits using the rules for addition of digits.

    • To obtain a complement (for use in subtraction), reverse Plus/Minus digits, and leave zero digits as is. This is simpler that the rule for twos complement in binary: Reverse one/zero digits and then add one, which can result in carry ripple all the way to the high order bit.

    • Carry occurs less often because only 2/9 of the possible digit sums result in carry (1/4 cause it in binary). Furthermore, carry ripples tend to be shorter in balanced trinary than in binary, due to zero resulting from a plus carry into a minus digit or vice versa. In binary, a carry ripples to the next digit position half the time, while it only ripples 1/3 of the time in balanced trinary.

      In a CPU with parallel logic for addition, carry ripple is the slowest part of the operation, because it is a serial operation. I do not know how much it can be speeded up by special circuitry, which started to be employed in the first solid state CPU (about 1959). The lower frequency of carry and the shorter ripples could be expected to result in a faster CPU (or one with simpler circuitry, assuming that special circuitry is employed to speed up carry ripple).

    • Note that signed magnitude notation allows for minus zero as well as plus zero. This is esthetically unpleasing. It also caused an anomaly in certain early computers (Minus zero failed a test for zero due to an oversight by the logical designers). This caused some strange effects for unwary programmers. For example, a major department store often dunned credit card accounts for a balance of zero dollars and zero cents (It looked like a nonzero negative balance, indicating money owed).

    • Note also that complement two notation is unbalanced, allowing a negative value which cannot be complemented, because there is no corresponding positive value.
    There are some anomalies in balanced trinary.
    • Fractional values can have a non-zero digit to the left of the radix point. For example: +.- is equivalent to 2/3 in decimal.

    • Some pairs of never ending fractional values approach the same limit. For example: +. - - - - - - - and 0.++++++++ both approach ½ in the limit. Note that in decimal, the never ending values 0.9999999 and 1.000000 approach the same value, but this is overlooked because the latter is never written that way. There are more examples in both systems, but the decimal examples all involve repeating nines and zeros, while the trinary examples involve repeating pluses and minuses.

    • Multiplication is simpler due to no special sign logic being required, but might take longer. In binary, half the bits in the multiplier require only a shift. In balanced trinary, only 1/3 of the tits require a shift-only operation.

    • Division is more complicated in balanced trinary. The difficulty is related to the two possible never ending equivalents of ½: +.- - - - - - and 0.++++++ and similar situations. For example, if you divide one by two (or 422 by 211, you can choose either 0 or + as the first digit of the quotient and get a correct result. For other dividends and divisors, the correct choice of digits at each stage can be critical, but not obvious.
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Aer Registered Senior Member

    Messages:
    2,250
    What is pi in trinary?
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Pete It's not rocket surgery Registered Senior Member

    Messages:
    10,167
    +0.0++-+++-000-0++-++0+-++++...
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Aer Registered Senior Member

    Messages:
    2,250
    still no pattern..

    Anyway, I still like the trinary system.
     
  8. Dinosaur Rational Skeptic Valued Senior Member

    Messages:
    4,885
    Aer: My occasionally reliable memory tells me that pi is approximately: 3.14159 26535 89793

    I get confused trying to convert directly to balanced trinary notation..

    If I did the arithmetic correctly, TriPi = 10.00102 11012 22201, using ordinary radix 3 notation, which is equivalent to about 7 decimal digits of precision. .

    Converting to balanced notation in stages (Each 2-tit must be converted by increasing the previous tit and replacing the 2-tit with a minus)
    Code:
    	[B]10.00102 11012  22201
    	+0.00++- ++02-  22201
    	+0.00++- +++--  22201
    	+0.00++- +++-0  -2201
    	+0.00++- +++-0  0-201
    	+0.00++- +++0-  00-01[/B]
    In balanced notation, TriPi = +0.00++- +++0- 00-01, assuming that I did not make a mistake some where.
     
  9. Dinosaur Rational Skeptic Valued Senior Member

    Messages:
    4,885
    I made errors in the last thread, I hope I have it right this time.

    My occasionally reliable memory tell me that pi is approximately: 3.14159 26535 89793
    I get confused trying to convert directly to balanced trinary notation..

    If I did the arithmetic correctly, TriPi = 10.01021 10122 22010, using ordinary radix 3 notation, which is equivalent to about 7 decimal digits of precision. .

    Converting to balanced notation in stages (Each 2-tit must be converted by increasing the previous tit and replacing the 2-tit with a minus)
    Code:
    	[B]10.01021  10122  22010
    	+0.0++-+  +02-2  220+0
    	+0.0++-+  ++-0-  220+0
    	+0.0++-+  ++-00  -20+0
    	+0.0++-+  ++-00  0-0+0[/B]
    In balanced notation, TriPi = +0.0++-+ ++-00 0-0+0
     
  10. Aer Registered Senior Member

    Messages:
    2,250
    Thanks for your efforts, I'll try to reverse engineer a script that will do the calculations automatically

    Please Register or Log in to view the hidden image!

    probably won't be able to until tomorrow though.
     
  11. Dinosaur Rational Skeptic Valued Senior Member

    Messages:
    4,885
    Aer: In case you are not familiar with conversion from one radxi to another, I will give you some clues.

    For converting integers, you use the mod operator repeatedly to obtain digits starting with the units digits. To convert fractions, you use multiplication repeatedly to obtain digits starting with the first digit to the right of the radix point. You use the arithmetic of the initial radix. Some examples follow, using A, B, C, D, et cetera for digits beyond 9.
    • Convert decimal 350 to radix 16

      Start with 350 mod 16 = 14, giving unit digit = E

      (350 - 14) / 16 = 21

      21 mod 16 = 5, giving next digit = 5

      (21 - 5) / 16 = 1. Since 1 < 16, last digit = 1

      Decimal 350 = Hexidecimal 15E

    • Convert .867 to radix 12

      .867 * 12 = 10.404, giving first digit = A

      .404 * 12 = 4.848, giving next digit = 4

      .848 * 12 = 10.176, giving next digit = A

      .176 * 12 = 2.112, giving next digit = 2

      Decimal .867 = duodecimal .A4A2 (approximately).

      If multiplication by the radix results in an integer value, the conversion is exact and you stop the process. For example decimal .828 125 = duodecimal .9A3

    • To convert to balanced Radix 3, first convert to ordinary radix 3 (using 0, 1, 2 as digits). Then you must convert 2-tits by adding one to the previous tit and replacing 2 by minus. I sometimes do this conversion in multiple steps to avoid confusion.

      Example: Decimal decimal 139 = ordinary trinary 12011, which is +- - 0++ in balanced notation.

      12011 initially converts to +2 0++
      +2 0++ converts to 2- 0++
      2- 0++ converts to +- - 0++
    Writing a C, C++, or Visual Basic program for ordinary radix conversion is easy. The conversion to balanced notation is a bit trickier.
     
  12. Aer Registered Senior Member

    Messages:
    2,250
    Thanks for the tips. However I went straight from decimal to trinary - I hate complicating things. Anyway, I cannot get the decimal fractions to work

    Please Register or Log in to view the hidden image!

    O well, maybe I'll fix it some other time. Here is the entire code:
    Code:
     t=document.getElementById('t1').value; user='smart';
     if (t.match(/\-/)) { t=t.replace(/\-/,''); c=new Array('0','-','+'); } else { c=new Array('0','+','-'); }
     if (t.match(/\.\d*/)) { t=t.replace(/(\.\d*)/,''); b=RegExp.$1; } else { b=0; }
     if (t.match(/\d+/)) { t=t.replace(/(\d+)/,''); a=RegExp.$1; } else { a=0; }
     if (t.match(/./)) { user='stupid'; document.getElementById('t2').value='funny number'; }
     a-=0; b-=0;
     if (user=='smart')
     {
       s_a=''; s_b=new Array();
      while (a>0)
      {
       r=a%3; s_a=''+c[r]+s_a;
       if (r==2) { a=a-0+1; }
       a=Math.floor(a/3); 
      }
      i=0; r=new Array();
      while (b>0)
      {
       i++;
       r[i]=Math.floor(b*3); s_b[i]=c[r[i]];
       if (r==2) { b=b-0+1; s_b[i-1]=c[r[i-1]+1]; d=2; while (s_b[i-d+1]==c[2]){ s_b[i-d]++; } }
       b=b*3-Math.floor(b*3);
       if (i>10) { break; }
      }
      document.getElementById('t2').value=s_a+' '+s_b;
     }
    
    Enter the number you want in the first textbox, the trinary code will show up in the second texbox. It only works for integers, anything else spits out nonsense for now. Also, you can denote a negative number at any point you want by including a '-' which is more of a reflection of my laziness in coding than it is a "feature" (e.g. -55=5-5=55-)
     
    Last edited: Sep 7, 2005
  13. Aer Registered Senior Member

    Messages:
    2,250
    OK - I was just being stupid before, here is the fully functional version.
    <script type='text/javascript'> function trinary() { t=document.getElementById('t3').value; user='smart'; if (t.match(/\-/)) { t=t.replace(/\-/,''); c=new Array('0','-','+','0'); } else { c=new Array('0','+','-','0'); } if (t.match(/\.\d*/)) { t=t.replace(/(\.\d*)/,''); b=RegExp.$1; } else { b=0; } if (t.match(/\d+/)) { t=t.replace(/(\d+)/,''); a=RegExp.$1; } else { a=0; } if (t.match(/./)) { user='stupid'; document.getElementById('t4').value='funny number'; } a-=0; b-=0; if (user=='smart') { s_a=''; s_b=new Array(); while (a>0) { r=a%3; s_a=''+c[r]+s_a; if (r==2) { a=a-0+1; } a=Math.floor(a/3); } i=-1; r=new Array(); while (b>0) { i++; r=Math.floor(b*3); s_b=c[r]; if (r==2) { s_b[i-1]=c[r[i-1]+1]; d=2; while (s_b[i-d+1]==c[2]){ s_b[i-d]=c[r[i-d]+1]; d++; } } b=b*3-Math.floor(b*3); if (i>20) { break; } } if (s_b.length>0) { point='.'; } else { point=''; } s_b=s_b.join(''); document.getElementById('t4').value=s_a+point+s_b; } } </script>
    <textarea id='t3' rows=1 cols=100 onkeyup='trinary();'></textarea>
    <textarea id='t4' rows=1 cols=100></textarea>
    Just as a general rule, do not try to feed it a 10000 digit number - it crashed my browser

    Please Register or Log in to view the hidden image!

     
  14. Pete It's not rocket surgery Registered Senior Member

    Messages:
    10,167
    It says my IP address is a funny number!
    And who are you calling stupid?!

    Please Register or Log in to view the hidden image!

     
  15. Aer Registered Senior Member

    Messages:
    2,250
    As soon as you interpret your IP address as a decimal digit, you can enter that. As for who is stupid, the computer decides that, not me

    Please Register or Log in to view the hidden image!

     

Share This Page