Wyższa Szkoła Zarządzania i Bankowości
LECTURESNOTEBOOKSPACKAGES
4 Number Representation and Error Propagation
4.1 Number Representation

1 Introduction to Simulation and Modeling
2 Discrete Medeling (L-Systems)
3 Population Dynamics
4 Number Representation and Error Propagation
4.1 Number Representation
4.2 Error Propagation
4.3 Conditioning
5 Modeling with Random Numbers
6 Heat Transfer in a Rod (Connection Mathematica and C: MathLink)
7 Special Topics in Stochastic Finance
8 Appendix: Introduction to Mathematica
9 Population Dynamics in Vensim®PLE
     
 

IV.1 Number Representation

Introduction

In everyday life referring to a number means referring to a decimal number, built up from units (digits) in the range from 0 up to 9. Most computers do not have a decimal look on numbers and rather use a different base for number representation. In particular, the binary number representation with base 2, where units are in the range from 0 up to 1, is widely used in computers.

The problem is that numbers, which can be represented exactly in one number system, are in general not exactly representable in another system. A binary computer cannot represent most decimal numbers exactly. To illustrate this, consider the following example. In a decimal system the output will be 1.0, 0.9, ..., 0.1. But what does a computer make of it? Try it! (Note: make sure you know how to stop an evaluation in Mathematica: use 'Alt ,' or 'Alt .')

[Graphics:Images/nb4_gr_2.gif]
[Graphics:Images/nb4_gr_3.gif]
[Graphics:Images/nb4_gr_4.gif]
[Graphics:Images/nb4_gr_5.gif]
[Graphics:Images/nb4_gr_6.gif]
[Graphics:Images/nb4_gr_7.gif]
[Graphics:Images/nb4_gr_8.gif]
[Graphics:Images/nb4_gr_9.gif]
[Graphics:Images/nb4_gr_10.gif]
[Graphics:Images/nb4_gr_11.gif]
[Graphics:Images/nb4_gr_12.gif]
[Graphics:Images/nb4_gr_13.gif]
[Graphics:Images/nb4_gr_14.gif]
[Graphics:Images/nb4_gr_15.gif]
[Graphics:Images/nb4_gr_16.gif]

The problem in this example is that 0.1, and therefore all intermediate results, could not be represented exactly and the stopping criterion was never satisfied.

The example above can be rewritten such that the stopping criterion can be satisfied. Verify that the code below works as expected.

[Graphics:Images/nb4_gr_17.gif]
[Graphics:Images/nb4_gr_18.gif]
[Graphics:Images/nb4_gr_19.gif]
[Graphics:Images/nb4_gr_20.gif]
[Graphics:Images/nb4_gr_21.gif]
[Graphics:Images/nb4_gr_22.gif]
[Graphics:Images/nb4_gr_23.gif]
[Graphics:Images/nb4_gr_24.gif]
[Graphics:Images/nb4_gr_25.gif]
[Graphics:Images/nb4_gr_26.gif]
[Graphics:Images/nb4_gr_27.gif]

As human beings and computers have a different number representation, conversion is required when going from one system to the other.  Results from a calculation by a computer need to be converted from binary or octal representation (nice for a computer) to a decimal representation (nice for a human being). For example

[Graphics:Images/nb4_gr_28.gif] and [Graphics:Images/nb4_gr_29.gif].

On the other hand, if one wants to provide input to a computer program, the input first has to be conversed from decimal to the number system of the computer before the data can be processed. A binary computer will make the following conversion when the decimal number 5.2 is fed into it (decimal numbers in magenta)

[Graphics:Images/nb4_gr_30.gif]

The erroneous expansion of decimal numbers can have a great impact. In the Gulf war of 1991 an Iraqi Scud slipped through the Patriot defence shield and hit an army camp in Dhahran due to an incorrect expansion of the number 0.1!

REQUIRED
IV.1a
The Patriot missile defence system kept time in unites of 1/10th of a second. When the fatal Scud that hit Dhahran, was detected, the Patriot clock had been running for 100 hours. How many bits in base 2 are required to represent 100 hours in units of 1/10th of a second?

