e7ec81265bda64d4c8169e5fe596bb7ee20456d8
[pspp-builds.git] / 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) (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 #ifndef min
47 #define min(A, B) ((A) < (B) ? (A) : (B))
48 #endif
49
50 #ifndef max
51 #define max(A, B) ((A) > (B) ? (A) : (B))
52 #endif
53
54 /* Clamps A to be between B and C. */
55 #define range(A, B, C) ((A) < (B) ? (B) : ((A) > (C) ? (C) : (A)))
56
57 /* Divides nonnegative X by positive Y, rounding up. */
58 #define DIV_RND_UP(X, Y) (((X) + ((Y) - 1)) / (Y))
59
60 /* Returns nonnegative difference between {nonnegative X} and {the
61    least multiple of positive Y greater than or equal to X}. */
62 #define REM_RND_UP(X, Y) ((X) % (Y) ? (Y) - (X) % (Y) : 0)
63
64 /* Rounds X up to the next multiple of Y. */
65 #define ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y) * (Y))
66
67 /* Rounds X down to the previous multiple of Y. */
68 #define ROUND_DOWN(X, Y) ((X) / (Y) * (Y))
69
70 int intlog10 (unsigned);
71
72 /* Returns the square of X. */
73 static inline double
74 pow2 (double x) 
75 {
76   return x * x;
77 }
78
79 /* Returns the cube of X. */
80 static inline double
81 pow3 (double x) 
82 {
83   return x * x * x;
84 }
85
86 /* Returns the fourth power of X. */
87 static inline double
88 pow4 (double x) 
89 {
90   double y = x * x;
91   y *= y;
92   return y;
93 }
94
95 #endif /* math/misc.h */