Representation of Numbers in the Computer
Questions to David Rotermund
By Jan Wiersig, modified by Udo Ernst and translated into English by Daniel Harnack. David Rotermund replaced the Matlab code with Python code.
Bits and Bytes
In digital computers, information is represented by discrete states of circuit elements. The smallest possible unit of information is one bit (binary digit). It can be in two different states, that may be described differently:
true | false | |
yes | no | |
1 | 0 | |
A bit is easily realized physically:
current flowing | current not flowing | |
voltage there | voltage gone | |
magnetized | not magnetized | |
For example, in the communication between integrated circuits, information is traded via changes in voltage, while the bits on the hard disk are written by magnetization of ferromagnetic materials. Between electronic musical instruments, bits are exchanged through current loops (MIDI-Standard). In the stone age of computers, the fifties, bits were even once realized as air bubbles in a liquid medium.
By combining several bits, complex information like numbers, letters (e.g. 8 bit-ASCII-code), images, color, music, etc, can be represented. Much like in the decimal system, where whole numbers are made up of the digits 0-9, the same numbers can be written in the so called dual system by sequences of 0 and 1:
dual system | decimal system |
---|---|
000 | 0 |
001 | 1 |
010 | 2 |
011 | 3 |
100 | 4 |
101 | 5 |
110 | 6 |
111 | 7 |
Two further examples for 8-bit numbers:
01010101
10101010
To represent negative numbers, a small trick is necessary: one specific bit codes for the sign of the number. In an
dual system | decimal system |
---|---|
01111111 | +127 |
01111110 | +126 |
00000010 | +2 |
00000001 | +1 |
00000000 | +0 |
11111111 | -1 |
11111110 | -2 |
10000010 | -126 |
10000001 | -127 |
10000000 | -128 |
Certain bit lengths have names:
1 Byte | 8 Bit |
1 Word | 16 Bit |
1 Kilobyte | 1024 Byte |
1 Megabyte | 1024 Kilobyte |
1 Gigabyte | 1024 Megabyte |
1 Terabyte | 1024 Gigabyte |
Representation of Real Numbers and Numerical Errors
Real numbers are, by their nature, analogue quantities. Hence we would expect the handling of these numbers on digital computers not to be completely problem-free. Present digital computers usually represent real numbers as floating-point numbers.
Thereby, the precision, with which the real number can be represented, is determined by the number of available bits.”Simple precision” (i.e. float32) requires 4 Bytes, for double precision (i.e. float64) 8~Bytes are needed. The latter is the default configuration in Python and Matlab. The IEEE format of double precision uses 53-Bits for the mantissa, 11-Bits for the exponent and for the basis the remaining 2. One Bit of the mantissa respectively the exponent are used for the sign of the quantity. Thus, the exponent can vary between
Range Error
The maximal range of the floating-point numbers is determined by the number of bits used to code for the exponent. A typical number for single precision is
and for double precision
Via application of arithmetic operations on these numbers, the range can be exceeded. The error occurring in that case is named a range error. As an example we consider the Bohr radius in SI units
The quantity
An even bigger problem can be illustrated by the calculation of the factorial. The factorial is defined as
In Python or Matlab, it can be easily verified by using the function factorial(n), that the factorial for
For bigger
The factorial
To get the mantissa and the exponent, we form the logarithm to the basis 10 (reminder:
We now associate the integer part of
From these examples we learn that range errors can usually be circumvented with a little creativity.
Rounding Error
Rounding errors stem from the finite precision of the mantissa. The following program illustrates this fact:
x: float = 1.0
while 1 + x != 1:
x = x / 2
x = x * 2
print(x) # -> 2.220446049250313e-16
One might think that this constitutes an infinite loop. To the contrary, the loop will be left in finite time. The result for double precision is
The source code is Open Source and can be found on GitHub.