Integer Division and Its Uses Integer division In elementary school, you learned how to divide one integer by another. Your solutions to drill-and-practice problems probably looked something like the following: 3 remainder 1 4 | 13 which means that when dividing 13 by 4, the quotient is 3 and the remainder is 1. Quotients and remainders 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 chocolate bars (although you might prefer to have 2.666). Similarly, 69 inches is more commonly 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 number. For example, (truncate (truncate (truncate (truncate 3.8) returns 3 -3.8) returns –3 (/ 13 4)) returns 3 (/ -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 (remainder (remainder (remainder 13 4) returns 1 -13 4) returns –1 (since –3 times 4 plus –1 is –13) 13 -4) returns 1 (since –3 times –4 plus 1 is 13) -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 determine, 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 addition of 7 to the current hour. To see what other operations are involved, it helps to make a table: current-hour 1 2 3 4 5 6 7 8 9 10 11 12 (+ current-hour 7) 8 9 10 11 12 13 14 15 16 17 18 19 desired result 8 9 10 11 12 1 2 3 4 5 6 7 We need a way to transform the values 13, 14, ... into correct time values. One way is to use the if operation. Another way, which doesn’t use any kind of conditional operation, is to use remainder. A first try is to compute the remainder of current-hour + 7 when divided by 12: (+ current-hour 7) 8 9 10 11 12 13 14 15 16 17 18 19 (remainder (+ current-hour 7) 12) 8 9 10 11 0 1 2 3 4 5 6 7 desired result 8 9 10 11 12 1 2 3 4 5 6 7 This is almost what we want. The problem is that computing a remainder when dividing 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) 8 9 10 11 12 13 14 15 16 17 18 19 (remainder (+ current-hour 7) 12) 8 9 10 11 0 1 2 3 4 5 6 7 (+ 1 (remainder (+ current-hour 7) 12) ) 9 10 11 12 1 2 3 4 5 6 7 8 desired result 8 9 10 11 12 1 2 3 4 5 6 7 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 1 2 3 4 5 6 7 8 9 10 11 12 (+ (- current-hour 1) 7) 7 8 9 10 11 12 13 14 15 16 17 18 (remainder (+ (- current-hour 1) 7) 12) 7 8 9 10 11 0 1 2 3 4 5 6 (+ 1 (remainder (+ (- current-hour 1) 7) 12) 8 9 10 11 12 1 2 3 4 5 6 7 22
© Copyright 2026 Paperzz