Fundamentals of Computer Science I: Media Computing (CS151.01 2008S)
Summary: We explore some of the kinds of numbers and procedures that Scheme (well, DrFu) supports.
a. Create a 200x100 image and name it
Consider the following procedure.
(define bound (lambda (val lower upper) (min (max val lower) upper)))
a. Suppose we always use 0 for
upper. What value do you expect
bound to return when
is 10? 120? 2386? -42?
b. Check your answers experimentally.
c. Explain why this procedure is called
Consider the following procedure.
(define image-bounded-select-ellipse! (lambda (image selection left top width height) (image-select-ellipse! image selection (bound left 0 (* 0.5 (image-width image))) (bound top 0 (* 0.5 (image-height image))) (bound width 20 40) (bound height 20 40))))
a. Identify at least one set of values for which
image-bounded-select-ellipse! behaves the same
b. Identify at least one set of values for which
image-bounded-select-ellipse! behaves differently
c. Explain in English what
As the reading suggests, the
computes a value much like the remainder, except that the result is
always the same sign as the second parameter, called the modulus.
(So, when we use a positive modulus, we get a positive result.)
The reading also suggests that
an interesting alternative to using
min to limit the values of functions.
a. What value do you expect each of the following to produce?
(modulo 254 256)
(modulo 256 256)
(modulo 257 256)
(modulo 515 256)
(modulo 2567 256)
(modulo 0 256)
(modulo -256 256)
(modulo -257 256)
(modulo -255 256)
(modulo -1 256)
b. Check your answers experimentally, one at a time. If you find that any of your answers don't match what Scheme does, try to figure out why (asking me or a tutor if necessary), and then rethink your remaining answers before checking them experimentally.
As the reading on
numbers suggests, Scheme provides four functions that convert
real numbers to nearby integers:
truncate. The reading also claims that there
are differences between all four.
To the best of your ability, figure out what each does, and what distinguishes it from the other three. In your tests, you should try both positive and negative numbers, numbers close to whole numbers and numbers far from whole numbers. (Numbers whose fractional part is 0.5 are about as far from a whole number as any real number can be.)
Once you have figured out answers, check, the notes on this problem.
As you may recall from a previous lab, it is sometimes useful to be able to count the value 1 for a high score and a value 0 for a lower score. (For convenience, we'll say that a score of 80 or above is high and a score below 80 is low. We'll also assume that all scores are between 0 and 100.)
Most of us would give the instructions for converting score to count as something like “If the score is 80 or above, the count is 1; otherwise, the count is 0”. However, you have yet to learn to write conditionals.
Are you doomed? Certainly not. One of the four functions you've just learned, in conjunction with some other arithmetic operations would allow us to create counts for scores. (Yes, we're being deliberately vague. Part of the goal of this problem is for you to think about approaches.)
Write a procedure,
( that, given a grade between 0
and 100, returns 0 if the number is less than 80 and 1 if the grade
is 80 or above. If the grade is not in the range [0..100], this
procedure can do anything.
You may recall that we have a number of mechanisms for rounding real numbers to integers. But what if we want to round not to an integer, but to only two digits after the decimal point? Scheme does not include a built-in operation for doing that kind of rounding. Nonetheless, it is fairly straightforward.
Write a procedure,
( that rounds
to the nearest hundredth. For example,
As you may have noted, the procedure
image-bounded-select-ellipse! converted an
elliptical selection to one that had to start in the upper-left
quadrant and had both height and width between 20 and 40.
We might use
modulo to achieve
similar limits. Here's one attempt.
(define image-strange-select-ellipse! (lambda (image selection left top width height) (image-select-ellipse! image selection (modulo left (quotient (image-width image) 2)) (modulo top (quotient (image-height image) 2)) (+ 20 (modulo (- width 20) 21)) (+ 20 (modulo (- height 20) 21)))))
a. What differences, if any, do you expect between the following three selection calls:
(image-select-ellipse! canvas selection-replace 10 10 30 30)
(image-bounded-select-ellipse! canvas selection-replace 10 10 30 30)
(image-strange-select-ellipse! canvas selection-replace 10 10 30 30)
b. Check your answer experimentally.
c. What differences, if any, do you expect between the following three selection calls:
(image-select-ellipse! canvas selection-replace 100 10 30 30)
(image-bounded-select-ellipse! canvas selection-replace 100 10 30 30)
(image-strange-select-ellipse! canvas selection-replace 100 10 30 30)
d. Check your answer experimentally.
e. What differences, if any, do you expect between the following three selection calls:
(image-select-ellipse! canvas selection-replace 10 10 60 5)
(image-bounded-select-ellipse! canvas selection-replace 10 10 60 5)
(image-strange-select-ellipse! canvas selection-replace 10 10 60 5)
f. Check your answer experimentally.
Here are the ways we tend to think of the four functions:
the largest integer less than or equal to
Some would phrase this as “
finds the smallest integer greater than or equal to
r. Some would phrase this as
ceiling rounds up”.
removes the fractional portion of
r, the portion
after the decimal point.
r to the nearest integer.
It rounds up if the decimal portion is greater than 0.5 and it rounds
down if the decimal portion is less than 0.5. If the decimal portion
equals 0.5, it rounds toward the even number.
It's pretty clear that
ceiling differ - If
has a fractional component, then
( is one less than
It's also pretty clear that
round differs from all of them,
since it can round in two different directions.
We can also tell that
truncate is different from
ceiling, at least for positive numbers, because
ceiling always rounds up, and
removing the fractional portion of a positive number causes us
to round down.
So, how do
differ? As the previous paragraph implies, they differ for
negative numbers. When
you remove the fractional component of a negative number, you
effectively round up. (After all, -2 is bigger than -2.3.) However,
floor always rounds down.
Why does Scheme include so many ways to convert reals to integers? Because experience suggests that if you leave any of them out, some programmer will need that precise conversion.
Copyright (c) 2007-8 Janet Davis, Matthew Kluber, and Samuel A. Rebelsky. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)
This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.
This work is licensed under a Creative Commons
Attribution-NonCommercial 2.5 License. To view a copy of this
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.