]> pd.if.org Git - pdclib/blob - functions/stdlib/div.c
f1523fc99b5dda62db2c8afd9e9e01c622680aa2
[pdclib] / functions / stdlib / div.c
1 /* div( int, int )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <stdlib.h>
8
9 #ifndef REGTEST
10
11 div_t div( int numer, int denom )
12 {
13     div_t rc;
14     rc.quot = numer / denom;
15     rc.rem  = numer % denom;
16     /* TODO: pre-C99 compilers might require modulus corrections */
17     return rc;
18 }
19
20 #endif
21
22 #ifdef TEST
23 #include <_PDCLIB_test.h>
24
25 int main( void )
26 {
27     div_t result;
28     result = div( 5, 2 );
29     TESTCASE( result.quot == 2 && result.rem == 1 );
30     result = div( -5, 2 );
31     TESTCASE( result.quot == -2 && result.rem == -1 );
32     result = div( 5, -2 );
33     TESTCASE( result.quot == -2 && result.rem == 1 );
34     TESTCASE( sizeof( result.quot ) == sizeof( int ) );
35     TESTCASE( sizeof( result.rem )  == sizeof( int ) );
36     return TEST_RESULTS;
37 }
38
39 #endif