Edinburgh Speech Tools  2.1-release
EST_math.h
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1994,1995,1996 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author : Alan W Black */
34 /* Date : August 1996 */
35 /*-----------------------------------------------------------------------*/
36 /* OS system dependent math routines */
37 /* You may use this instead of math.h to get a system independent */
38 /* interface to the math functions (or include in addition, it's up to */
39 /* you) */
40 /*=======================================================================*/
41 #ifndef __EST_MATH_H__
42 #define __EST_MATH_H__
43 
44 #if defined(__APPLE__)
45 /* Not sure why I need this here, but I do */
46 extern "C" int isnan(double);
47 #endif
48 
49 /* this isn't included from c, but just to be safe... */
50 #ifdef __cplusplus
51 #include <cmath>
52 #include <climits>
53 #include <cfloat>
54 #else
55 #include <math.h>
56 #include <limits.h>
57 #include <float.h>
58 #endif
59 
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 /* Although isnan(double) exists on all machine isnanf(float) does not */
66 /* Automatic conversion between floats to doubles for out of range */
67 /* values in ANSI is undefined so we can't depend on that, but I */
68 
69 /* Solaris 2.X and SGIs IRIX*/
70 #if defined(__svr4__) || defined(__SYSTYPE_SVR4__)
71 #include <ieeefp.h>
72 #endif
73 
74 /* SunOS 4.1.X */
75 /* It doesn't exist on SunOS. One could use the macro that Solaris uses */
76 /* but I can't including it here, besides the follow will almost definitely */
77 /* have the same effect */
78 /* The defines are of course heuristics, this fails for NetBSD */
79 #if defined(__sun__) && defined(__sparc__) && !defined(__svr4__)
80 #define isnanf(X) isnan(X)
81 #endif
82 
83 /* Linux (and presumably Hurd too as Linux is GNU libc based) */
84 /* Sorry I haven't confirmed this cpp symbol yet */
85 #if defined(linux)
86 #define isnanf(X) __isnanf(X)
87 #endif
88 
89 /* OS/2 with gcc EMX */
90 #if defined(__EMX__)
91 #define isnanf(X) isnan(X)
92 #define finite(X) isfinite(X)
93 #endif
94 
95 /* AIX */
96 #if defined(_AIX)
97 #define isnanf(X) isnan(X)
98 #endif
99 
100 /* Apple OSX */
101 #if defined(__APPLE__)
102 #define isnanf(X) isnan((double)(X))
103 /* on some previous versions of OSX we seemed to need the following */
104 /* but not on 10.4 */
105 /* #define isnan(X) __isnan(X) */
106 #endif
107 
108 /* FreeBSD *and other 4.4 based systems require anything, isnanf is defined */
109 #if defined(__FreeBSD__)
110 
111 #endif
112 
113 /* Cygwin (at least cygwin 1.7 with gcc 4.3.4) */
114 #if defined(__CYGWIN__)
115 #if __GNUG__ > 3
116 #define isnanf(X) isnan(X)
117 #endif
118 #endif
119 
120 /* WIN32 has stupid names for things */
121 #if defined(SYSTEM_IS_WIN32)
122 #define isfinite(X) _finite(X)
123 #define finite(X) _finite(X)
124 #define round(X) win32_round(X)
125  inline double win32_round(double d) { return (d>0.0)?floor(d+0.5):ceil(d-0.5);}
126 #endif
127 
128 /* These are making assumptions about the under lying architecture */
129 /* that could be wrong (though most probably in a conservative way) */
130 #ifndef MAXFLOAT
131 #define MAXFLOAT ((float)3.0e+37)
132 #endif
133 #ifndef FLT_MAX
134 #define FLT_MAX ((float)3.0e+37)
135 #endif
136 #ifndef MINFLOAT
137 #define MINFLOAT ((float)1e-37)
138 #endif
139 #ifndef FLT_MAX
140 #define FLT_MIN ((float)1e-37)
141 #endif
142 
143 #ifndef PI
144 #define PI 3.14159265358979323846
145 #endif
146 #ifndef M_PI
147 #define M_PI PI
148 #endif
149 
150 #ifndef RAND_MAX
151 #define RAND_MAX 32767
152 #endif
153 
154 #define SAFE_LOG_ZERO -9538
155 
156 #define EST_NINT(X) ((int)((X)+0.5))
157 
158 inline double safe_log(const double x)
159 {
160  double l;
161  if (x == 0)
162  return SAFE_LOG_ZERO;
163  l=log(x);
164  if (l<SAFE_LOG_ZERO)
165  return SAFE_LOG_ZERO;
166  else
167  return l;
168 }
169 
170 inline double safe_exp(const double x)
171 {
172  if(x<=SAFE_LOG_ZERO)
173  return 0;
174  else
175  return exp(x);
176 }
177 
178 inline double safe_log10(const double x)
179 {
180  double l;
181  if (x == 0)
182  return SAFE_LOG_ZERO;
183  l=log10(x);
184  if(l<SAFE_LOG_ZERO)
185  return SAFE_LOG_ZERO;
186  else
187  return l;
188 }
189 
190 inline double safe_exp10(const double x)
191 {
192  if(x<=SAFE_LOG_ZERO)
193  return 0;
194  else
195  return pow(10.0,x);
196 }
197 
198 
199 #ifdef __cplusplus
200 }
201 #endif
202 
203 
204 #endif /*__EST_CUTILS_H__ */
double safe_log(const double x)
Definition: EST_math.h:158
double safe_exp10(const double x)
Definition: EST_math.h:190
double safe_log10(const double x)
Definition: EST_math.h:178
double safe_exp(const double x)
Definition: EST_math.h:170
#define SAFE_LOG_ZERO
Definition: EST_math.h:154
#define isnan(N)