minmax.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* MIN, MAX macros.
  2. Copyright (C) 1995, 1998, 2001, 2003, 2005, 2009-2021 Free Software
  3. Foundation, Inc.
  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 3, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, see <https://www.gnu.org/licenses/>. */
  14. #ifndef _MINMAX_H
  15. #define _MINMAX_H
  16. /* Note: MIN, MAX are also defined in <sys/param.h> on some systems
  17. (glibc, IRIX, HP-UX, OSF/1). Therefore you might get warnings about
  18. MIN, MAX macro redefinitions on some systems; the workaround is to
  19. #include this file as the last one among the #include list. */
  20. /* Before we define the following symbols we get the <limits.h> file
  21. since otherwise we get redefinitions on some systems if <limits.h> is
  22. included after this file. Likewise for <sys/param.h>.
  23. If more than one of these system headers define MIN and MAX, pick just
  24. one of the headers (because the definitions most likely are the same). */
  25. #if HAVE_MINMAX_IN_LIMITS_H
  26. # include <limits.h>
  27. #elif HAVE_MINMAX_IN_SYS_PARAM_H
  28. # include <sys/param.h>
  29. #endif
  30. /* Note: MIN and MAX should be used with two arguments of the
  31. same type. They might not return the minimum and maximum of their two
  32. arguments, if the arguments have different types or have unusual
  33. floating-point values. For example, on a typical host with 32-bit 'int',
  34. 64-bit 'long long', and 64-bit IEEE 754 'double' types:
  35. MAX (-1, 2147483648) returns 4294967295.
  36. MAX (9007199254740992.0, 9007199254740993) returns 9007199254740992.0.
  37. MAX (NaN, 0.0) returns 0.0.
  38. MAX (+0.0, -0.0) returns -0.0.
  39. and in each case the answer is in some sense bogus. */
  40. /* MAX(a,b) returns the maximum of A and B. */
  41. #ifndef MAX
  42. # define MAX(a,b) ((a) > (b) ? (a) : (b))
  43. #endif
  44. /* MIN(a,b) returns the minimum of A and B. */
  45. #ifndef MIN
  46. # define MIN(a,b) ((a) < (b) ? (a) : (b))
  47. #endif
  48. #endif /* _MINMAX_H */