Merge commit 'origin/stable'
[pspp-builds.git] / src / libpspp / misc.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 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 3 of the License, or
7    (at your option) 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, see <http://www.gnu.org/licenses/>. */
16
17 #if !libpspp_misc_h
18 #define libpspp_misc_h 1
19
20 #include <float.h>
21 #include <math.h>
22
23 #define EPSILON (10 * DBL_EPSILON)
24
25 /* Divides nonnegative X by positive Y, rounding up. */
26 #define DIV_RND_UP(X, Y) (((X) + ((Y) - 1)) / (Y))
27
28 /* Returns nonnegative difference between {nonnegative X} and {the
29    least multiple of positive Y greater than or equal to X}. */
30 #define REM_RND_UP(X, Y) ((X) % (Y) ? (Y) - (X) % (Y) : 0)
31
32 /* Rounds X up to the next multiple of Y. */
33 #define ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y) * (Y))
34
35 /* Rounds X down to the previous multiple of Y. */
36 #define ROUND_DOWN(X, Y) ((X) / (Y) * (Y))
37
38 int intlog10 (unsigned);
39
40 /* Returns the square of X. */
41 static inline double
42 pow2 (double x)
43 {
44   return x * x;
45 }
46
47 /* Returns the cube of X. */
48 static inline double
49 pow3 (double x)
50 {
51   return x * x * x;
52 }
53
54 /* Returns the fourth power of X. */
55 static inline double
56 pow4 (double x)
57 {
58   double y = x * x;
59   y *= y;
60   return y;
61 }
62
63 /* Set *DEST to the lower of *DEST and SRC */
64 static inline void
65 minimize (double *dest, double src)
66 {
67   if (src < *dest)
68     *dest = src;
69 }
70
71
72 /* Set *DEST to the greater of *DEST and SRC */
73 static inline void
74 maximize (double *dest, double src)
75 {
76   if (src > *dest)
77     *dest = src;
78 }
79
80
81 /* Set *DEST to the lower of *DEST and SRC */
82 static inline void
83 minimize_int (int *dest, int src)
84 {
85   if (src < *dest)
86     *dest = src;
87 }
88
89
90 /* Set *DEST to the greater of *DEST and SRC */
91 static inline void
92 maximize_int (int *dest, int src)
93 {
94   if (src > *dest)
95     *dest = src;
96 }
97
98
99 #endif /* libpspp/misc.h */