# Laboratory: Numeric Values

Summary: We explore some of the kinds of numbers and procedures that Scheme (well, DrFu) supports.

## Preparation

a. Create a 200x100 image and name it `canvas`.

## Exercises

### Exercise 1: Bounds

Consider the following procedure.

```(define bound
(lambda (val lower upper)
(min (max val lower) upper)))
```

a. Suppose we always use 0 for `lower` and 100 for `upper`. What value do you expect `bound` to return when `val` is 10? 120? 2386? -42?

c. Explain why this procedure is called “`bound`”.

### Exercise 2: Bounding Objects

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 as `image-select-ellipse!`

b. Identify at least one set of values for which `image-bounded-select-ellipse!` behaves differently than `image-select-ellipse!`

c. Explain in English what `image-bounded-select-ellipse!` does.

### Exercise 3: Modulo

As the reading suggests, the `modulo` procedure 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 `modulo` provides an interesting alternative to using `max` and `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.

### Exercise 4: From Reals to Integers

As the reading on numbers suggests, Scheme provides four functions that convert real numbers to nearby integers: `floor`, `ceiling`, `round`, and `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.

### Exercise 5: Points for High Grades

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, ```(grade-count grade)``` 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. For example,

````>` `(grade-count 80)`
`1`
`>` `(grade-count 79)`
`0`
`>` `(grade-count 81)`
`1`
`>` `(grade-count 10)`
`0`
`>` `(grade-count 95)`
`1`
```

## For Those with Extra Time

If you have extra time left at the end of this lab, you might try the exploration below or you might try one of these problems.

### Extra 1: Rounding, Revisited

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, ```(round-to-hundredths r)``` that rounds `r` to the nearest hundredth. For example,

````>` `(round-to-hundredths 22.71256)`
`22.71`
`>` `(round-to-hundredths 10.7561)`
`10.76`
```

### Extra 2: Modulo, Revisited

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)`
```

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)`
```

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)`
```

## Notes

### Notes on Exercise 4: From Reals to Integers

Here are the ways we tend to think of the four functions:

`(floor r)` finds the largest integer less than or equal to `r`. Some would phrase this as “`floor` rounds down”.

`(ceiling r)` finds the smallest integer greater than or equal to `r`. Some would phrase this as “`ceiling` rounds up”.

`(truncate r)` removes the fractional portion of `r`, the portion after the decimal point.

`(round r)` rounds `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.

````>` `(round 1.5)`
`2`
`>` `(round 2.5)`
`2`
`>` `(round 7.5)`
`8`
`>` `(round 8.5)`
`8`
`>` `(round -1.5)`
`-2`
`>` `(round -2.5)`
`-2`
```

It's pretty clear that `floor` and `ceiling` differ - If `r` has a fractional component, then ```(floor r)``` is one less than `(ceiling r)`.

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 `truncate` and `floor` 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.

Samuel A. Rebelsky, rebelsky@grinnell.edu

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 license, visit `http://creativecommons.org/licenses/by-nc/2.5/` or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.