<<

Integer Division and Its Uses division In elementary school, you learned how to divide one integer by another. Your solu- tions to drill-and-practice problems probably looked something like the following: 3 1 4 | 13 which means that when dividing 13 by 4, the is 3 and the remainder is 1. and are computed all the time in real life. For instance, if you have a serious sweet tooth and $2, and a chocolate bar costs $.75, you buy two choco- late bars (although you might prefer to have 2.666). Similarly, 69 inches is more com- monly referred to as 5 feet, 9 inches, where 5 and 9 are the quotient and remainder, respectively, when 69 is divided by 12. Integer division in Scheme Integer division is done in Scheme via the functions truncate and remainder. Truncate, given a single numeric argument, returns the result of discarding any fractional part of the . For example, (truncate 3.8) returns 3 (truncate -3.8) returns –3 (truncate (/ 13 4)) returns 3 (truncate (/ -13 4)) returns –3 The last two examples in the list above show how truncate may be used to return the quotient of an integer division. Remainder, given two integer arguments, returns the remainder that results from dividing the first by the second. The sign of the result is the same as the sign of the first argument. For example, (remainder 13 4) returns 1 (remainder -13 4) returns –1 (since –3 times 4 plus –1 is –13) (remainder 13 -4) returns 1 (since –3 times –4 plus 1 is 13) (remainder -13 -4) returns –1 (since 3 times –4 plus –1 is –13) An application of truncate Truncate is useful for converting small units to larger ones. Suppose we wanted to de- termine, given some number of hours, how many days it represents. We simply divide by 24 and truncate: (define (number-of-days number-of-hours) (truncate (/ number-of-hours 24)) ) An application of remainder Remainder is especially useful in applications where values are cyclic, i.e. they move through a sequence of values and then back to the beginning. Time on a clock is an example of a cyclic application; the hours start at 1, go up to 12, and then back to 1.

20

Suppose that we want to find out where on the clock the “hour hand” will point, seven hours from now. We might decide to define a Scheme function called 7-hrs-later that would take the current hour as an argument: (define (7-hrs-later current-hour) ______) Current-hour is assumed to be an integer between 1 and 12, inclusive. What should the body of the function look like? One obvious part of the function is the of 7 to the current hour. To see what other operations are involved, it helps to make a table:

current-hour 123456789101112 (+ current-hour 7) 891011 12 13 14 15 16 17 18 19 desired result 8 9 10 11 12 1234567

We need a way to transform the values 13, 14, ... into correct time values. One way is to use the if . Another way, which doesn’t use any kind of conditional opera- tion, is to use remainder. A first try is to compute the remainder of current-hour + 7 when divided by 12:

(+ current-hour 7) 891011 12 13 14 15 16 17 18 19 (remainder 89101101234567 (+ current-hour 7) 12) desired result 8 9 10 11 12 1234567

This is almost what we want. The problem is that computing a remainder when di- viding by 12 always will yield a value between 0 and 11, and we need a value between 1 and 12. Thus we will need to add 1 to the remainder:

(+ current-hour 7) 891011 12 13 14 15 16 17 18 19 (remainder 89101101234567 (+ current-hour 7) 12) (+ 1 9101112 12345678 (remainder (+ current-hour 7) 12) ) desired result 8 9 10 11 12 1234567

21

This computation is shifted one element over in the table from the desired result. If, instead of current-hour, current-hour – 1 is used in the remainder computation, we get what we want. current-hour 123456789101112 (+ 7891011 12 13 14 15 16 17 18 (- current-hour 1) 7) (remainder 78910110123456 (+ (- current-hour 1) 7) 12) (+ 1 891011121234567 (remainder (+ (- current-hour 1) 7) 12)

22