REQUIRED
IV.1b
How many bits are required to represent 1-[Graphics:Images/nb4_gr_31.gif] exactly in binary? How many decimal places are required to represent the same number exactly in base 10?

Positional System

The way in which numbers are represented, both in normal life and in a computer, is by combining a sequence of units or digits with the positions at which these units occur in the number. For example, the decimal number 792 means seven hundreds (or ten times ten), nine tens and two units, or [Graphics:Images/nb4_gr_32.gif]. This positional system was invented 4000 years ago by the Babylonian who did not use a decimal system as we do, but a hexadecimal with base 60.

The positional system is certainly not the only system. Take for example the Romans who used a completely other way for dealing with numbers. Their system had no such a thing as a base 2 or 10. Instead they used a fixed number of symbols I, V, X, L, C, D and M, for 1, 5, 10, 50, 100, 500 and 1000 respectively and numbers were formed by a sequence of symbols in decreasing order of magnitude. (Note: when too many instances of a symbol would be needed, the order would change such that lower value symbols would precede higher value symbols.) So, the number 68 would have been written by the Romans as LXVIII.

The positional system is determined by the choice for the base or radix [Graphics:Images/nb4_gr_33.gif], a positive integer number, and the 'digits' [Graphics:Images/nb4_gr_34.gif]. Besides the well-known decimal system with base 10 the following systems are familiar in computer environments

Binary:        [Graphics:Images/nb4_gr_35.gif]:        [Graphics:Images/nb4_gr_36.gif]
Octal:        [Graphics:Images/nb4_gr_37.gif]:        [Graphics:Images/nb4_gr_38.gif]
Hexadecimal:    [Graphics:Images/nb4_gr_39.gif]:    [Graphics:Images/nb4_gr_40.gif]

Integer Number Representation

An arbitrary integer number [Graphics:Images/nb4_gr_41.gif] will have the following form in a positional system with base [Graphics:Images/nb4_gr_42.gif]

[Graphics:Images/nb4_gr_43.gif],

where [Graphics:Images/nb4_gr_44.gif] is the sign that can be [Graphics:Images/nb4_gr_45.gif], and each of the [Graphics:Images/nb4_gr_46.gif] digits is in the range from 0 to [Graphics:Images/nb4_gr_47.gif]. If we define [Graphics:Images/nb4_gr_48.gif],  the value of [Graphics:Images/nb4_gr_49.gif] is given by

[Graphics:Images/nb4_gr_50.gif].

The decimal number [Graphics:Images/nb4_gr_51.gif] has according to this notation the value:

[Graphics:Images/nb4_gr_52.gif].

Fractional Number Representation

The above-described representation for integer numbers can be modified to include fractional numbers. An arbitrary fractional number [Graphics:Images/nb4_gr_53.gif] will have the following form in a positional system with base [Graphics:Images/nb4_gr_54.gif]

[Graphics:Images/nb4_gr_55.gif],

where the sign [Graphics:Images/nb4_gr_56.gif] [Graphics:Images/nb4_gr_57.gif] is followed by a point, and each of the [Graphics:Images/nb4_gr_58.gif] digits is in the range from 0 to [Graphics:Images/nb4_gr_59.gif]. If we define [Graphics:Images/nb4_gr_60.gif],  the value of [Graphics:Images/nb4_gr_61.gif] is given by

[Graphics:Images/nb4_gr_62.gif].

In this notation the decimal number [Graphics:Images/nb4_gr_63.gif] has the value [Graphics:Images/nb4_gr_64.gif]. Similarly, the binary number [Graphics:Images/nb4_gr_65.gif] has the value [Graphics:Images/nb4_gr_66.gif]

Notice the use of the dot notation that is common to the Anglo-Saxon world, whereas the rest of the world uses a comma to denote fractional numbers.

Floating Point Number Representation

Fractional number representation becomes awkward as soon as the numbers become very large or small. The floating point number representation introduces an exponent part that is adequate especially when dealing with large or small numbers. The general form for the floating point representation of a number [Graphics:Images/nb4_gr_67.gif] is

[Graphics:Images/nb4_gr_68.gif],

