Chapter 2: Computer Memory and Storage, Representing Numbers, Random Numbers
Chapter 2: Computer Memory and Storage, Representing Numbers, Random Numbers
(B) Book
say 40 lines × 80 characters per page,
1 page = 3200 bytes ≈ 3 K bytes.
300 pages ≈ 1 M byte.
(C) Music
To detect a frequency of 20KHz you need 40,000 valves of pressure per second. If each valve is given by 16
bits (CD quality) this amounts to
80 K Bytes / second
10 M Bytes / minute
650 M Bytes / 65 minute CD...about the capacity of a single disc.
(D) Pictures
Each dot on a computer image is called a PIXEL.
A good screen might have 1080 × 780 ≈ 1 M PIXELS.
Each pixel has colour and brightness specified by (about) 3 bytes.
Therefore a single image requires 3 Mb.
(A chemical photograph contains approximately 30-40 Mb of information.)
2
Representation of Numbers
11010101 = −1 × 27 + 1 × 26 + 1 × 24 + 1 × 22 + 1 × 20 = −43
i.e. the left-most bit represents −27 then 26 , 25 ,...., 20 . This allows the range [−128, 127] to be stored.
How are these numbers added and subtracted?
Addition
Addition proceeds just like ordinary decimal addition
Example
1001 1101 − 99
+ 0001 0100 + 20
1011 0001 − 79
Subtraction
For subtraction we exploit the following theorem and then use addition.
Theorem
Suppose f (n) is a function that flips all the bits in n (e.g. f (1001 0111) = 0110 1000) then
−n = f (n) + 1
Proof
Therefore a calculation
a=b−c
can be rewritten as
a = b + (−c) = b + f (c) + 1
This uses only addition and bit flipping, both of which are fast operations. Note, however, that in two’s
complement arithmetic we have the unusual results
In the case of 32-bit (standard) floating point numbers, the mantissa is usually 23 bits long, and the exponent
is an 8-bit (two’s complement) integer in the range [−128, 127].
Representing the reals in this way has several consequences:
Rational Fractions
In base 2, just like base 10, many rational fractions have a repeating pattern after the decimal point.
Example
Store 1/7 (in base 10) as a decimal in binary.
1
7 = 2−3 (1 + 71 )
= 2−3 + 2−6 (1 + 71 ) ! substituting for 1/7 from above
= (0.001)2 ! in binary
Example
Store 1/10 (in base 10) as a decimal in binary.
1 16
10 = 2−4 ( 10 )
6
= 2 (1 + 10
−4
)
1
= 2 + 2 + 2−4 . 10
−4 −5
1
= 2 + 2 + 2 + 2−9 + 2−8 . 10
−4 −5 −8
= (0.00011)2
In binary, then, both 1/7 and 1/10 are repeating fractions! Since the reals are stored with a finite mantissa
(typically 23 bits) even 1/10 will only be approximately stored.
To improve precision we can use reals with more bits, e.g. Salford allows 64-bit reals, which can be called
with the declaration
integer, parameter::long=selected real kind(p=12)
real(kind=long)::x ! makes x a 64 bit real
Ordinary reals have 23 digits past the decimal point in binary ≈ 7 digits past the decimal point in base 10.
p=12 in the expression above asks for AT LEAST 12 digits in base 10 (in fact you get 16 for 64 bit reals).
4
1 + x = 1.00000000000000000000001 × 20
The final 1500 terms in this series are all small < 10−7 so if we add the summation FORWARDS (starting)
from the first term, they will not contribute to the final answer as there is a 1+x =1 round-off error for
every term. However, taken together, the final 1500 terms add to give ≈ 1.36 × 10 −4 , a significant error!
Solution One (not entirely foolproof) way to get around this is to add the terms in the summation BACK-
WARDS, i.e. from the smallest term first. This will minimise the accumulated round-off error.
Quadratic Equations
Consider a quadratic equation
ax2 + bx + c = 0.
and assume that b > 0 for what follows (although the argument is easily modified). The roots of this
equation are √ √
−b − b2 − 4ac −b + b2 − 4ac
x1 = , and x2 =
2a 2a
2
√ 2
2
Supposing we have b 4ac. Then b − 4ac ≈ b(1 − 2ac/b ) and
b c
x1 ≈ − , and x2 = −
a b
Note that |x1 | |x2 | since x1 /x2 = b2 /ac 1.
A problem with round-off error may arise if 4ac < 10−7 b2 . Then the computer will calculate b2 − 4ac = b2
and will then calculate x2 = 0!!!!
Solution A robust quadratic solver proceeds as follows. First calculate
√
−b − b2 − 4ac
x1 =
2a
5
Random Numbers
A computer is an entirely deterministic device, i.e. it does not have access to any genuinely random process.
‘Random’ numbers must therefore be generated from a deterministic sequence - ideally one which ‘appears’
to be random to the casual observer (although of course is not really). ‘Random’ numbers generated in this
fashion are adequate for most pratical purposes.
Example: m = 7, a = 2, c = 3
4. This generates a sequence of integers between 0 and m − 1. Dividing by m gives a sequence of reals
between 0 and 1 − 1/m.
or alternatively add
print *,’enter number < 1000
read *,nran
do i=1,nran
call random number(x)
end do !returns last value of x