Machine Numbers and Errors
Machine Numbers and Errors
Dr. Aysun TEZEL ÖZTURAN
Machine Numbers and Errors
Table of Contents
Floating-Point Representation
Errors in Representing Numbers
Machine Numbers and Errors
Floating-Point Representation
Floating-point number system
Computers usually do not use base-10 arithmetic for storage or
computation. Numbers that have a finite expression in one number
system may have an infinite expression in another system. This
phenomenon is illustrated when the familiar decimal number 1/10
is converted into the binary system:
(0.1)10 = (0.000110011001100110011001100110011...)2
Machine Numbers and Errors
Floating-Point Representation
A standard form, often called normalized scientific notation, is
obtained by shifting the decimal point and supplying appropriate
powers of 10
37.21829 = 0.3721829 → 102
0.002271828 = 0.2271828 → 10→2
3000527.11059 = 0.300052711059 → 107
In normalized scientific notation, the number is represented by a
fraction multiplied by 10n , and the leading digit in the fraction is
not zero (except when the number involved is zero). Thus, we
write 79325 as 0.79325 → 105 , not 0.079325 → 106 or 7.9325 → 104
or some other way.
Machine Numbers and Errors
Floating-Point Representation
Normalized Floating-Point Representation
In the context of computer science, normalized scientific notation
is also called normalized floating-point representation. In the
decimal system, any real number x (other than zero) can be
represented in normalized floating-point form as
x = ±0.d1 d2 d3 ... → 10n
where d1 ↑= 0 and n is an integer (positive, negative, or zero). The
numbers d1 , d2 , ... are the decimal digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and
9.
Machine Numbers and Errors
Floating-Point Representation
Stated another way, the real number x, if di!erent from zero, can
be represented in normalized floating-point decimal form as
x = ±r → 10n (1/10 ↓ r < 1)
This representation consists of three parts: a sign that is either +
or ↔, a number r in the interval [1/10, 1), and an integer power of
10. The number r is called the normalized mantissa and n the
exponent.
Machine Numbers and Errors
Floating-Point Representation
The floating-point representation in the binary system is similar
to that in the decimal system in several ways. If x ↑= 0, it can be
written as
x = ±q → 2m (1/2 ↓ q < 1)
Every computer has only a finite word length and a finite total
capacity, so only numbers with a finite number of digits can be
represented. Clearly, irrational numbers, for example
↗
7, ω ↘ 3.1415 · · · , e ↘ 2.711828 · · · , etc.
cannot be represented, nor can those rational numbers that do not
fit the finite format imposed by the computer. Furthermore,
numbers may be either too large or too small to be representable.
The real numbers that are representable in a computer are called
its machine numbers.
Machine Numbers and Errors
Floating-Point Representation
Since any number used in calculations with a computer system
must conform to the format of numbers in that system, it must
have a finite expansion. Numbers that have a nonterminating
expansion cannot be accommodated precisely. Moreover, a number
that has a terminating expansion in one base may have a
nonterminating expansion in another. A good example of this is
the following simple fraction as given in the beginning
1
= (0.1)10 = (0.06314631463146314...)8
10
or
(0.1)10 = (0.000110011001100110011001100110011...)2
The important point here is that most real numbers cannot
be represented exactly in a computer.
Machine Numbers and Errors
Floating-Point Representation
If, in the course of a computation, a number x is produced of the
form ±q → 2m , where m is outside the computers permissible
range, then we say that an overflow or an underflow has occurred
or that x is outside the range of the computer.
Generally, an overflow results in a fatal error (or exception), and
the normal execution of the program stops. An underflow, however,
is usually treated automatically by setting x to zero without any
interruption of the program but with a warning message in most
computers.
Machine Numbers and Errors
Floating-Point Representation
Many binary computers have a word length of 32 bits (binary
digits).
By single-precision floating-point numbers, we mean all acceptable
numbers in a computer using the standard single-precision
floating-point arithmetic format. This set is a finite subset of the
real numbers. It consists of ±0, ±≃, normal and subnormal
single-precision floating-point numbers, but not Not a Number
(NaN) values.
Machine Numbers and Errors
Floating-Point Representation
Recall that most real numbers cannot be represented exactly as
floating-point numbers, since they have infinite decimal or binary
expansions (all irrational numbers and some rational numbers); for
example,
1 1
ω, e, ,
3 10
and so on Because of the 32-bit word-length, as much as possible
of the normalized floating-point number
±q → 2m
must be contained in those 32 bits. One way of allocating the 32
bits is as follows:
sign of q 1 bit
integer |m| 8 bits
number q 23 bits
Machine Numbers and Errors
Floating-Point Representation
Single-Precision Floating-Point Form
In such a scheme, we can represent real numbers with |m| as large
as 27 ↔ 1 = 127. The exponent represents numbers from ↔127
through 128.
We now describe a machine number of the following form in
standard single-precision floating-point representation:
(↔1)s → 2c→127 → (1.f )2
The leftmost bit is used for the sign of the mantissa, where s = 0
corresponds to + and s = 1 corresponds to ↔. The next eight bits
are used to represent the number c in the exponent
x xxxxxxxx xxxxxxxxxxxxxxxxxxxxxxx
sign (1) exponent (8) mantissa (23)
Finally, the last 23 bits represent f from the fractional part of the
mantissa in the 1-plus form: (1.f )2
Machine Numbers and Errors
Floating-Point Representation
The procedure for determining the representation of a real
number x
If x is zero, it is represented by a full word of zero bits with the
possible exception of the sign bit.
For a nonzero x, first assign the sign bit for x and consider |x|.
Then convert both the integer and fractional parts of |x| from
decimal to binary. Next one-plus normalize (|x|)2 by shifting the
binary point so that the first bit to the left of the binary point is a
1 and all bits to the left of this 1 are 0
Machine Numbers and Errors
Floating-Point Representation
The procedure for determining the representation of a real
number x
To compensate for this shift of the binary point, adjust the
exponent of 2; that is, multiply by the appropriate power of 2. The
24-bit one-plus-normalized mantissa in binary is thus found.
Now the current exponent of 2 should be set equal to c ↔ 127 to
determine c, which is then converted from decimal to binary. The
sign bit of the mantissa is combined with (c)2 and (f )2 . Finally,
write the 32-bit representation of x as eight hexadecimal digits.
Machine Numbers and Errors
Floating-Point Representation
Example: Determine the single-precision machine representation of
the decimal number ↔52.234375 in single precision.
Converting the integer part to binary, we have
(52)10 = (110100.)2
Next, converting the fractional part, we have
(.234375)10 = (.001111)2
(52.234375)10 = (110100.001111)2 = (1.101000011110)2 x25
is the corresponding one-plus form in base 2, and
(.101000011110)2 is the stored mantissa. Next the exponent is
(5)10 , and since c ↔ 127 = 5, we immediately see that
(132)10 = (10000100)2 is the stored exponent.
Machine Numbers and Errors
Floating-Point Representation
Thus, the single-precision machine representation of ↔52.234375 is
[1 10 000 100 101 000 011 110 000 000 000 00]2
= [11000010010100001111000000000000]2
Example: Determine the decimal numbers that correspond to if
the first number in binary is
[0100 0101 1101 1110 0100 0000 0000 0000]2
and the second word in binary is
[1011 1010 0011 1001 0000 0000 0000 0000]2
Machine Numbers and Errors
Floating-Point Representation
Solution: The stored exponent is (10 001 011)2 = (139)10 , so
139 ↔ 127 = 12. The mantissa is positive and represents the
number
(1.101 111 001)2 → 212 = (1 101 111 001 000.)2 = 7112
The exponential part of the word is (01110100)2 = 116, so the
exponent is 116 ↔ 127 = ↔11. The mantissa is negative and
corresponds to the following floating point number:
↔(1.011100100)2 → 2→11 = ↔(0.000000000010111001)2
↘ ↔7.0571899 → 10→4
Machine Numbers and Errors
Errors in Representing Numbers
Errors
The error that results from replacing a number with its
floating-point form is called round-o! error. For example, 0, 3̄ in
¯ We have to ignore some numbers after some
binary system 0, 01.
point because of finite number precision. Floating point
representation in single-precision floating point form is
00111110101010101010101010101011
If we convert this into decimal system we get the number
0.3333333432674407958984375
The error is approximately 9.934107x10→9 . So, a real number and
its representation in computer may not be the same. Let s is be
real number, then let fl(s) denote floating point representation and
we know that s = fl(s) may not hold.
Machine Numbers and Errors
Errors in Representing Numbers
Types of Errors
There are two types of errors: Rounding error and Chopping error.
Rounding: If the number you are rounding is followed by
5, 6, 7, 8, 9, round the number up.
Example:0, 0256815 is rounded to 0, 0257 for 4 decimal places.
136, 01546312 is rounded to 136, 01546 for 5 decimal places.
Chopping: Rounding a number by chopping amounts to dropping
the extra digits.
Example:0, 0256815 is chopped to 0, 0256 for 4 decimal places.
136, 01546312 is chopped to 136, 01546 for 5 decimal places.
Machine Numbers and Errors
Errors in Representing Numbers
How We Measure Approximation Errors?
There are three common methods for measuring approximation
errors.
Definition: Let a: exact value , â: approximate value. Then,
Ea = |a ↔ â| absolute error
|a ↔ â|
Er = , a ↑= 0, relative error
|a|
|a ↔ â|
Ep = ⇐ 100 = Er ⇐ 100 relative error percentage
|a|
Machine Numbers and Errors
Errors in Representing Numbers
Example: Let
a = 3.141592, â = 3.14
given. Then, we find
Ea = |a ↔ â| = |3.141592 ↔ 3.14| = 0.001592 absolute error
|a ↔ â| 0.001592
Er = = = 0.000507 relative error
|a| 3.141592
Ep = Er ⇐ 100 = %0.0507 relative error percentage
Machine Numbers and Errors
Errors in Representing Numbers
Example: i) Let a = 0.3000 → 101 , â = 0.3100 → 101 given.
Ea = |a ↔ â| = 0.1 absolute error
|a ↔ â|
Er = = 0.3333̄ → 10→1 relative error
|a|
ii) Let a = 0.3000 → 10→3 , â = 0.3100 → 10→3 given.
Ea = |a ↔ â| = 0.1 → 10→4 , absolute error
|a ↔ â|
Er = = 0.3333̄ → 10→1 relative error
|a|
iii) Let a = 0.3000 → 104 , â = 0.3100 → 104 given.
Ea = |a ↔ â| = 0.1 → 103 , absolute error
|a ↔ â|
Er = = 0.3333̄ → 10→1 relative error
|a|
Machine Numbers and Errors
Errors in Representing Numbers
This example shows that the same relative error can occur for
widely varying absolute errors.
As a measure of accuracy, the absolute error can be misleading and
the relative error is more meaningful, since the relative error takes
into consideration the size of the true value.