where [Graphics:Images/nb4_gr_69.gif] is the by now well-known sign, [Graphics:Images/nb4_gr_70.gif] is the mantissa with [Graphics:Images/nb4_gr_71.gif], and [Graphics:Images/nb4_gr_72.gif] is the exponent which can be a positive or negative integer number. In the special case where [Graphics:Images/nb4_gr_73.gif] the mantissa is zero, but the value of the exponent is undefined and is system dependent. In the floating point notation the decimal number -0.000001234 would be written as [Graphics:Images/nb4_gr_74.gif].

Finite Number Representation

A computer does not have an infinite length for the mantissa or exponent. Instead it uses a fixed length for both the mantissa and the exponent. This implies that the numbers that can be represented by a computer are bounded by a maximum (and of course a minimum). The largest possible value for the exponent is denoted by [Graphics:Images/nb4_gr_75.gif] and the smallest (negative) value for the exponent is denoted by [Graphics:Images/nb4_gr_76.gif]. All numbers represented by a computer thus lie in a fixed range, mainly depending on [Graphics:Images/nb4_gr_77.gif] and [Graphics:Images/nb4_gr_78.gif].

The fixed length of the mantissa imposes a restriction on the accuracy with which numbers can be represented.

By selecting a combination of a particular [Graphics:Images/nb4_gr_79.gif], mantissa length [Graphics:Images/nb4_gr_80.gif], the minimum value for the exponent [Graphics:Images/nb4_gr_81.gif] and the maximum value for the exponent [Graphics:Images/nb4_gr_82.gif], the numbers that can be represented are completely determined. A finite floating point number system is dependent on this combination and is denoted as [Graphics:Images/nb4_gr_83.gif].

To enforce unambiguous representation of floating point numbers we use normalised floating point numbers, where the mantissa starts with a non-zero digit for floating point numbers not equal to zero.

As an example all normalised numbers in the floating point system [Graphics:Images/nb4_gr_84.gif] are listed, and also the total of the numbers that can be represented in this system.

[Graphics:Images/nb4_gr_85.gif] [Graphics:Images/nb4_gr_86.gif] [Graphics:Images/nb4_gr_87.gif] [Graphics:Images/nb4_gr_88.gif]
[Graphics:Images/nb4_gr_89.gif] [Graphics:Images/nb4_gr_90.gif] [Graphics:Images/nb4_gr_91.gif] [Graphics:Images/nb4_gr_92.gif]
[Graphics:Images/nb4_gr_93.gif] [Graphics:Images/nb4_gr_94.gif] [Graphics:Images/nb4_gr_95.gif] [Graphics:Images/nb4_gr_96.gif]
[Graphics:Images/nb4_gr_97.gif] [Graphics:Images/nb4_gr_98.gif] [Graphics:Images/nb4_gr_99.gif] [Graphics:Images/nb4_gr_100.gif]
[Graphics:Images/nb4_gr_101.gif] [Graphics:Images/nb4_gr_102.gif] [Graphics:Images/nb4_gr_103.gif] [Graphics:Images/nb4_gr_104.gif]
[Graphics:Images/nb4_gr_105.gif] [Graphics:Images/nb4_gr_106.gif] [Graphics:Images/nb4_gr_107.gif] [Graphics:Images/nb4_gr_108.gif]
0
[Graphics:Images/nb4_gr_109.gif] [Graphics:Images/nb4_gr_110.gif] [Graphics:Images/nb4_gr_111.gif] [Graphics:Images/nb4_gr_112.gif]
[Graphics:Images/nb4_gr_113.gif] [Graphics:Images/nb4_gr_114.gif] [Graphics:Images/nb4_gr_115.gif] [Graphics:Images/nb4_gr_116.gif]


Each row contains 900 numbers. Looking at the positive numbers there are five rows with a positive exponent, four rows with a negative exponent and one row with exponent zero. This gives ten rows for the positive numbers.  This is also true for the negative numbers, giving a total of twenty rows with 900 elements each. Finally there is a row containing only the number zero. The total numbers to be represented in this system is therefore [Graphics:Images/nb4_gr_117.gif]. The largest number that can be represented in this system is [Graphics:Images/nb4_gr_118.gif] and the smallest number is [Graphics:Images/nb4_gr_119.gif]. The smallest positive number is given by [Graphics:Images/nb4_gr_120.gif].

