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