New module minmax.
[pspp] / lib / minmax.h
1 /* MIN, MAX macros.
2    Copyright (C) 1995, 1998, 2001, 2003 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #ifndef _MINMAX_H
19 #define _MINMAX_H
20
21 /* Before we define the following symbols we get the <limits.h> file
22    since otherwise we get redefinitions on some systems.  */
23 #include <limits.h>
24
25 /* Note: MIN and MAX should preferrably be used with two arguments of the
26    same type.  They might not return the minimum and maximum of their two
27    arguments, if the arguments have different types or have unusual
28    floating-point values.  For example, on a typical host with 32-bit 'int',
29    64-bit 'long long', and 64-bit IEEE 754 'double' types:
30
31      MAX (-1, 2147483648) returns 4294967295.
32      MAX (9007199254740992.0, 9007199254740993) returns 9007199254740992.0.
33      MAX (NaN, 0.0) returns 0.0.
34      MAX (+0.0, -0.0) returns -0.0.
35
36    and in each case the answer is in some sense bogus.  */
37
38 /* MAX(a,b) returns the maximum of A and B.  */
39 #ifndef MAX
40 # if __STDC__ && defined __GNUC__ && __GNUC__ >= 2
41 #  define MAX(a,b) (__extension__                                           \
42                      ({__typeof__ (a) _a = (a);                             \
43                        __typeof__ (b) _b = (b);                             \
44                        _a > _b ? _a : _b;                                   \
45                       }))
46 # else
47 #  define MAX(a,b) ((a) > (b) ? (a) : (b))
48 # endif
49 #endif
50
51 /* MIN(a,b) returns the minimum of A and B.  */
52 #ifndef MIN
53 # if __STDC__ && defined __GNUC__ && __GNUC__ >= 2
54 #  define MIN(a,b) (__extension__                                           \
55                      ({__typeof__ (a) _a = (a);                             \
56                        __typeof__ (b) _b = (b);                             \
57                        _a < _b ? _a : _b;                                   \
58                       }))
59 # else
60 #  define MIN(a,b) ((a) < (b) ? (a) : (b))
61 # endif
62 #endif
63
64 #endif /* _MINMAX_H */