The largest number that can be represented in a floating point number system [Graphics:Images/nb4_gr_121.gif] is called maxreal or overflow threshold or giant and its value is given by [Graphics:Images/nb4_gr_122.gif].

The smallest positive number that can be represented in a floating point number system [Graphics:Images/nb4_gr_123.gif] is called minreal or underflow threshold or dwarf and its value is given by [Graphics:Images/nb4_gr_124.gif].

All representable numbers in a system [Graphics:Images/nb4_gr_125.gif] are called the machine numbers. For any machine number [Graphics:Images/nb4_gr_126.gif] other than the giant, there exists a smallest larger machine number [Graphics:Images/nb4_gr_127.gif], called the upper neighbour. Also, for any machine number [Graphics:Images/nb4_gr_128.gif] other than the dwarf, there exists a largest smaller machine number [Graphics:Images/nb4_gr_129.gif], called the lower neighbour.

The relative dispersion is defined for any non-zero machine number other than the giant as

[Graphics:Images/nb4_gr_130.gif].

For [Graphics:Images/nb4_gr_131.gif], i.e. with mantissa [Graphics:Images/nb4_gr_132.gif] and exponent [Graphics:Images/nb4_gr_133.gif], the relative dispersion can be expressed as

ρ := [Graphics:Images/nb4_gr_134.gif],

with [Graphics:Images/nb4_gr_135.gif].

From this an interval for the relative dispersion can be derived:

[Graphics:Images/nb4_gr_136.gif]

The maximum value for the relative dispersion is called the machine precision or the system resolution and is usually denoted with η. It can be shown that the following equality holds

[Graphics:Images/nb4_gr_137.gif].

If we take the same floating point system as in the previous example the relative dispersion is given by [Graphics:Images/nb4_gr_138.gif][Graphics:Images/nb4_gr_139.gif], with [Graphics:Images/nb4_gr_140.gif].
The machine precision (or equivalent: maximal dispersion) in this system is given by  [Graphics:Images/nb4_gr_141.gif].

A real number [Graphics:Images/nb4_gr_142.gif] in the range of the machine numbers, i.e. [Graphics:Images/nb4_gr_143.gif] or [Graphics:Images/nb4_gr_144.gif], will be approximated in a computer by a machine number [Graphics:Images/nb4_gr_145.gif]. This machine number [Graphics:Images/nb4_gr_146.gif] will be chosen such that in a way it approximates [Graphics:Images/nb4_gr_147.gif] best among all machine numbers. The notation for this is [Graphics:Images/nb4_gr_148.gif]. In a system using rounding [Graphics:Images/nb4_gr_149.gif] will be the machine number closest to [Graphics:Images/nb4_gr_150.gif] (and closer to zero in case [Graphics:Images/nb4_gr_151.gif] lies exactly between two machine numbers). For a system with truncation [Graphics:Images/nb4_gr_152.gif] will be the largest machine number less or equal to [Graphics:Images/nb4_gr_153.gif] for positive numbers, while for negative numbers the equivalence [Graphics:Images/nb4_gr_154.gif] is used.

Proposition
For real numbers in the range of the machine numbers the following holds:
[Graphics:Images/nb4_gr_155.gif] with [Graphics:Images/nb4_gr_156.gif]
[Graphics:Images/nb4_gr_157.gif]

The unknown ε is called the representation error.

For example, take the system [Graphics:Images/nb4_gr_158.gif](2, 3, -1, 2). The machine precision in this system is given by [Graphics:Images/nb4_gr_159.gif]. The representation error is then given by [Graphics:Images/nb4_gr_160.gif] in case of rounding and [Graphics:Images/nb4_gr_161.gif] in case of truncation.

[Graphics:Images/nb4_gr_162.gif]

