Fork me

Negative Base Calculation

A negative base is a numeral system where the base (or radix) is a negative integer. Unlike conventional positive bases (like base 10 or base 2), negative bases can represent numbers in a unique way that allows for the representation of both positive and negative integers without needing a separate sign.

For example, in base -2, the digits used are typically 0 and 1, just like in binary. However, the positional values are powers of -2. So, the representation of the number 6 in base -2 would be:

  • 6 = 1 × ( 2 ) 2 + 1 × ( 2 ) 1 + 0 × ( 2 ) 0 6 = 1 \times (-2)^2 + 1 \times (-2)^1 + 0 \times (-2)^0

This can be calculated as:

  • 1 × 4 + 1 × ( 2 ) + 0 × 1 = 4 2 + 0 = 2 1 \times 4 + 1 \times (-2) + 0 \times 1 = 4 - 2 + 0 = 2

The key point about negative bases is that they allow for an interesting way to express numbers without needing additional symbols to represent negative values, which can be quite useful in certain computational applications and theoretical mathematics.

Negative-base systems can accommodate all the same numbers as standard place-value systems, but both positive and negative numbers are represented without the use of a minus sign (or, in computer representation, a sign bit); this advantage is countered by an increased complexity of arithmetic operations. The need to store the information normally contained by a negative sign often results in a negative-base number being one digit longer than its positive-base equivalent.

Negative-base positional numeral systems are formed by prefixing nega- keyword to the name of the corresponding positive-base system; such as negadecimal (base −10) corresponds to decimal (base 10), negabinary (base −2) to binary (base 2), negaternary (base −3) to ternary (base 3), and negaquaternary (base −4) to quaternary (base 4).

Calculator


Converter
-
-
Addition
(+)
-

Calculation

To find the base r expansion of a number, repeatedly divide by r while recording the non-negative remainders in the set { 0 , 1 , , r 1 } \{0, 1, \ldots, r-1\} . After obtaining all remainders, concatenate them starting from the last one. Remember that if a / b a / b equals c c with a remainder d d , then b c + d = a bc + d = a , which implies d = a b c d = a - bc . To ensure a proper conversion, the value for c c should be chosen so that d d remains non-negative and as small as possible. This consideration applies to the fourth line in the following example.

5 ÷ ( 3 ) = 2   r e m a i n d e r   1 {\displaystyle -5\div (-3)=2~\mathrm {remainder} ~1}

has to be chosen — and not = 3   r e m a i n d e r   4 {\displaystyle =3~\mathrm {remainder} ~4} nor = 1   r e m a i n d e r   2. {\displaystyle =1~\mathrm {remainder} ~-\!2.}

For example, to convert 146 in decimal to negaternary:

146 ÷ ( 3 ) = 48   r e m a i n d e r   2 48 ÷ ( 3 ) = 16   r e m a i n d e r   0 16 ÷ ( 3 ) = 5   r e m a i n d e r   1 5 ÷ ( 3 ) = 2   r e m a i n d e r   1 2 ÷ ( 3 ) = 0   r e m a i n d e r   2 {\displaystyle {\begin{array}{rr}146\div (-3)=&-48~\mathrm {remainder} ~2\\-48\div (-3)=&16~\mathrm {remainder} ~0\\16\div (-3)=&-5~\mathrm {remainder} ~1\\-5\div (-3)=&2~\mathrm {remainder} ~1\\2\div (-3)=&0~\mathrm {remainder} ~2\end{array}}}

Reading the remainders backward we obtain the negaternary representation of 14610 : 21102–3.

Proof: -3 · (-3 · (-3 · (-3 · ( 2 ) + 1 ) + 1 ) + 0 ) + 2 = (((2 · (–3) + 1) · (–3) + 1) · (–3) + 0) · (–3) + 2= 14610.

Addition

Negative base addition like regular binary (positive base) numbers, except the carry is negative and should be subtracted from each column instead of added to it.

For that last , note that translates to 1 with a positive (black) carry of (as in regular numbers is in negabinary).

The two isolated negative 1's each yield a result of 1 in their respective columns, along with a positive carry of 1. In the column with three positive 1's, you get a result of 1 and a negative carry to the next column. This process should cover all possible scenarios that can arise when adding two numbers together.

References

Implementation Code (Javascript)

Credits