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~#anotenumbers#43610> explained, DrScheme's teaching levels interpret all numbers in our programs as exact rationals, unless they are prefixed with <#67975#><#43611#>#<#43611#><#43612#>i<#43612#><#67975#>. 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; 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. It is then important to ask a numerical analyst to study our program so that we understand how much the program's results can differ from the true results. Over the past few decades, this advanced topic, called numerical analysis, has become a subject of its own right in applied mathematics. Numerical analysts study how badly the result of a computation can differ from the mathematically correct result, and thus whether it is acceptable to trade speed of computation for lack of accuracy.
external ~<#43617#>The three exercises clarify why people use inexact arithmetic on computers and what risk they run into doing so. They should impress both aspects of the coin on students.<#43617#> <#43618#>Exercise 33.4.1<#43618#> Evaluate
<#43624#>(expt<#43624#> <#43625#>1.001<#43625#> <#43626#>1e-12)<#43626#>
in <#43630#>Full<#43630#> Scheme (any variant) and in <#43631#>Intermediate Student<#43631#> Scheme. Explain the observations.~ external Solution<#67976#><#67976#> <#43637#>Exercise 33.4.2<#43637#> Develop the function <#67977#><#43639#>my-expt<#43639#><#67977#>, which raises one number to the power of some integer. Using this function, conduct the following experiment. Add
<#43644#>(define<#43644#> <#43645#>inex<#43645#> <#43646#>(+<#43646#> <#43647#>1<#43647#> <#43648#>#<#43648#><#43649#>i1e-12))<#43649#>
<#43650#>(define<#43650#> <#43651#>exac<#43651#> <#43652#>(+<#43652#> <#43653#>1<#43653#> <#43654#>1e-12))<#43654#> 
to the <#43658#>Definitions<#43658#> window. What is <#67978#><#43659#>(my-expt<#43659#>\ <#43660#>inex<#43660#>\ <#43661#>30)<#43661#><#67978#>? How about <#67979#><#43662#>(my-expt<#43662#>\ <#43663#>exac<#43663#>\ <#43664#>30)<#43664#><#67979#>? Which answer is more useful?~ external Solution<#67980#><#67980#> <#43670#>Exercise 33.4.3<#43670#> 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_inline74076#:

#displaymath74078#

but if the number system supports only 15 digits, the closest answer is #tex2html_wrap_inline74080#. At first glance, this doesn't look too bad. After all, being wrong by one part in #tex2html_wrap_inline74082# (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:

<#43680#>(d<#43680#><#43681#>efine<#43681#> <#43682#>JANUS<#43682#>
  <#43683#>(list<#43683#> <#43684#>#<#43684#><#43685#>i31<#43685#> 
        <#43686#>#<#43686#><#43687#>i2e+34<#43687#> 
        <#43688#>#<#43688#><#43689#>i-1.2345678901235e+80<#43689#> 
        <#43690#>#<#43690#><#43691#>i2749<#43691#> 
        <#43692#>#<#43692#><#43693#>i-2939234<#43693#> 
        <#43694#>#<#43694#><#43695#>i-2e+33<#43695#> 
        <#43696#>#<#43696#><#43697#>i3.2e+270<#43697#> 
        <#43698#>#<#43698#><#43699#>i17<#43699#> 
        <#43700#>#<#43700#><#43701#>i-2.4e+270<#43701#> 
        <#43702#>#<#43702#><#43703#>i4.2344294738446e+170<#43703#> 
        <#43704#>#<#43704#><#43705#>i1<#43705#> 
        <#43706#>#<#43706#><#43707#>i-8e+269<#43707#> 
        <#43708#>#<#43708#><#43709#>i0<#43709#> 
        <#43710#>#<#43710#><#43711#>i99))<#43711#> 
Determine the values <#67981#><#43715#>(sum<#43715#>\ <#43716#>JANUS)<#43716#><#67981#> and <#67982#><#43717#>(sum<#43717#>\ <#43718#>(reverse<#43718#>\ <#43719#>JANUS))<#43719#><#67982#>. Explain the difference. Can we trust computers?~ external Solution<#67983#><#67983#>