Fundamentals of Computer Science I (CS151.01 2006F)
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Homework]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151.02 2006F (Davis)]
[CSCS151 2005S (Stone)]
[CSC151 2003F (Rebelsky)]
[CSC153 2004S (Rebelsky)]
This lab is also available in PDF.
Summary: We explore the variety of types of numbers that Scheme (well, DrScheme) provides. We also explore some of the interesting procedures that operate on numeric values.
Numeric predicates used in this lab:
complex?
,
exact?
,
inexact?
,
number?
,
rational?
, and
real?
.
Numeric procedures used in this lab:
denominator
,
numerator
, and
sqrt
.
Contents:
Start DrScheme.
Have DrScheme confirm that 3/4 is a rational number but not an integer and that the square root of 1 is a complex number but not a real number.
Confirm that the value DrScheme computes for (sqrt 2)
is an
inexact real that is also rational.
As you know, one of the goals we have in this class is of verifying the answers given to us by algorithms (primarily our own algorithms, but, at times, those given to us by the computer).
We might confirm that the value returned by (sqrt 2)
is
correct by computing the square of that value and then subtracting 2.
If the square root is correct, the result should be 0.
Check the value returned by the previously described expression (that is, two less than the square of the square root of 2). Is it 0? Why do you think you got the answer you got?
a. Do you expect to have the same problem as in the previous exercise if you compute the square root of 4 rather than the square root of 2? Why or why not?
b. Confirm your answer experimentally.
Write a Scheme numeral for 1.507 times ten to the fifteenth power, as an exact number. Have Scheme evaluate the numeral.
Write a Scheme numeral for onethird, as an inexact number. Have Scheme evaluate the numeral.
Scheme provides a number of numerical procedues that can
produce integer results.
We've already explored
expt
,
abs
,
+
,

, and
*
.
Here are some others. For each, try to figure (by experimentation, by discussing results with other students, and, eventually, by reviewing the reading and associated documentation) out how many parameters each procedure can take and what the procedure does. Make sure to try a variety of values for each procedure, including positive and negative, integer and real.
Warning: You may not be able to figure all of them out.
a. quotient
b. remainder
c. modulo
d. max
e. min
f. numerator
g. denominator
h. gcd
i. lcm
j. floor
k. ceiling
l. truncate
m. round
Since you've found that DrScheme seems to represent every real number as a rational, it might be worth finding a way to see what that rational number is.
a. Determine the numerator and denominator of the rational representation of the square root of 2.
b. Determine the numerator and denominator of the rational representation of 1.5.
c. Determine the numerator and denominator of the rational representation of 1.2.
d. Determine the numerator and denominator of 6/5.
e. Determine the numerator and denominator of #i6/5.
If you're puzzled by some of the later answers, you may want to read the notes on this problem.
For small numbers, the
exact>inexact
procedure produces lots and lots of digits after the decimal point.
Figure out how to get just two digits after the decimal point. You may
need to use multiplication, division, and some of the last procedures
from the previous exercise.
You need not implement your algorithm; simply come up with one you think will work.
We've already seen a variety of predicates (procedures that return
true or false) that can be applied to numbers. These predicates
include
exact?
,
integer?
, and
real?
.
By reading the Scheme documentation, identify other predicates that can be applied to numbers.
If you finish early, you might
(inexact>exact (exact>inexact a/b))
is not the same as a/b
.
When you get stuck on this problem, it's probably worth skimming through DrScheme's Help Desk. The numeric operations are documented in section 6.2.5 of the Revised(5) Report on the Algorithmic Language Scheme. (I have not yet put all of these procedures in the Glimmer Scheme Reference.
DrScheme seems to represent the fractional part of many numbers as the ratio of some number and 4503599627370496, which happens to be 2^{52}. (Most computers like powers of 2.) If you are energetic, you might scour the Web to find out why they use an exponent of 52.
Wednesday, 31 January 2001 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/Labs/numbers.html
.
Monday, 9 September 2002 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Labs/numbers.html
.
Thursday, 30 January 2003 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003S/Labs/numbers.html
.
Tuesday, 9 September 2003 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003S/Labs/numbers.html
.
Friday, 1 September 2006 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2006F/Labs/numbers.html
.
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Homework]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151.02 2006F (Davis)]
[CSCS151 2005S (Stone)]
[CSC151 2003F (Rebelsky)]
[CSC153 2004S (Rebelsky)]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Thu Nov 30 21:43:05 2006.
The source to the document was last modified on Fri Sep 1 11:39:46 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2006F/Labs/numbers.html
.
You may wish to validate this document's HTML ; ;
Samuel A. Rebelsky, rebelsky@grinnell.eduhttp://creativecommons.org/licenses/bync/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.