A negative base (or negative radix) may be used to construct a non-standard positional numeral system. Like other place-value systems, each position holds multiples of the appropriate power of the system's base; but that base is negative—that is to say, the base b is equal to −r for some natural number r (r ≥ 2).

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.

The common names for negative-base positional numeral systems are formed by prefixing nega- to the name of the corresponding positive-base system; for example, 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).[1][2]

Example

Consider what is meant by the representation 12243 in the negadecimal system, whose base b is −10:

Multiples of
(−10)4 = 10,000 (−10)3 = −1,000 (−10)2 = 100 (−10)1 = −10 (−10)0 = 1
1 2 2 4 3

The representation 12,243−10 (which is intended to be negadecimal notation) is equivalent to 8,16310 in decimal notation, because 10,000 + (−2,000) + 200 + (−40) + 3 = 8,163.

Remark

On the other hand, −8,16310 in decimal would be written 9,977−10 in negadecimal.

History

Negative numerical bases were first considered by Vittorio Grünwald in an 1885 monograph published in Giornale di Matematiche di Battaglini.[3] Grünwald gave algorithms for performing addition, subtraction, multiplication, division, root extraction, divisibility tests, and radix conversion. Negative bases were later mentioned in passing by A. J. Kempner in 1936[4] and studied in more detail by Zdzisław Pawlak and A. Wakulicz in 1957.[5]

Negabinary was implemented in the early Polish computer BINEG (and UMC), built 1957–59, based on ideas by Z. Pawlak and A. Lazarkiewicz from the Mathematical Institute in Warsaw.[6] Implementations since then have been rare.

Notation and use

Denoting the base as −r, every integer a can be written uniquely as

where each digit dk is an integer from 0 to r − 1 and the leading digit dn is > 0 (unless n = 0). The base −r expansion of a is then given by the string dndn−1...d1d0.

Negative-base systems may thus be compared to signed-digit representations, such as balanced ternary, where the radix is positive but the digits are taken from a partially negative range. (In the table below the digit of value −1 is written as the single character T.)

Some numbers have the same representation in base −r as in base r. For example, the numbers from 100 to 109 have the same representations in decimal and negadecimal. Similarly,

and is represented by 10001 in binary and 10001 in negabinary.

Some numbers with their expansions in a number of positive and corresponding negative bases are:

DecimalNegadecimalBinaryNegabinaryTernaryNegaternaryBalanced TernaryBalanced NegaternaryQuaternaryNegaquaternary
−1525−1111110001−1201220T11011T0−331301
−515−1011111−1221T11TT1−1123
−416−1001100−1122TT1T−1010
−317−111101−1010T010−311
−218−1010−211T111−212
−119−111−112TT−113
0000000000
1111111111
2210110221TTT22
33111111012010T033
441001001112111T110130
55101101121221TT11T11131
6611011010201101T011012132
7711111011211111T111113133
881000110002211210T10T20120
9910011100110010010010021121
1019010101111010110110110122122
1119110111111110210211T1TT23123
121921100111001102201101T030110
131931101111011112211111T131111
141941110100101122221TTTTT1T32112
151951111100111202101TT0TT1033113
1619610000100001212111TT1TT11100100
1719710001100011222121T0TTT0T101101
1819810010101102002001T00TT00102102

Note that, with the exception of nega balanced ternary, the base −r expansions of negative integers have an even number of digits, while the base −r expansions of the non-negative integers have an odd number of digits.

Calculation

The base −r expansion of a number can be found by repeated division by −r, recording the non-negative remainders in , and concatenating those remainders, starting with the last. Note that if a /b is c with remainder d, then bc + d = a and therefore d = abc. To arrive at the correct conversion, the value for c must be chosen such that d is non-negative and minimal. For the fourth line of the following example this means that

has to be chosen — and not nor

For example, to convert 146 in decimal to negaternary:

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.

Reading the remainders forward we can obtain the negaternary least-significant-digit-first representation.

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

Note that in most programming languages, the result (in integer arithmetic) of dividing a negative number by a negative number is rounded towards 0, usually leaving a negative remainder. In such a case we have a = (−r)c + d = (−r)c + dr + r = (−r)(c + 1) + (d + r). Because |d| < r, (d + r) is the positive remainder. Therefore, to get the correct result in such case, computer implementations of the above algorithm should add 1 and r to the quotient and remainder respectively.

Example implementation code

To negabinary

