6.11.0.4

### 11 The Nature of Numbers

Purpose Decimals numbers in almost all programming languages are not what they seem. A first course on programming and computing must include a first glimpse at the pitfalls of numeric computations. So this lab is really a lecture with hands-on exercises that drive home the treacherous nature of numbers.

Textbook references Intermezzo 4: The Nature of Numbers

### Representing Numbers in Computers

15 minutes

The Real Numbers are information. Due to their importance, the developers of computing hardware and programming languages had to decide how to represent this information.

The teaching languages represent decimal constants (that you write down) as exact rational numbers. Just to get an understanding of what this means, enter expressions such as

(expt 10 -200)

into the interactions window and watch. Now compare this with

(expt 10.0 -200)

What happened?

The designers of your computers understood that a faithful representation of numbers would require the use of arbitrarily large forms of data and that addition, multiplication, and other operations would have to traverse this data—meaning they would take more than a constant amount of time. While such a choice might be acceptable for calculators, it didn’t seem so for computer programs that would perform gazillions of such operations (say to simulate the air flow around the wings of jets, nuclear explosions, or weather predictions). Hence hardware designers chose to go with a fixed-size data representation:
 (define-struct inex [mantissa sign exponent]) ; An Inex is a structure: ;   (make-inex N99 S N99) ; An S is one of: ; – +1 ; – -1 ; An N99 is an N between 0 and 99 (inclusive).

• Represent 1200 with an Inex.

• Interpret (make-inex 52 -1 43) as a real number.

• Explain
 ; N Number N -> Inex ; makes an instance of Inex after checking the arguments (define (create-inex m s e) (cond [(and (<= 0 m 99) (<= 0 e 99) (or (= s 1) (= s -1))) (make-inex m s e)] [else (error "bad values given")])) ; Inex -> Number ; converts an inex into its numeric equivalent (define (inex->number an-inex) (* (inex-mantissa an-inex) (expt 10 (* (inex-sign an-inex) (inex-exponent an-inex)))))

### Code

15 minutes

Exercise 1 From HtDP/2e, 412 and 414.

### Code Review

15 minutes

The Teaching Assistants will pick two pairs who will explain a solution to their lab mates following the design recipe steps.

### Overflow and Underflow

15 minutes

While scientific notation expands the range of numbers we can represent with fixed-size pieces of data, the range remains finite. Hence some numbers are just too big for this representation. For example,

99 * 10500

can’t be represented because the exponent 500 won’t fit into two digits, and the mantissa is as large as legally possible. If an operation produces such a number, we speak of overflow.

At the opposite end of the spectrum, there are small numbers that don’t have a representation in Inex. For example,

1/10500

is not 0, but it’s smaller than the smallest non-zero number we can represent. If an operation produces such a number, we speak of underflow.

Exercise 2 From HtDP/2e, 415 and 416.

### Code Review

15 minutes

The Teaching Assistants will pick two pairs who will explain a solution to their lab mates following the design recipe steps.

### Demo

15 minutes

Let’s work through 419.