div.c revision 1573
167754Smsmith/* 267754Smsmith * Copyright (c) 1990, 1993 367754Smsmith * The Regents of the University of California. All rights reserved. 467754Smsmith * 567754Smsmith * This code is derived from software contributed to Berkeley by 667754Smsmith * Chris Torek. 7217365Sjkim * 8229989Sjkim * Redistribution and use in source and binary forms, with or without 970243Smsmith * modification, are permitted provided that the following conditions 1067754Smsmith * are met: 11217365Sjkim * 1. Redistributions of source code must retain the above copyright 12217365Sjkim * notice, this list of conditions and the following disclaimer. 13217365Sjkim * 2. Redistributions in binary form must reproduce the above copyright 14217365Sjkim * notice, this list of conditions and the following disclaimer in the 15217365Sjkim * documentation and/or other materials provided with the distribution. 16217365Sjkim * 3. All advertising materials mentioning features or use of this software 17217365Sjkim * must display the following acknowledgement: 18217365Sjkim * This product includes software developed by the University of 19217365Sjkim * California, Berkeley and its contributors. 20217365Sjkim * 4. Neither the name of the University nor the names of its contributors 21217365Sjkim * may be used to endorse or promote products derived from this software 22217365Sjkim * without specific prior written permission. 23217365Sjkim * 24217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2567754Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26217365Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27217365Sjkim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28217365Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2967754Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32217365Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33217365Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34217365Sjkim * SUCH DAMAGE. 35217365Sjkim */ 36217365Sjkim 37217365Sjkim#if defined(LIBC_SCCS) && !defined(lint) 38217365Sjkimstatic char sccsid[] = "@(#)div.c 8.1 (Berkeley) 6/4/93"; 39217365Sjkim#endif /* LIBC_SCCS and not lint */ 40217365Sjkim 41217365Sjkim#include <stdlib.h> /* div_t */ 42217365Sjkim 4367754Smsmithdiv_t 4467754Smsmithdiv(num, denom) 4567754Smsmith int num, denom; 4667754Smsmith{ 4767754Smsmith div_t r; 4867754Smsmith 4967754Smsmith r.quot = num / denom; 50117521Snjl r.rem = num % denom; 5167754Smsmith /* 5267754Smsmith * The ANSI standard says that |r.quot| <= |n/d|, where 5367754Smsmith * n/d is to be computed in infinite precision. In other 5467754Smsmith * words, we should always truncate the quotient towards 55102550Siwasaki * 0, never -infinity. 5691116Smsmith * 5791116Smsmith * Machine division and remainer may work either way when 5887031Smsmith * one or both of n or d is negative. If only one is 5967754Smsmith * negative and r.quot has been truncated towards -inf, 6067754Smsmith * r.rem will have the same sign as denom and the opposite 6187031Smsmith * sign of num; if both are negative and r.quot has been 6267754Smsmith * truncated towards -inf, r.rem will be positive (will 6367754Smsmith * have the opposite sign of num). These are considered 6467754Smsmith * `wrong'. 65129684Snjl * 66129684Snjl * If both are num and denom are positive, r will always 67129684Snjl * be positive. 68138287Smarks * 69129684Snjl * This all boils down to: 70129684Snjl * if num >= 0, but r.rem < 0, we got the wrong answer. 71129684Snjl * In that case, to get the right answer, add 1 to r.quot and 72129684Snjl * subtract denom from r.rem. 73129684Snjl */ 74129684Snjl if (num >= 0 && r.rem < 0) { 75117521Snjl r.quot++; 76117521Snjl r.rem -= denom; 77151937Sjkim } 78117521Snjl return (r); 79151937Sjkim} 80151937Sjkim