xref: /dflybsd-src/contrib/cvs-1.12/lib/minmax.h (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /* MIN, MAX macros.
2*86d7f5d3SJohn Marino    Copyright (C) 1995, 1998, 2001, 2003, 2005 Free Software Foundation, Inc.
3*86d7f5d3SJohn Marino 
4*86d7f5d3SJohn Marino    This program is free software; you can redistribute it and/or modify
5*86d7f5d3SJohn Marino    it under the terms of the GNU General Public License as published by
6*86d7f5d3SJohn Marino    the Free Software Foundation; either version 2, or (at your option)
7*86d7f5d3SJohn Marino    any later version.
8*86d7f5d3SJohn Marino 
9*86d7f5d3SJohn Marino    This program is distributed in the hope that it will be useful,
10*86d7f5d3SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*86d7f5d3SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*86d7f5d3SJohn Marino    GNU General Public License for more details.
13*86d7f5d3SJohn Marino 
14*86d7f5d3SJohn Marino    You should have received a copy of the GNU General Public License
15*86d7f5d3SJohn Marino    along with this program; if not, write to the Free Software Foundation,
16*86d7f5d3SJohn Marino    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17*86d7f5d3SJohn Marino 
18*86d7f5d3SJohn Marino #ifndef _MINMAX_H
19*86d7f5d3SJohn Marino #define _MINMAX_H
20*86d7f5d3SJohn Marino 
21*86d7f5d3SJohn Marino /* Note: MIN, MAX are also defined in <sys/param.h> on some systems
22*86d7f5d3SJohn Marino    (glibc, IRIX, HP-UX, OSF/1).  Therefore you might get warnings about
23*86d7f5d3SJohn Marino    MIN, MAX macro redefinitions on some systems; the workaround is to
24*86d7f5d3SJohn Marino    #include this file as the last one among the #include list.  */
25*86d7f5d3SJohn Marino 
26*86d7f5d3SJohn Marino /* Before we define the following symbols we get the <limits.h> file
27*86d7f5d3SJohn Marino    since otherwise we get redefinitions on some systems if <limits.h> is
28*86d7f5d3SJohn Marino    included after this file.  Likewise for <sys/param.h>.
29*86d7f5d3SJohn Marino    If more than one of these system headers define MIN and MAX, pick just
30*86d7f5d3SJohn Marino    one of the headers (because the definitions most likely are the same).  */
31*86d7f5d3SJohn Marino #if HAVE_MINMAX_IN_LIMITS_H
32*86d7f5d3SJohn Marino # include <limits.h>
33*86d7f5d3SJohn Marino #elif HAVE_MINMAX_IN_SYS_PARAM_H
34*86d7f5d3SJohn Marino # include <sys/param.h>
35*86d7f5d3SJohn Marino #endif
36*86d7f5d3SJohn Marino 
37*86d7f5d3SJohn Marino /* Note: MIN and MAX should be used with two arguments of the
38*86d7f5d3SJohn Marino    same type.  They might not return the minimum and maximum of their two
39*86d7f5d3SJohn Marino    arguments, if the arguments have different types or have unusual
40*86d7f5d3SJohn Marino    floating-point values.  For example, on a typical host with 32-bit 'int',
41*86d7f5d3SJohn Marino    64-bit 'long long', and 64-bit IEEE 754 'double' types:
42*86d7f5d3SJohn Marino 
43*86d7f5d3SJohn Marino      MAX (-1, 2147483648) returns 4294967295.
44*86d7f5d3SJohn Marino      MAX (9007199254740992.0, 9007199254740993) returns 9007199254740992.0.
45*86d7f5d3SJohn Marino      MAX (NaN, 0.0) returns 0.0.
46*86d7f5d3SJohn Marino      MAX (+0.0, -0.0) returns -0.0.
47*86d7f5d3SJohn Marino 
48*86d7f5d3SJohn Marino    and in each case the answer is in some sense bogus.  */
49*86d7f5d3SJohn Marino 
50*86d7f5d3SJohn Marino /* MAX(a,b) returns the maximum of A and B.  */
51*86d7f5d3SJohn Marino #ifndef MAX
52*86d7f5d3SJohn Marino # define MAX(a,b) ((a) > (b) ? (a) : (b))
53*86d7f5d3SJohn Marino #endif
54*86d7f5d3SJohn Marino 
55*86d7f5d3SJohn Marino /* MIN(a,b) returns the minimum of A and B.  */
56*86d7f5d3SJohn Marino #ifndef MIN
57*86d7f5d3SJohn Marino # define MIN(a,b) ((a) < (b) ? (a) : (b))
58*86d7f5d3SJohn Marino #endif
59*86d7f5d3SJohn Marino 
60*86d7f5d3SJohn Marino #endif /* _MINMAX_H */
61