div.c revision 1573
1168404Spjd/* 2168404Spjd * Copyright (c) 1990, 1993 3168404Spjd * The Regents of the University of California. All rights reserved. 4168404Spjd * 5168404Spjd * This code is derived from software contributed to Berkeley by 6168404Spjd * Chris Torek. 7168404Spjd * 8168404Spjd * Redistribution and use in source and binary forms, with or without 9168404Spjd * modification, are permitted provided that the following conditions 10168404Spjd * are met: 11168404Spjd * 1. Redistributions of source code must retain the above copyright 12168404Spjd * notice, this list of conditions and the following disclaimer. 13168404Spjd * 2. Redistributions in binary form must reproduce the above copyright 14168404Spjd * notice, this list of conditions and the following disclaimer in the 15168404Spjd * documentation and/or other materials provided with the distribution. 16168404Spjd * 3. All advertising materials mentioning features or use of this software 17168404Spjd * must display the following acknowledgement: 18168404Spjd * This product includes software developed by the University of 19168404Spjd * California, Berkeley and its contributors. 20168404Spjd * 4. Neither the name of the University nor the names of its contributors 21168404Spjd * may be used to endorse or promote products derived from this software 22219089Spjd * without specific prior written permission. 23249643Smm * 24168404Spjd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25168404Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26168404Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27168404Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28168404Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29168404Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30168404Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31168404Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32168404Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33168404Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34168404Spjd * SUCH DAMAGE. 35168404Spjd */ 36168404Spjd 37168404Spjd#if defined(LIBC_SCCS) && !defined(lint) 38168404Spjdstatic char sccsid[] = "@(#)div.c 8.1 (Berkeley) 6/4/93"; 39168404Spjd#endif /* LIBC_SCCS and not lint */ 40168404Spjd 41185029Spjd#include <stdlib.h> /* div_t */ 42168404Spjd 43169023Spjddiv_t 44168404Spjddiv(num, denom) 45168404Spjd int num, denom; 46168404Spjd{ 47168404Spjd div_t r; 48168404Spjd 49168404Spjd r.quot = num / denom; 50168404Spjd r.rem = num % denom; 51168404Spjd /* 52168404Spjd * The ANSI standard says that |r.quot| <= |n/d|, where 53168404Spjd * n/d is to be computed in infinite precision. In other 54185029Spjd * words, we should always truncate the quotient towards 55219089Spjd * 0, never -infinity. 56219089Spjd * 57168404Spjd * Machine division and remainer may work either way when 58185029Spjd * one or both of n or d is negative. If only one is 59168404Spjd * negative and r.quot has been truncated towards -inf, 60168404Spjd * r.rem will have the same sign as denom and the opposite 61185029Spjd * sign of num; if both are negative and r.quot has been 62185029Spjd * truncated towards -inf, r.rem will be positive (will 63185029Spjd * have the opposite sign of num). These are considered 64185029Spjd * `wrong'. 65185029Spjd * 66185029Spjd * If both are num and denom are positive, r will always 67185029Spjd * be positive. 68185029Spjd * 69185029Spjd * This all boils down to: 70185029Spjd * if num >= 0, but r.rem < 0, we got the wrong answer. 71185029Spjd * In that case, to get the right answer, add 1 to r.quot and 72185029Spjd * subtract denom from r.rem. 73185029Spjd */ 74185029Spjd if (num >= 0 && r.rem < 0) { 75185029Spjd r.quot++; 76185029Spjd r.rem -= denom; 77185029Spjd } 78185029Spjd return (r); 79185029Spjd} 80185029Spjd