We've had a mix of min, max from libpspp/misc.h and MIN, MAX from
[pspp-builds.git] / src / libpspp / misc.h
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #if !math_misc_h
21 #define math_misc_h 1
22
23 #include <float.h>
24 #include <math.h>
25
26 #define EPSILON (10 * DBL_EPSILON)
27
28 /* HUGE_VAL is traditionally defined as positive infinity, or
29    alternatively, DBL_MAX. */
30 #if !HAVE_ISINF
31 #define isinf(X) (fabs (X) == HUGE_VAL)
32 #endif
33
34 /* A Not a Number is not equal to itself. */
35 #if !HAVE_ISNAN
36 #define isnan(X) ((X) != (X))
37 #endif
38
39 /* Finite numbers are not infinities or NaNs. */
40 #if !HAVE_FINITE
41 #define finite(X) (!isinf (X) && !isnan (X))
42 #elif HAVE_IEEEFP_H
43 #include <ieeefp.h>             /* Declares finite() under Solaris. */
44 #endif
45
46 /* Clamps A to be between B and C. */
47 #define range(A, B, C) ((A) < (B) ? (B) : ((A) > (C) ? (C) : (A)))
48
49 /* Divides nonnegative X by positive Y, rounding up. */
50 #define DIV_RND_UP(X, Y) (((X) + ((Y) - 1)) / (Y))
51
52 /* Returns nonnegative difference between {nonnegative X} and {the
53    least multiple of positive Y greater than or equal to X}. */
54 #define REM_RND_UP(X, Y) ((X) % (Y) ? (Y) - (X) % (Y) : 0)
55
56 /* Rounds X up to the next multiple of Y. */
57 #define ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y) * (Y))
58
59 /* Rounds X down to the previous multiple of Y. */
60 #define ROUND_DOWN(X, Y) ((X) / (Y) * (Y))
61
62 int intlog10 (unsigned);
63
64 /* Returns the square of X. */
65 static inline double
66 pow2 (double x) 
67 {
68   return x * x;
69 }
70
71 /* Returns the cube of X. */
72 static inline double
73 pow3 (double x) 
74 {
75   return x * x * x;
76 }
77
78 /* Returns the fourth power of X. */
79 static inline double
80 pow4 (double x) 
81 {
82   double y = x * x;
83   y *= y;
84   return y;
85 }
86
87 #endif /* math/misc.h */