C#
static string ToNegabinary(int value)
{
	string result = string.Empty;

	while (value != 0)
	{
		int remainder = value % -2;
		value = value / -2;

		if (remainder < 0)
		{
			remainder += 2;
			value += 1;
		}

		result = remainder.ToString() + result;
	}

	return result;
}
C++
auto to_negabinary(int value)
{
    std::bitset<sizeof(int) * CHAR_BIT > result;
    std::size_t bit_position = 0;

    while (value != 0)
    {
        const auto div_result = std::div(value, -2);

        if (div_result.rem < 0)
            value = div_result.quot + 1;
        else
            value = div_result.quot;

        result.set(bit_position, div_result.rem != 0);

        ++bit_position;
    }

    return result;
}

To negaternary

C#
static string Negaternary(int value)
{
	string result = string.Empty;

	while (value != 0)
	{
		int remainder = value % -3;
		value = value / -3;

		if (remainder < 0)
		{
			remainder += 3;
			value += 1;
		}

		result = remainder.ToString() + result;
	}

	return result;
}
Visual Basic .NET
Private Shared Function ToNegaternary(value As Integer) As String
	Dim result As String = String.Empty

	While value <> 0
		Dim remainder As Integer = value Mod -3
		value /= -3

		If remainder < 0 Then
			remainder += 3
			value += 1
		End If

		result = remainder.ToString() & result
	End While

	Return result
End Function
Python
def negaternary(i: int) -> str:
    """Decimal to negaternary."""
    if i == 0:
        digits = ["0"]
    else:
        digits = []
        while i != 0:
            i, remainder = divmod(i, -3)
            if remainder < 0:
                i, remainder = i + 1, remainder + 3
            digits.append(str(remainder))
    return "".join(digits[::-1])
