1 /* 2 FUNCTION 3 <<div>>---divide two integers 4 5 INDEX 6 div 7 8 ANSI_SYNOPSIS 9 #include <stdlib.h> 10 div_t div(int <[n]>, int <[d]>); 11 12 TRAD_SYNOPSIS 13 #include <stdlib.h> 14 div_t div(<[n]>, <[d]>) 15 int <[n]>, <[d]>; 16 17 DESCRIPTION 18 Divide 19 @tex 20 $n/d$, 21 @end tex 22 @ifnottex 23 <[n]>/<[d]>, 24 @end ifnottex 25 returning quotient and remainder as two integers in a structure <<div_t>>. 26 27 RETURNS 28 The result is represented with the structure 29 30 . typedef struct 31 . { 32 . int quot; 33 . int rem; 34 . } div_t; 35 36 where the <<quot>> field represents the quotient, and <<rem>> the 37 remainder. For nonzero <[d]>, if `<<<[r]> = div(<[n]>,<[d]>);>>' then 38 <[n]> equals `<<<[r]>.rem + <[d]>*<[r]>.quot>>'. 39 40 To divide <<long>> rather than <<int>> values, use the similar 41 function <<ldiv>>. 42 43 PORTABILITY 44 <<div>> is ANSI. 45 46 No supporting OS subroutines are required. 47 */ 48 49 /* 50 * Copyright (c) 1990 Regents of the University of California. 51 * All rights reserved. 52 * 53 * This code is derived from software contributed to Berkeley by 54 * Chris Torek. 55 * 56 * Redistribution and use in source and binary forms, with or without 57 * modification, are permitted provided that the following conditions 58 * are met: 59 * 1. Redistributions of source code must retain the above copyright 60 * notice, this list of conditions and the following disclaimer. 61 * 2. Redistributions in binary form must reproduce the above copyright 62 * notice, this list of conditions and the following disclaimer in the 63 * documentation and/or other materials provided with the distribution. 64 * 3. All advertising materials mentioning features or use of this software 65 * must display the following acknowledgement: 66 * This product includes software developed by the University of 67 * California, Berkeley and its contributors. 68 * 4. Neither the name of the University nor the names of its contributors 69 * may be used to endorse or promote products derived from this software 70 * without specific prior written permission. 71 * 72 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 73 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 74 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 75 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 76 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 77 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 78 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 79 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 80 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 81 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 82 * SUCH DAMAGE. 83 */ 84 85 #include <_ansi.h> 86 #include <stdlib.h> /* div_t */ 87 88 div_t 89 _DEFUN (div, (num, denom), 90 int num _AND 91 int denom) 92 { 93 div_t r; 94 95 r.quot = num / denom; 96 r.rem = num % denom; 97 /* 98 * The ANSI standard says that |r.quot| <= |n/d|, where 99 * n/d is to be computed in infinite precision. In other 100 * words, we should always truncate the quotient towards 101 * 0, never -infinity or +infinity. 102 * 103 * Machine division and remainer may work either way when 104 * one or both of n or d is negative. If only one is 105 * negative and r.quot has been truncated towards -inf, 106 * r.rem will have the same sign as denom and the opposite 107 * sign of num; if both are negative and r.quot has been 108 * truncated towards -inf, r.rem will be positive (will 109 * have the opposite sign of num). These are considered 110 * `wrong'. 111 * 112 * If both are num and denom are positive, r will always 113 * be positive. 114 * 115 * This all boils down to: 116 * if num >= 0, but r.rem < 0, we got the wrong answer. 117 * In that case, to get the right answer, add 1 to r.quot and 118 * subtract denom from r.rem. 119 * if num < 0, but r.rem > 0, we also have the wrong answer. 120 * In this case, to get the right answer, subtract 1 from r.quot and 121 * add denom to r.rem. 122 */ 123 if (num >= 0 && r.rem < 0) { 124 ++r.quot; 125 r.rem -= denom; 126 } 127 else if (num < 0 && r.rem > 0) { 128 --r.quot; 129 r.rem += denom; 130 } 131 return (r); 132 } 133