26b9a52639b3cafb7088e3c9690f132cc65cb99d
[pspp] / src / 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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, 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)                                \
32         (fabs (X) == HUGE_VAL)
33 #endif
34
35 /* A Not a Number is not equal to itself. */
36 #if !HAVE_ISNAN
37 #define isnan(X)                                \
38         ((X) != (X))
39 #endif
40
41 /* Finite numbers are not infinities or NaNs. */
42 #if !HAVE_FINITE
43 #define finite(X)                               \
44         (!isinf (X) && !isnan (X))
45 #elif HAVE_IEEEFP_H
46 #include <ieeefp.h>             /* Declares finite() under Solaris. */
47 #endif
48
49 #if __TURBOC__
50 #include <stdlib.h>             /* screwed-up Borland headers define min(), max(),
51                                    so we might as well let 'em */
52 #endif
53
54 #ifndef min
55 #if __GNUC__ && !__STRICT_ANSI__
56 #define min(A, B)                               \
57         ({                                      \
58           int _a = (A), _b = (B);               \
59           _a < _b ? _a : _b;                    \
60         })
61 #else /* !__GNUC__ */
62 #define min(A, B)                               \
63         ((A) < (B) ? (A) : (B))
64 #endif /* !__GNUC__ */
65 #endif /* !min */
66
67 #ifndef max
68 #if __GNUC__ && !__STRICT_ANSI__
69 #define max(A, B)                               \
70         ({                                      \
71           int _a = (A), _b = (B);               \
72           _a > _b ? _a : _b;                    \
73         })
74 #else /* !__GNUC__ */
75 #define max(A, B)                               \
76         ((A) > (B) ? (A) : (B))
77 #endif /* !__GNUC__ */
78 #endif /* !max */
79
80 /* Clamps A to be between B and C. */
81 #define range(A, B, C)                          \
82         ((A) < (B) ? (B) : ((A) > (C) ? (C) : (A)))
83
84 /* Divides nonnegative X by positive Y, rounding up. */
85 #define DIV_RND_UP(X, Y)                        \
86         (((X) + ((Y) - 1)) / (Y))
87
88 /* Returns nonnegative difference between {nonnegative X} and {the
89    least multiple of positive Y greater than or equal to X}. */
90 #if __GNUC__ && !__STRICT_ANSI__
91 #define REM_RND_UP(X, Y)                        \
92         ({                                      \
93           int rem = (X) % (Y);                  \
94           rem ? (Y) - rem : 0;                  \
95         })
96 #else
97 #define REM_RND_UP(X, Y)                        \
98         ((X) % (Y) ? (Y) - (X) % (Y) : 0)
99 #endif
100
101 /* Rounds X up to the next multiple of Y. */
102 #define ROUND_UP(X, Y)                          \
103         (((X) + ((Y) - 1)) / (Y) * (Y))
104
105 /* Rounds X down to the previous multiple of Y. */
106 #define ROUND_DOWN(X, Y)                        \
107         ((X) / (Y) * (Y))
108
109 int intlog10 (unsigned);
110
111 /* Returns the square of X. */
112 static inline double
113 pow2 (double x) 
114 {
115   return x * x;
116 }
117
118 /* Returns the cube of X. */
119 static inline double
120 pow3 (double x) 
121 {
122   return x * x * x;
123 }
124
125 /* Returns the fourth power of X. */
126 static inline double
127 pow4 (double x) 
128 {
129   double y = x * x;
130   y *= y;
131   return y;
132 }
133
134 #endif /* math/misc.h */