COMP203: Lecture 20


Syllabus | Homework and Assignments | Grading Rubric | Midterm Exam | Final Project

Decimals and Precision

It is not true that decimal representations are more precise than fractions, even if the word "precision" is seen more often when speaking of decimal numbers. Fractions are exact representations of numbers. In contrast, some decimal numbers are merely approximations of exact values.

For example, consider the fraction 1/3. The fraction 1/3 describes an exact value. You can easily imagine 1/3 of a cheese pizza.

Now let's think about a decimal approximation of 1/3. 1/3 is about 3/10, so one possible approximation is 0.3. This is not a very precise approximation -- it only has one digit. In fact, the estimate is "off" by 1/30 = 1/3 - 3/10.

To get a more precise approximation, we can use two decimal places. 1/3 is about 33/100, so a better approximation would be 0.33. This estimate is off by 1/300 = 1/3 - 33/100.

We can get as many decimal places of accuracy as we wish by using long division. By dividing 3 into 1 we will quickly see that 1/3 = 1.333...; there are infinitely many 3's after the decimal point.

The Digits variable and evalf command in Maple allow us to approximate fractions as closely as we like, but the decimal numbers Maple gives us are often merely approximations of an exact value.

Maple Programming and Shift-Enter

Below is a definition of a Maple procedure that adds two numbers. What happens if you try to type it into Maple?
thing := proc(a,b)
  # A procedure to add two numbers.
  a+b;
end proc;
The error message Error, unterminated procedure means that Maple thinks you've finished writing a procedure without giving it an "end" instruction. Of course you hadn't finished -- you had only just typed in the name of the procedure and its inputs!

The problem is that Maple interprets your hitting the Enter key as a request to evaluate what you've typed in. We need some sort of equivalent of Logo's ~ to tell Maple "this procedure definition will be continued on the next line". That equivalent is holding down the Shift key while hitting Enter.

Try typing in the procedure again, but this time when you get to the end of the first line, hold down the Shift key while you hit the Enter key. Now you can type in the second line (a comment) without getting an error from Maple. Keep holding down the Shift key as you advance to each new line until you have typed in end proc;. Then hit Enter without the Shift key held down to tell Maple to evaluate the procedure definition.

It takes a while to get the hang of this. Be patient, and when Maple gives you an error message just move the cursor up to the end of the last line you typed and hit Shift-Enter in stead of Enter. You may have to do this several times while writing your first few procedures, but with practice holding down the shift key will become automatic.