8647c00f80e33870fc4e302600b9b701436a4d7a
[alexxy/gromacs.git] / src / gromacs / math / utilities.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifndef GMX_MATH_UTILITIES_H
38 #define GMX_MATH_UTILITIES_H
39
40 #include <limits.h>
41 #include <math.h>
42
43 #include "gromacs/utility/basedefinitions.h"
44 #include "gromacs/utility/real.h"
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 #ifndef M_PI
51 #define M_PI        3.14159265358979323846
52 #endif
53
54 #ifndef M_PI_2
55 #define M_PI_2      1.57079632679489661923
56 #endif
57
58 #ifndef M_2PI
59 #define M_2PI       6.28318530717958647692
60 #endif
61
62 #ifndef M_SQRT2
63 #define M_SQRT2 sqrt(2.0)
64 #endif
65
66 #ifndef M_1_PI
67 #define M_1_PI      0.31830988618379067154
68 #endif
69
70 #ifndef M_FLOAT_1_SQRTPI /* used in CUDA kernels */
71 /* 1.0 / sqrt(M_PI) */
72 #define M_FLOAT_1_SQRTPI 0.564189583547756f
73 #endif
74
75 #ifndef M_1_SQRTPI
76 /* 1.0 / sqrt(M_PI) */
77 #define M_1_SQRTPI 0.564189583547756
78 #endif
79
80 #ifndef M_2_SQRTPI
81 /* 2.0 / sqrt(M_PI) */
82 #define M_2_SQRTPI  1.128379167095513
83 #endif
84
85 int     gmx_nint(real a);
86 real    sign(real x, real y);
87
88 real    cuberoot (real a);
89 double  gmx_erfd(double x);
90 double  gmx_erfcd(double x);
91 float   gmx_erff(float x);
92 float   gmx_erfcf(float x);
93 #ifdef GMX_DOUBLE
94 #define gmx_erf(x)   gmx_erfd(x)
95 #define gmx_erfc(x)  gmx_erfcd(x)
96 #else
97 #define gmx_erf(x)   gmx_erff(x)
98 #define gmx_erfc(x)  gmx_erfcf(x)
99 #endif
100
101 #if defined(_MSC_VER) && _MSC_VER < 1800
102 #define gmx_expm1(x) (exp(x)-1)
103 #define gmx_log1p(x) log(1+x)
104 #else
105 #define gmx_expm1 expm1
106 #define gmx_log1p log1p
107 #endif
108
109 gmx_bool gmx_isfinite(real x);
110 gmx_bool gmx_isnan(real x);
111
112 /*! \brief Check if two numbers are within a tolerance
113  *
114  *  This routine checks if the relative difference between two numbers is
115  *  approximately within the given tolerance, defined as
116  *  fabs(f1-f2)<=tolerance*fabs(f1+f2).
117  *
118  *  To check if two floating-point numbers are almost identical, use this routine
119  *  with the tolerance GMX_REAL_EPS, or GMX_DOUBLE_EPS if the check should be
120  *  done in double regardless of Gromacs precision.
121  *
122  *  To check if two algorithms produce similar results you will normally need
123  *  to relax the tolerance significantly since many operations (e.g. summation)
124  *  accumulate floating point errors.
125  *
126  *  \param f1  First number to compare
127  *  \param f2  Second number to compare
128  *  \param tol Tolerance to use
129  *
130  *  \return 1 if the relative difference is within tolerance, 0 if not.
131  */
132 int
133 gmx_within_tol(double   f1,
134                double   f2,
135                double   tol);
136
137 /*!
138  * \brief Check if a number is smaller than some preset safe minimum
139  * value, currently defined as GMX_REAL_MIN/GMX_REAL_EPS.
140  *
141  * If a number is smaller than this value we risk numerical overflow
142  * if any number larger than 1.0/GMX_REAL_EPS is divided by it.
143  *
144  * \return 1  if 'almost' numerically zero, 0 otherwise.
145  */
146 int
147 gmx_numzero(double a);
148
149 /*! \brief Compute floor of logarithm to base 2
150  *
151  * \return log2(x)
152  */
153 unsigned int
154 gmx_log2i(unsigned int x);
155
156 /*! \brief Multiply two large ints
157  *
158  * \return False iff overflow occured
159  */
160 gmx_bool
161 check_int_multiply_for_overflow(gmx_int64_t  a,
162                                 gmx_int64_t  b,
163                                 gmx_int64_t *result);
164
165 /*! \brief Find greatest common divisor of two numbers
166  *
167  * \return GCD of the two inputs
168  */
169 int
170 gmx_greatest_common_divisor(int p, int q);
171
172 #ifdef __cplusplus
173 }
174 #endif
175
176 #endif