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