#drnsecbasicsnum#228>
Numbers come in many different flavors: positive and negative integers,
fractions (also known as rationals) and reals are the most well-known
classes of numbers:
<#60311#><#230#>5<#230#><#60311#>
<#60312#><#231#>-5<#231#><#60312#>
<#60313#><#232#>2/3<#232#><#60313#>
<#60314#><#233#>17/3<#233#><#60314#>
<#60315#><#234#>#<#234#><#235#>i1.4142135623731<#235#><#60315#>
The first is an integer, the second one a negative integer, the next two
are fractions, and the last one is an inexact representation of a real
number.
Like a pocket calculator, the simplest of computers, Scheme permits
programmers to add, subtract, multiply, and divide numbers:
<#60316#><#238#>(+<#238#>\ <#239#>5<#239#>\ <#240#>5)<#240#><#60316#>
<#60317#><#241#>(+<#241#>\ <#242#>-5<#242#>\ <#243#>5)<#243#><#60317#>
<#60318#><#244#>(+<#244#>\ <#245#>5<#245#>\ <#246#>-5)<#246#><#60318#>
<#60319#><#247#>(-<#247#>\ <#248#>5<#248#>\ <#249#>5)<#249#><#60319#>
<#60320#><#250#>(*<#250#>\ <#251#>3<#251#>\ <#252#>4)<#252#><#60320#>
<#60321#><#253#>(/<#253#>\ <#254#>8<#254#>\ <#255#>12)<#255#><#60321#>
The first three ask Scheme to perform additions; the last three demand a
subtraction, a multiplication, and a division. All arithmetic expressions
are parenthesized and mention the operation first; the numbers follow the
operation and are separated by spaces.
As in arithmetic or algebra, we can nest expressions:
<#261#>(*<#261#> <#262#>(+<#262#> <#263#>2<#263#> <#264#>2)<#264#> <#265#>(/<#265#> <#266#>(*<#266#> <#267#>(+<#267#> <#268#>3<#268#> <#269#>5)<#269#> <#270#>(/<#270#> <#271#>30<#271#> <#272#>10))<#272#> <#273#>2))<#273#>
Scheme evaluates these expressions exactly like we do. It first reduces the
innermost parenthesized expressions to numbers, then the next layer and so
on:
<#281#>(*<#281#> <#282#>(+<#282#> <#283#>2<#283#> <#284#>2)<#284#> <#285#>(/<#285#> <#286#>(*<#286#> <#287#>(+<#287#> <#288#>3<#288#> <#289#>5)<#289#> <#290#>(/<#290#> <#291#>30<#291#> <#292#>10))<#292#> <#293#>2))<#293#>
<#294#>=<#294#> <#295#>(*<#295#> <#296#>4<#296#> <#297#>(/<#297#> <#298#>(*<#298#> <#299#>8<#299#> <#300#>3)<#300#> <#301#>2))<#301#>
<#302#>=<#302#> <#303#>(*<#303#> <#304#>4<#304#> <#305#>(/<#305#> <#306#>24<#306#> <#307#>2))<#307#>
<#308#>=<#308#> <#309#>(*<#309#> <#310#>4<#310#> <#311#>12)<#311#>
<#312#>=<#312#> <#313#>48<#313#>
Because every Scheme expression has the shape
<#321#>(operation<#321#> <#322#>A<#322#> <#323#>...<#323#> <#324#>B)<#324#>
there is never any question about which part has to be evaluated first.
Whenever <#60322#><#328#>A<#328#>\ <#329#>...<#329#>\ <#330#>B<#330#><#60322#> are numbers, the expression can be evaluated;
otherwise, <#60323#><#331#>A<#331#>\ <#332#>...<#332#>\ <#333#>B<#333#><#60323#> are evaluated first. Contrast this with
#displaymath72416#
which is an expression that we encounter in grade school. Only a
substantial amount of practice guarantees that we remember to evaluate the
multiplication first and the addition afterwards.
Finally, Scheme not only provides simple arithmetical operations but a whole
range of advanced mathematical operations on numbers. Here are five
examples:
- <#60324#><#336#>(sqrt<#336#>\ <#337#>A)<#337#><#60324#> computes #tex2html_wrap_inline72418#;
- <#60325#><#339#>(expt<#339#>\ <#340#>A<#340#>\ <#341#>B)<#341#><#60325#> computes #tex2html_wrap_inline72420#;
- <#60326#><#342#>(remainder<#342#>\ <#343#>A<#343#>\ <#344#>B)<#344#><#60326#> computes the remainder of the integer division A/B;
- <#60327#><#345#>(log<#345#>\ <#346#>A)<#346#><#60327#> computes the natural logarithm of <#60328#><#347#>A<#347#><#60328#>;
and
- <#60329#><#348#>(sin<#348#>\ <#349#>A)<#349#><#60329#> computes the sine of <#350#>A<#350#> radians.
When in doubt whether a primitive operation exists or how it works, use
DrScheme to test whether an operation is available with a simple example.
<#352#>A Note on Numbers<#352#>:\
Scheme computes with <#60330#><#354#>EXACT<#354#><#60330#> integers and rationals as long as we
use primitive operations that produce exact results. Thus, it displays the
result of <#60331#><#355#>(/<#355#>\ <#356#>44<#356#>\ <#357#>14)<#357#><#60331#> as <#60332#><#358#>22/7<#358#><#60332#>. Unfortunately, Scheme and
other programming languages compromise as far as real numbers are
concerned. For example, since the square root of <#60333#><#359#>2<#359#><#60333#> is not a
rational but a real number, Scheme uses an <#60334#><#360#>INEXACT NUMBER<#360#><#60334#>:
<#365#>(sqrt<#365#> <#366#>2)<#366#>
<#367#>=<#367#> <#368#>#<#368#><#369#>i1.4142135623731<#369#>
The <#60335#><#373#>#<#373#><#374#>i<#374#><#60335#> notation warns the programmer that the result is an
approximation of the true number. Once an inexact number has become a part
of a calculation, the process continues in an approximate manner the true result of
operations. To wit:
<#379#>(-<#379#> <#380#>#<#380#><#381#>i1.0<#381#> <#382#>#<#382#><#383#>i0.9)<#383#>
<#384#>=<#384#> <#385#>#<#385#><#386#>i0.09999999999999998<#386#>
but
<#394#>(-<#394#> <#395#>#<#395#><#396#>i1000.0<#396#> <#397#>#<#397#><#398#>i999.9)<#398#>
<#399#>=<#399#> <#400#>#<#400#><#401#>i0.10000000000002274<#401#>
even though we know from mathematics that both differences should be
<#60336#><#405#>0.1<#405#><#60336#> and equal. Once numbers are inexact, caution is necessary.
This imprecision is due to the common simplification of writing down
numbers like the square root of 2 or #tex2html_wrap_inline72424# as rational numbers. Recall that
the decimal representation of these numbers are infinitely long (without
repetition). A computer, however, has a finite size, and can therefore
only represent a portion of such a number. If we choose to represent these
numbers as rationals of with a fixed number of digits, the representation
is necessarily inexact. Intermezzo~6 will explain how inexact numbers work.
To focus our studies on the important concepts of computing and away from
these details, the teaching lanuages of DrScheme deal with numbers as
precise numbers as much as possible. When we write <#60337#><#406#>1.25<#406#><#60337#>, DrScheme
interprets this number as a precise fraction, not as an inexact number.
When DrScheme's <#407#>Interaction<#407#> window displays a number such as
<#60338#><#408#>1.25<#408#><#60338#> or <#60339#><#409#>22/7<#409#><#60339#>, it is the result of a computation with
precise rationals and fractions. Only numbers prefixed by <#60340#><#410#>#<#410#><#411#>i<#411#><#60340#> are
inexact representations.~<#60341#><#60341#>
<#416#>Exercise 2.1.1<#416#>
Evaluate <#60342#><#418#>(sqrt<#418#>\ <#419#>4)<#419#><#60342#>, <#60343#><#420#>(sqrt<#420#>\ <#421#>2)<#421#><#60343#>, and <#60344#><#422#>(sqrt<#422#>\ <#423#>-1)<#423#><#60344#> in
DrScheme. Then, find out whether DrScheme knows an operation for
determining the tangent of an angle. <#60345#><#60345#>