Section 33

How to Design Programs: An Introduction to Programming and Computing

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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 fixed-size chunks. Programming languages must mediate the gap between these fixed-size 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 hardware-based choice. This intermezzo explains the fixed-size representation for numbers and its consequences in some detail. The first subsection introduces a concrete fixed-size 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 fixed-size number arithmetic: overflow and underflow, respectively. 33.1 Fixed-size 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 so-called 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 10-based factor. For pure scientific notation, the base is between 0 and 9. We relax this constraint and just write numbers as [curriculumE-Z-G-1.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/curriculum-Z-H-41.html (1 of 10) [2/5/2008 4:55:59 PM]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
How to Design Programs: An Introduction to Computing and Programming [curriculumE-Z-G-2.gif] another one is [curriculumE-Z-G-3.gif] In general, a number has several equivalents in mantissa-exponent 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, [curriculumE-Z-G-4.gif] stands for [curriculumE-Z-G-5.gif] As before, the fraction has several representations in the new notation. To use a form of mantissa-exponent 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 [curriculumE-Z-G-6.gif]
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This test prep was uploaded on 02/06/2008 for the course CS 1102 taught by Professor Fisler during the Fall '07 term at WPI.

Page1 / 10

Section 33 - How to Design Programs: An Introduction to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online