[previous] [up] [next]     [index]
Next: Changing the State of Up: Intermezzo 6 The Nature of Previous: Underflow

DrScheme's Numbers

Most programming languages support only inexact number representations (and arithmetic) for both integers and reals. Scheme, in contrast, supports both exact and inexact numbers and arithmetic. Of course, the base of the representation is 2, not 10, because Scheme uses the underlying computer's on-off machinery.

As the note on page [cross-reference] explained, DrScheme's teaching levels interpret all numbers in our programs as exact rationals, unless they are prefixed with #i. Some numeric operations, though, produce inexact numbers. Plain Scheme, which is called Full Scheme in DrScheme, interprets all numbers with a dot as inexact numbers;[footnote] it also prints inexact reals with just a dot, implying that all such numbers are inexact and possibly distant from the actual result.

Scheme programmers can thus choose to use exact arithmetic or inexact arithmetic as necessary. For example, numbers in financial statements should always be interpreted as exact numbers; arithmetical operations on such numbers should be as precise as possible. For some problems, however, we may not wish to spend the extra time to produce exact results. Scientific computations are a primary example. In such cases, we may wish switch to inexact numbers and arithmetic.

Numerical Analysis: When we use inexact numbers and arithmetic, it is natural to ask how much the program's results differs from the true results. Over the past few decades, the study of this complex question has evolved into an advanced topic, called numerical analysis. The discipline has become a subject of its own right in applied mathematics or in computer science departments. 


Exercises


external

Exercise 33.4.1

Evaluate

(expt 1.001 1e-12)
in Full Scheme (any variant) and in Intermediate Student Scheme. Explain the observations. Solution

Exercise 33.4.2

Develop the function my-expt, which raises one number to the power of some integer. Using this function, conduct the following experiment. Add

(define inex (+ 1 #i1e-12))
(define exac (+ 1 1e-12)) 
to the Definitions window. What is (my-expt inex 30)? How about (my-expt exac 30)? Which answer is more useful? Solution

Exercise 33.4.3

When we add two inexact numbers of vastly different orders of magnitude, we may get the larger one back as the result. For example, if we are using only 15 significant digits, then we run into problems when adding numbers which vary by more than a factor of tex2html_wrap_inline73303 :

displaymath73305

but if the number system supports only 15 digits, the closest answer is tex2html_wrap_inline73303 . At first glance, this doesn't look too bad. After all, being wrong by one part in tex2html_wrap_inline73303 (ten million billion) is close enough to the accurate result. Unfortunately, this kind of problem can add up to huge problems.

Consider the following list of inexact numbers:

(define JANUS
  (list #i31 
        #i2e+34 
        #i-1.2345678901235e+80 
        #i2749 
        #i-2939234 
        #i-2e+33 
        #i3.2e+270 
        #i17 
        #i-2.4e+270 
        #i4.2344294738446e+170 
        #i1 
        #i-8e+269 
        #i0 
        #i99)) 
Determine the values (sum JANUS) and (sum (reverse JANUS)). Explain the difference. Can we trust computers? Solution

  tex2html_wrap73311    


[previous] [up] [next]     [index]
Next: Changing the State of Up: Intermezzo 6 The Nature of Previous: Underflow

PLT