mantissa [Graphics:Images/nb4_gr_163.gif] [Graphics:Images/nb4_gr_164.gif] [Graphics:Images/nb4_gr_165.gif] [Graphics:Images/nb4_gr_166.gif]
exponent
[Graphics:Images/nb4_gr_167.gif] [Graphics:Images/nb4_gr_168.gif] [Graphics:Images/nb4_gr_169.gif] [Graphics:Images/nb4_gr_170.gif] [Graphics:Images/nb4_gr_171.gif]
0 [Graphics:Images/nb4_gr_172.gif] [Graphics:Images/nb4_gr_173.gif] [Graphics:Images/nb4_gr_174.gif] [Graphics:Images/nb4_gr_175.gif]
1 [Graphics:Images/nb4_gr_176.gif] [Graphics:Images/nb4_gr_177.gif] [Graphics:Images/nb4_gr_178.gif] [Graphics:Images/nb4_gr_179.gif]
2 [Graphics:Images/nb4_gr_180.gif] [Graphics:Images/nb4_gr_181.gif] [Graphics:Images/nb4_gr_182.gif] [Graphics:Images/nb4_gr_183.gif]

REQUIRED
IV.1c
What other numbers can be represented if we drop the restriction of a normalised mantissa?

The numbers in the floating point system can also be drawn graphically. In the picture below the numbers are depicted on a one-dimensional axis.

[Graphics:Images/nb4_gr_184.gif]

REQUIRED
IV.1d
a) Use the function Table[] to  form a list  of all the positive  normalized  machine numbers on a three-didgit, base 4 computer, that has exponents in the range -2 to 2, inlcuding 0 and sign bits.
b) How many numbers are there in the list?
c) What are the smallest  and largest numbers on the list?
d) ListPLot the Log of the Sorted, Flattened list of numbers. Why do the points tend to lie in a straight line?
e) Why is the line scalloped? (hightly  irregular)
f) Would  the scallops be more or less pronounced in base 16?
(You might want to use the function  BaseForm[])

[Graphics:Images/nb4_gr_185.gif]

Machine Arithmetic and Errors

The arithmetic operators +, -, × and / all have a machine implementation denoted by [Graphics:Images/nb4_gr_186.gif], [Graphics:Images/nb4_gr_187.gif], [Graphics:Images/nb4_gr_188.gif] and [Graphics:Images/nb4_gr_189.gif] respectively. For any two machine numbers [Graphics:Images/nb4_gr_190.gif] the relation between these operators and their machine counterparts is given by

[Graphics:Images/nb4_gr_191.gif] = [Graphics:Images/nb4_gr_192.gif] [Graphics:Images/nb4_gr_193.gif]
[Graphics:Images/nb4_gr_194.gif] = [Graphics:Images/nb4_gr_195.gif] [Graphics:Images/nb4_gr_196.gif]
[Graphics:Images/nb4_gr_197.gif] = [Graphics:Images/nb4_gr_198.gif] [Graphics:Images/nb4_gr_199.gif]
[Graphics:Images/nb4_gr_200.gif] = [Graphics:Images/nb4_gr_201.gif] [Graphics:Images/nb4_gr_202.gif]

More generally:

[Graphics:Images/nb4_gr_203.gif].

Note: instead of the ^ notation to emphasize the finite precision of the calculation there is another notation for this purpose: fl (). Thus [Graphics:Images/nb4_gr_204.gif] is equivalent to fl(u + v).

For example consider the floating point system [Graphics:Images/nb4_gr_205.gif], where the minimum and maximum values for the exponent may be anything, since these are not relevant in this example. The machine precision for this system is given by [Graphics:Images/nb4_gr_206.gif]. Further, take [Graphics:Images/nb4_gr_207.gif]. What is [Graphics:Images/nb4_gr_208.gif]?

In exact arithmetic:
[Graphics:Images/nb4_gr_209.gif]
For the floating point system with a four digits mantissa this gives [Graphics:Images/nb4_gr_210.gif]. The absolute error due to round-off in this case is [Graphics:Images/nb4_gr_211.gif], whereas the relative error is [Graphics:Images/nb4_gr_212.gif].

Definition
The absolute error for a number is the absolute value of the exact value minus the approximated value.

The relative error for a number different from zero is the absolute error divided by the absolute value of the exact value.

