Fix: Print propagte warning only if GMX_GPU=on
[alexxy/gromacs.git] / src / gromacs / legacyheaders / maths.h
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.2.0
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2004, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * For more info, check our website at http://www.gromacs.org
32  * 
33  * And Hey:
34  * Gromacs Runs On Most of All Computer Systems
35  */
36
37 #ifndef _maths_h
38 #define _maths_h
39
40 #include <math.h>
41 #include "types/simple.h"
42 #include "typedefs.h"
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 #ifndef M_PI
49 #define M_PI            3.14159265358979323846
50 #endif
51
52 #ifndef M_PI_2
53 #define M_PI_2          1.57079632679489661923
54 #endif
55
56 #ifndef M_2PI
57 #define M_2PI           6.28318530717958647692
58 #endif
59     
60 #ifndef M_SQRT2
61 #define M_SQRT2 sqrt(2.0)
62 #endif
63
64 #ifndef M_1_PI
65 #define M_1_PI      0.31830988618379067154
66 #endif
67
68 int             gmx_nint(real a);
69 real    sign(real x,real y);
70
71 int             gmx_nint(real a);
72 real    sign(real x,real y);
73 real    cuberoot (real a);
74 double  gmx_erfd(double x);
75 double  gmx_erfcd(double x);
76 float   gmx_erff(float x);
77 float   gmx_erfcf(float x);
78 #ifdef GMX_DOUBLE
79 #define gmx_erf(x)   gmx_erfd(x)
80 #define gmx_erfc(x)  gmx_erfcd(x)
81 #else
82 #define gmx_erf(x)   gmx_erff(x)
83 #define gmx_erfc(x)  gmx_erfcf(x)
84 #endif
85
86 gmx_bool gmx_isfinite(real x);
87
88 /*! \brief Check if two numbers are within a tolerance
89  *
90  *  This routine checks if the relative difference between two numbers is
91  *  approximately within the given tolerance, defined as
92  *  fabs(f1-f2)<=tolerance*fabs(f1+f2).
93  *
94  *  To check if two floating-point numbers are almost identical, use this routine 
95  *  with the tolerance GMX_REAL_EPS, or GMX_DOUBLE_EPS if the check should be
96  *  done in double regardless of Gromacs precision.
97  *  
98  *  To check if two algorithms produce similar results you will normally need
99  *  to relax the tolerance significantly since many operations (e.g. summation)
100  *  accumulate floating point errors.
101  *
102  *  \param f1  First number to compare
103  *  \param f2  Second number to compare
104  *  \param tol Tolerance to use
105  *
106  *  \return 1 if the relative difference is within tolerance, 0 if not.
107  */
108 static int
109 gmx_within_tol(double   f1,
110                double   f2,
111                double   tol)
112 {
113     /* The or-equal is important - otherwise we return false if f1==f2==0 */
114     if( fabs(f1-f2) <= tol*0.5*(fabs(f1)+fabs(f2)) )
115     {
116         return 1;
117     }
118     else
119     {
120         return 0;
121     }
122 }
123
124
125
126 /** 
127  * Check if a number is smaller than some preset safe minimum
128  * value, currently defined as GMX_REAL_MIN/GMX_REAL_EPS.
129  *
130  * If a number is smaller than this value we risk numerical overflow
131  * if any number larger than 1.0/GMX_REAL_EPS is divided by it.
132  *
133  * \return 1  if 'almost' numerically zero, 0 otherwise.
134  */
135 static int
136 gmx_numzero(double a)
137 {
138   return gmx_within_tol(a,0.0,GMX_REAL_MIN/GMX_REAL_EPS);
139 }
140
141
142 static real
143 gmx_log2(real x)
144 {
145   const real iclog2 = 1.0/log( 2.0 );
146
147     return log( x ) * iclog2;
148 }
149
150 /*! /brief Multiply two large ints
151  *
152  *  Returns true when overflow did not occur.
153  */
154 gmx_bool
155 check_int_multiply_for_overflow(gmx_large_int_t a,
156                                 gmx_large_int_t b,
157                                 gmx_large_int_t *result);
158
159 #ifdef __cplusplus
160 }
161 #endif
162
163 #endif  /* _maths_h */