Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site mips.UUCP Path: utzoo!watmath!clyde!burl!ulysses!bellcore!decvax!decwrl!glacier!mips!earl From: earl@mips.UUCP (Earl Killian) Newsgroups: net.math,net.lang.c,net.arch Subject: Re: Integer division Message-ID: <322@mips.UUCP> Date: Thu, 6-Feb-86 16:03:42 EST Article-I.D.: mips.322 Posted: Thu Feb 6 16:03:42 1986 Date-Received: Sun, 9-Feb-86 05:47:29 EST References: <367@mcgill-vision.UUCP> Organization: MIPS Computer Systems, Mountain View, CA Lines: 23 Xref: watmath net.math:2806 net.lang.c:7797 net.arch:2482 Of the languages I'm familiar with, I believe integer division is best handled in Common Lisp. There are four functions of two arguments: trunc, floor, ceil, and round. Each divides the first argument by the second and then rounds the result to an integer using round to zero, round to -infinity, round to +infinity, and round to nearest respectively. The second return value is the remainder of that division. Thus: (trunc 7 3) => 2, 1 ; 2*3 + 1 = 7 (trunc -7 3) => -2, -1 ; -2*3 + -1 = -7 (floor 7 3) => 2, 1 ; 2*3 + 1 = 7 (floor -7 3) => -3, 2 ; -3*3 + 2 = -7 (ceil 7 3) => 3, -2 ; 3*3 + -2 = 7 (ceil -7 3) => -2, -1 ; -2*3 + -1 = -7 (round 7 3) => 2, 1 ; 2*3 + 1 = 7 (round -7 3) => -2, -1 ; -2*3 + -1 = -7 The programmer picks what is appropriate. I have found floor and ceil to be the most useful, and trunc somewhat less. I have never used round. Actually these are also functions of one argument: (floor 3.5) => 3 0.5, etc.