How to Design Programs: An Introduction to Computing and Programming
[Go to
first
,
previous
,
next
page;
contents
;
index
]
Section 33
Intermezzo 6: The Nature of Inexact Numbers
Computers represent and process information in chunks of a fixed size. Because computers were first used
for numerical calculations, early computer engineers developed a representation for numbers in terms of
fixedsize chunks. Programming languages must mediate the gap between these fixedsize representations
and the true mathematics. Because using the hardware representation for numbers makes a program's
calculations as efficient as possible, most designers and implementors of programming languages adopted
the hardwarebased choice.
This intermezzo explains the fixedsize representation for numbers and its consequences in some detail. The
first subsection introduces a concrete fixedsize representation for numbers, discusses what it implies for the
representation of numbers, and shows how to calculate with such numbers. The second and third section
illustrate the two most fundamental problems of fixedsize number arithmetic: overflow and underflow,
respectively.
33.1
Fixedsize Number Arithmetic
Suppose we can use four digits to represent numbers. If we represent natural numbers, the representable
range is 0
...
9999. Alternatively we could represent 10,000 fractions between 0 and 1 with that many
digits. In either case, this is a rather small range of numbers and not useful for most scientific or business
computations.
We can represent a larger range of numbers if we use a different notation for numbers instead. In science, for
example, we encounter socalled scientific notation, which represents numbers as two parts:
1.
a MANTISSA, which is a base number, and
2.
an EXPONENT, which is used to determine a 10based factor.
For pure scientific notation, the base is between 0 and 9. We relax this constraint and just write numbers as
[curriculumEZG1.gif]
where
m
is the mantissa and
e
the exponent. For example, one representation of 1200 with this scheme is
file:///C/Documents%20and%20Settings/Linda%20Graue.
..How%20to%20Design%20Programs/curriculumZH41.html (1 of 10) [2/5/2008 4:55:59 PM]
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentHow to Design Programs: An Introduction to Computing and Programming
[curriculumEZG2.gif]
another one is
[curriculumEZG3.gif]
In general, a number has several equivalents in mantissaexponent representation.
We can also use negative exponents, which add fractions at the cost of one extra piece of data: the sign of the
exponent. For example,
[curriculumEZG4.gif]
stands for
[curriculumEZG5.gif]
As before, the fraction has several representations in the new notation.
To use a form of mantissaexponent notation for our problem, we must decide how many digits we wish to
use for the representation of the mantissa and how many for the exponent. Here we use two for each and a
sign for the exponent; other choices are possible. Given this decision, we can still represent 0 as
[curriculumEZG6.gif]
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '07
 Fisler
 Scientific Notation, DrScheme

Click to edit the document details