>>> negaternary(1000)
'2212001'
Common Lisp
(defun negaternary (i)
  (if (zerop i)
      "0"
      (let ((digits "")
            (rem 0))
        (loop while (not (zerop i)) do
          (progn
            (multiple-value-setq (i rem) (truncate i -3))
            (when (minusp rem)
              (incf i)
              (incf rem 3))
            (setf digits (concatenate 'string (write-to-string rem) digits))))
        digits)))

To any negative base

Java
public String negativeBase(int integer, int base) {
    String result = "";
    int number = integer;
    while (number != 0) {
        int i = number % base;
        number /= base;
        if (i < 0) {
            i += Math.abs(base);
            number++;
        }
        result = i + result;
    }
    return result;
}
AutoLisp

from [-10 -2] interval:

(defun negabase (num baz / dig rst)
  ;; NUM is any number. BAZ is any number in the interval [-10, -2].
  ;;
  ;; NUM and BAZ will be truncated to an integer if they're floats (e.g. 14.25
  ;; will be truncated to 14, -123456789.87 to -123456789, etc.).
  (if (and (numberp num)
           (numberp baz)
           (<= (fix baz) -2)
           (> (fix baz) -11))
      (progn
        (setq baz (float (fix baz))
              num (float (fix num))
              dig (if (= num 0) "0" ""))
        (while (/= num 0)
               (setq rst (- num (* baz (setq num (fix (/ num baz))))))
               (if (minusp rst)
                   (setq num (1+ num)
                         rst (- rst baz)))
               (setq dig (strcat (itoa (fix rst)) dig)))
        dig)
      (progn
        (prompt
         (cond
           ((and (not (numberp num))
                 (not (numberp baz)))
            "\nWrong number and negabase.")
           ((not (numberp num))
            "\nWrong number.")
           ((not (numberp baz))
            "\nWrong negabase.")
           (t
            "\nNegabase must be inside [-10 -2] interval.")))
        (princ))))
PHP

The conversion from integer to some negative base:

function toNegativeBase(int $no, int $base): []
{
    $digits = [];
    $base = intval($base);
    while ($no != 0) {
        $temp_no = $no;
        $no = intval($temp_no / $base);
        $remainder = ($temp_no % $base);

        if ($remainder < 0) {
            $remainder += abs($base);
            $no++;
        }

        array_unshift($digits, $remainder);
    }

    return $digits;
}
Visual Basic .NET
Function toNegativeBase(Number As Integer , base As Integer) As System.Collections.Generic.List(Of Integer)

    Dim digits As New System.Collections.Generic.List(Of Integer)
    while Number <> 0
        Dim remainder As Integer= Number Mod base
        Number = CInt(Number / base)
 
        if remainder < 0 then
            remainder += system.math.abs(base)
            Number+=1
        end if
 
        digits.Insert(0, remainder)
    end while
 
    return digits
end function

Shortcut calculation

The following algorithms assume that

  1. the input is available in bitstrings and coded in (base +2; digits in ) (as in most of today's digital computers),
  2. there are add (+) and xor (^) operations which operate on such bitstrings (as in most of today's digital computers),
  3. the set of output digits is standard, i. e. with base ,
  4. the output is coded in the same bitstring format, but the meaning of the places is another one.

To negabinary

The conversion to negabinary (base −2; digits in ) allows a remarkable shortcut (C implementation):

unsigned int toNegaBinary(unsigned int value) // input in standard binary
{
	unsigned int Schroeppel2 = 0xAAAAAAAA; // = 2/3*((2*2)^16-1) = ...1010
	return (value + Schroeppel2) ^ Schroeppel2; // eXclusive OR
	// resulting unsigned int to be interpreted as string of elements ε {0,1} (bits)
}

Due to D. Librik (Szudzik). The bitwise XOR portion is originally due to Schroeppel (1972).[7]

JavaScript port for the same shortcut calculation:

function toNegaBinary(number) {
    var Schroeppel2 = 0xAAAAAAAA;
    // Convert to NegaBinary String
    return ( ( number + Schroeppel2 ) ^ Schroeppel2 ).toString(2);
}

To negaquaternary

The conversion to negaquaternary (base −4; digits in ) allows a similar shortcut (C implementation):

unsigned int toNegaQuaternary(unsigned int value) // input in standard binary
{
	unsigned int Schroeppel4 = 0xCCCCCCCC; // = 4/5*((2*4)^8-1) = ...11001100 = ...3030
	return (value + Schroeppel4) ^ Schroeppel4; // eXclusive OR
	// resulting unsigned int to be interpreted as string of elements ε {0,1,2,3} (pairs of bits)
}

JavaScript port for the same shortcut calculation:

function toNegaQuaternary(number) {
    var Schroeppel4 = 0xCCCCCCCC;
    // Convert to NegaQuaternary String
    return ( ( number + Schroeppel4 ) ^ Schroeppel4 ).toString(4);
}

Arithmetic operations

The following describes the arithmetic operations for negabinary; calculations in larger bases are similar.

Addition

Adding negabinary numbers proceeds bitwise, starting from the least significant bits; the bits from each addend are summed with the (balanced ternary) carry from the previous bit (0 at the LSB). This sum is then decomposed into an output bit and carry for the next iteration as show in the table:

Sum Output Comment
Bit Carry
−2010−20101−2 −2 occurs only during subtraction.
−1011−21101−2
0000−20000−2
1001−21000−2
2110−20−111−2
3111−21−111−2 3 occurs only during addition.

The second row of this table, for instance, expresses the fact that −1 = 1 + 1 × −2; the fifth row says 2 = 0 + −1 × −2; etc.

As an example, to add 1010101−2 (1 + 4 + 16 + 64 = 85) and 1110100−2 (4 + 16 − 32 + 64 = 52),

Carry:          1 −1  0 −1  1 −1  0  0  0
First addend:         1  0  1  0  1  0  1
Second addend:        1  1  1  0  1  0  0 +
               --------------------------
Number:         1 −1  2  0  3 −1  2  0  1
Bit (result):   1  1  0  0  1  1  0  0  1
Carry:          0  1 −1  0 −1  1 −1  0  0

so the result is 110011001−2 (1 − 8 + 16 − 128 + 256 = 137).

Another method

While adding two negabinary numbers, every time a carry is generated an extra carry should be propagated to next bit. Consider same example as above

Extra carry:    1  1  1  0  1  0  0  0     
Carry:             0  1  1  0  1  0  0  0
First addend:         1  0  1  0  1  0  1
Second addend:        1  1  1  0  1  0  0 +
               --------------------------
Answer:         1  1  0  0  1  1  0  0  1

Negabinary full adder

A full adder circuit can be designed to add numbers in negabinary. The following logic is used to calculate the sum and carries:[8]

Incrementing negabinary numbers

Incrementing a negabinary number can be done by using the following formula:[9]

Subtraction

To subtract, multiply each bit of the second number by −1, and add the numbers, using the same table as above.

As an example, to compute 1101001−2 (1 − 8 − 32 + 64 = 25) minus 1110100−2 (4 + 16 − 32 + 64 = 52),

Carry:          0  1 −1  1  0  0  0
First number:   1  1  0  1  0  0  1
Second number: −1 −1 −1  0 −1  0  0 +
               --------------------
Number:         0  1 −2  2 −1  0  1
Bit (result):   0  1  0  0  1  0  1
Carry:          0  0  1 −1  1  0  0

so the result is 100101−2 (1 + 4 −32 = −27).

Unary negation, x, can be computed as binary subtraction from zero, 0 − x.

Multiplication and division

Shifting to the left multiplies by −2, shifting to the right divides by −2.

To multiply, multiply like normal decimal or binary numbers, but using the negabinary rules for adding the carry, when adding the numbers.

First number:                   1  1  1  0  1  1  0
Second number:                  1  0  1  1  0  1  1 ×
              -------------------------------------
                                1  1  1  0  1  1  0
                             1  1  1  0  1  1  0

                       1  1  1  0  1  1  0
                    1  1  1  0  1  1  0

              1  1  1  0  1  1  0                   +
              -------------------------------------
Carry:        0 −1  0 −1 −1 −1 −1 −1  0 −1  0  0
Number:       1  0  2  1  2  2  2  3  2  0  2  1  0
Bit (result): 1  0  0  1  0  0  0  1  0  0  0  1  0
Carry:           0 −1  0 −1 −1 −1 −1 −1  0 −1  0  0

For each column, add carry to number, and divide the sum by −2, to get the new carry, and the resulting bit as the remainder.

Comparing negabinary numbers

It is possible to compare negabinary numbers by slightly adjusting a normal unsigned binary comparator. When comparing the numbers and , invert each odd positioned bit of both numbers. After this, compare and using a standard unsigned comparator.[10]

Fractional numbers

Base −r representation may of course be carried beyond the radix point, allowing the representation of non-integer numbers.

As with positive-base systems, terminating representations correspond to fractions where the denominator is a power of the base; repeating representations correspond to other rationals, and for the same reason.

Non-unique representations

Unlike positive-base systems, where integers and terminating fractions have non-unique representations (for example, in decimal 0.999... = 1) in negative-base systems the integers have only a single representation. However, there do exist rationals with non-unique representations. For the digits {0, 1, ..., t} with the biggest digit and

we have

    as well as

So every number with a terminating fraction added has two distinct representations.

For example, in negaternary, i.e. and , there is

.

Such non-unique representations can be found by considering the largest and smallest possible representations with integer parts 0 and 1 respectively, and then noting that they are equal. (Indeed, this works with any integer-base system.) The rationals thus non-uniquely expressible are those of form

with

Imaginary base

Just as using a negative base allows the representation of negative numbers without an explicit negative sign, using an imaginary base allows the representation of Gaussian integers. Donald Knuth proposed the quater-imaginary base (base 2i) in 1955.[11]

See also

References

  1. Knuth, Donald (1998), The Art of Computer Programming, Volume 2 (3rd ed.), pp. 204–205. Knuth mentions both negabinary and negadecimal.
  2. The negaternary system is discussed briefly in Petkovšek, Marko (1990), "Ambiguous numbers are dense", The American Mathematical Monthly, 97 (5): 408–411, doi:10.2307/2324393, ISSN 0002-9890, JSTOR 2324393, MR 1048915.
  3. Vittorio Grünwald. Intorno all'aritmetica dei sistemi numerici a base negativa con particolare riguardo al sistema numerico a base negativo-decimale per lo studio delle sue analogie coll'aritmetica ordinaria (decimale), Giornale di Matematiche di Battaglini (1885), 203-221, 367
  4. Kempner, A. J. (1936), "Anormal Systems of Numeration", American Mathematical Monthly, 43 (10): 610–617, doi:10.2307/2300532, JSTOR 2300532, MR 1523792. The only reference to negative bases is a footnote on page 610, which reads, "Positive numbers less than 1 and negative numbers may be used as bases with slight modifications of the process and suitable restrictions on the set of digits employed."
  5. Pawlak, Z.; Wakulicz, A. (1957), "Use of expansions with a negative basis in the arithmometer of a digital computer", Bulletin de l'Académie Polonaise des Sciences, Classe III, 5: 233–236.
  6. Marczynski, R. W., "The First Seven Years of Polish Computing" Archived 2011-07-19 at the Wayback Machine, IEEE Annals of the History of Computing, Vol. 2, No 1, January 1980
  7. See the MathWorld Negabinary link
  8. Francis, Yu; Suganda, Jutamulia; Shizuhuo, Yin (4 September 2001). Introduction to Information Optics. Academic Press. p. 498. ISBN 9780127748115.
  9. "Archived copy". Retrieved 2016-08-29.
  10. Murugesan, San (1977). "Negabinary arithmetic circuits using binary arithmetic". IEE Journal on Electronic Circuits and Systems. 1 (2): 77. doi:10.1049/ij-ecs.1977.0005.
  11. D. Knuth. The Art of Computer Programming. Volume 2, 3rd Edition. Addison-Wesley. pp. 205, "Positional Number Systems"

Further reading

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.