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.
(expt 10 -200)
(expt 10.0 -200)
(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
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
99 * 10500
1/10500
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.