The big question now is what the 'computer result' will be of an operation [Graphics:Images/nb4_gr_213.gif] , where [Graphics:Images/nb4_gr_214.gif] and [Graphics:Images/nb4_gr_215.gif] are real numbers. In general the numbers [Graphics:Images/nb4_gr_216.gif] and [Graphics:Images/nb4_gr_217.gif] will not be machine numbers, so there will be an error in representing these numbers. Moreover, as we have seen above, also the operations will introduce errors due to limited mantissa length. It appears that a distinction has to be made between the operators [Graphics:Images/nb4_gr_218.gif] on the one hand, and + and - on the other hand when we want to compute [Graphics:Images/nb4_gr_219.gif].

Absolute error for [Graphics:Images/nb4_gr_220.gif] and /

In the following the error in the computation with the [Graphics:Images/nb4_gr_221.gif] operator is discussed. By replacing the [Graphics:Images/nb4_gr_222.gif] operator by the / operator the error in the division operation is derived. If the relative errors in representing [Graphics:Images/nb4_gr_223.gif] and [Graphics:Images/nb4_gr_224.gif] are given by [Graphics:Images/nb4_gr_225.gif] and [Graphics:Images/nb4_gr_226.gif] respectively, the machine numbers [Graphics:Images/nb4_gr_227.gif] and [Graphics:Images/nb4_gr_228.gif] are given by [Graphics:Images/nb4_gr_229.gif] and [Graphics:Images/nb4_gr_230.gif]. For [Graphics:Images/nb4_gr_231.gif] we then find

[Graphics:Images/nb4_gr_232.gif],

for some [Graphics:Images/nb4_gr_233.gif] in the order of the machine precision. From this it follows that

[Graphics:Images/nb4_gr_234.gif]

In this derivation we have assumed (1) [Graphics:Images/nb4_gr_235.gif] and ignored (2) the higher order terms in [Graphics:Images/nb4_gr_236.gif] (see Wikinson in:IMA Bull. 22 (11/12) p.192-200, 1986). The relative error for the multiplication easily follows from the result

[Graphics:Images/nb4_gr_237.gif].

Absolute error for [Graphics:Images/nb4_gr_238.gif] and -

The error bounds for the + and - operators are less severe than for the × and / operators. Like in the previous subsection we will only discuss the error bound for one of the operators, the + operator. The error bound for the subtraction can be derived in a similar way. Again, let the relative errors in representing [Graphics:Images/nb4_gr_239.gif] and [Graphics:Images/nb4_gr_240.gif] be given by [Graphics:Images/nb4_gr_241.gif] and [Graphics:Images/nb4_gr_242.gif] respectively. The machine numbers [Graphics:Images/nb4_gr_243.gif] and [Graphics:Images/nb4_gr_244.gif] are given by [Graphics:Images/nb4_gr_245.gif] and [Graphics:Images/nb4_gr_246.gif]. For [Graphics:Images/nb4_gr_247.gif] we then find



[Graphics:Images/nb4_gr_248.gif],

for some [Graphics:Images/nb4_gr_249.gif] in the order of the machine precision. From this it follows that

[Graphics:Images/nb4_gr_250.gif]

In this derivation it has been assumed (1) [Graphics:Images/nb4_gr_251.gif] and ignored (2) the higher order terms in [Graphics:Images/nb4_gr_252.gif]. The relative error for the addition easily follows from the result

[Graphics:Images/nb4_gr_253.gif].

The above implies that for addition and subtraction the absolute error in the result is proportional to the sum of the absolute values of the operands. Adding two operands with the same sign or subtracting operands with opposite sign also gives a small relative error compared to the exact result. But it can be seen immediately that when adding two operands with opposite sign or subtracting operands with equal sign the relative error may explode due to cancellation of numbers.

ADVANCED
IV.1e
Take the floating point system [Graphics:Images/nb4_gr_254.gif]. The machine precision is [Graphics:Images/nb4_gr_255.gif]. If [Graphics:Images/nb4_gr_256.gif] and [Graphics:Images/nb4_gr_257.gif] what is [Graphics:Images/nb4_gr_258.gif]? What is the absolute error? What is the relative error? Use machine implementation of  "-" with arguments having the same exponent.


 
     
  Lectures | Notebooks | Packages

 
  Copyright © 2003 Wyższa Szkoła Zarządzania i Bankowości. Wszystkie prawa zastrzeżone
webmaster@wszib.edu.pl