sl@0: /* sl@0: * FUNCTION sl@0: * <
>---divide two integers sl@0: * INDEX sl@0: * div sl@0: * ANSI_SYNOPSIS sl@0: * #include sl@0: * div_t div(int <[n]>, int <[d]>); sl@0: * TRAD_SYNOPSIS sl@0: * #include sl@0: * div_t div(<[n]>, <[d]>) sl@0: * int <[n]>, <[d]>; sl@0: * Divide sl@0: * @tex sl@0: * $n/d$, sl@0: * @end tex sl@0: * @ifinfo sl@0: * <[n]>/<[d]>, sl@0: * @end ifinfo sl@0: * returning quotient and remainder as two integers in a structure <>. sl@0: * RETURNS sl@0: * The result is represented with the structure sl@0: * . typedef struct sl@0: * . int quot; sl@0: * . int rem; sl@0: * . } div_t; sl@0: * where the <> field represents the quotient, and <> the sl@0: * remainder. For nonzero <[d]>, if `<<<[r]> = div(<[n]>,<[d]>);>>' then sl@0: * <[n]> equals `<<<[r]>.rem + <[d]>*<[r]>.quot>>'. sl@0: * To divide <> rather than <> values, use the similar sl@0: * function <>. sl@0: * PORTABILITY sl@0: * <
> is ANSI. sl@0: * No supporting OS subroutines are required. sl@0: * sl@0: * sl@0: */ sl@0: sl@0: sl@0: sl@0: /* sl@0: * Copyright (c) 1990 Regents of the University of California. sl@0: * All rights reserved. sl@0: * sl@0: * This code is derived from software contributed to Berkeley by sl@0: * Chris Torek. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * This product includes software developed by the University of sl@0: * California, Berkeley and its contributors. sl@0: * 4. Neither the name of the University nor the names of its contributors sl@0: * may be used to endorse or promote products derived from this software sl@0: * without specific prior written permission. sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: */ sl@0: sl@0: #include <_ansi.h> sl@0: #include /* div_t */ sl@0: sl@0: /** sl@0: Divide two integer values. sl@0: numer is divided by denom. sl@0: Quotient and remainder are returned in a div_t structure. sl@0: @return A div_t structure is returned sl@0: @param num Numerator. sl@0: @param denom Denominator. sl@0: */ sl@0: EXPORT_C div_t div (int num, int denom) sl@0: { sl@0: div_t r; sl@0: sl@0: r.quot = num / denom; sl@0: r.rem = num % denom; sl@0: /* sl@0: * The ANSI standard says that |r.quot| <= |n/d|, where sl@0: * n/d is to be computed in infinite precision. In other sl@0: * words, we should always truncate the quotient towards sl@0: * 0, never -infinity. sl@0: * sl@0: * Machine division and remainer may work either way when sl@0: * one or both of n or d is negative. If only one is sl@0: * negative and r.quot has been truncated towards -inf, sl@0: * r.rem will have the same sign as denom and the opposite sl@0: * sign of num; if both are negative and r.quot has been sl@0: * truncated towards -inf, r.rem will be positive (will sl@0: * have the opposite sign of num). These are considered sl@0: * `wrong'. sl@0: * sl@0: * If both are num and denom are positive, r will always sl@0: * be positive. sl@0: * sl@0: * This all boils down to: sl@0: * if num >= 0, but r.rem < 0, we got the wrong answer. sl@0: * In that case, to get the right answer, add 1 to r.quot and sl@0: * subtract denom from r.rem. sl@0: */ sl@0: if (num >= 0 && r.rem < 0) { sl@0: r.quot++; sl@0: r.rem -= denom; sl@0: } sl@0: return (r); sl@0: }