[previous] [up] [next]     [index]
Next: Variables and Programs Up: NumbersExpressions, Simple Programs Previous: NumbersExpressions, Simple Programs

Numbers and Arithmetic

external

Numbers come in many different flavors: positive and negative integers, fractions (also known as rationals), and reals are the most widely known classes of numbers: 5 -5 2/3 17/3 #i1.4142135623731

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: (+ 5 5) (+ -5 5) (+ 5 -5) (- 5 5) (* 3 4) (/ 8 12)

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.

external As in arithmetic or algebra, we can nest expressions:

  (* (+ 2 2) (/ (* (+ 3 5) (/ 30 10)) 2))
Scheme evaluates these expressions exactly as we do. It first reduces the innermost parenthesized expressions to numbers, then the next layer, and so on:
  (* (+ 2 2) (/ (* (+ 3 5) (/ 30 10)) 2))
= (* 4 (/ (* 8 3) 2)) 
= (* 4 (/ 24 2)) 
= (* 4 12) 
= 48 
Because every Scheme expression has the shape

(operation A ... B)
there is never any question about which part has to be evaluated first. Whenever A ... B are numbers, the expression can be evaluated; otherwise, A ... B are evaluated first. Contrast this with

displaymath71572

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.[footnote]

Finally, Scheme not only provides simple arithmetical operations but a whole range of advanced mathematical operations on numbers. Here are five examples:

  1. (sqrt A) computes tex2html_wrap_inline71574 ;
  2. (expt A B) computes tex2html_wrap_inline71576 ;
  3. (remainder A B) computes the remainder of the integer division A/B;
  4. (log A) computes the natural logarithm of A; and
  5. (sin A) computes the sine of A 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.

A Note on Numbers: Scheme computes with EXACT integers and rationals as long as we use primitive operations that produce exact results. Thus, it displays the result of (/ 44 14) as 22/7. Unfortunately, Scheme and other programming languages compromise as far as real numbers are concerned. For example, since the square root of 2 is not a rational but a real number, Scheme uses an INEXACT NUMBER:

  (sqrt 2)
= #i1.4142135623731 
The #i 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. To wit:
  (- #i1.0 #i0.9)
= #i0.09999999999999998 
but
  (- #i1000.0 #i999.9)
= #i0.10000000000002274 
even though we know from mathematics that both differences should be 0.1 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_inline71582 as rational numbers. Recall that the decimal representations of these numbers are infinitely long (without repetition). A computer, however, has a finite size, and therefore can only represent a portion of such a number. If we choose to represent these numbers as rationals 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 not on these details, the teaching languages of DrScheme deal as much as possible with numbers as precise numbers. When we write 1.25, DrScheme interprets this number as a precise fraction, not as an inexact number. When DrScheme's Interactions window displays a number such as 1.25 or 22/7, it is the result of a computation with precise rationals and fractions. Only numbers prefixed by #i are inexact representations. 


Exercises

Exercise 2.1.1

Find out whether DrScheme has operations for squaring a number; for computing the sine of an angle; and for determining the maximum of two numbers. Solution

Exercise 2.1.2

Evaluate (sqrt 4), (sqrt 2), and (sqrt -1) in DrScheme. Then, find out whether DrScheme knows an operation for determining the tangent of an angle. Solution



[previous] [up] [next]     [index]
Next: Variables and Programs Up: NumbersExpressions, Simple Programs Previous: NumbersExpressions, Simple Programs

PLT