Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
EST_math.h
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 using namespace std;
61 
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65 
66 /* Although isnan(double) exists on all machine isnanf(float) does not */
67 /* Automatic conversion between floats to doubles for out of range */
68 /* values in ANSI is undefined so we can't depend on that, but I */
69 
70 /* Solaris 2.X and SGIs IRIX*/
71 #if defined(__svr4__) || defined(__SYSTYPE_SVR4__)
72 #include <ieeefp.h>
73 #endif
74 
75 /* SunOS 4.1.X */
76 /* It doesn't exist on SunOS. One could use the macro that Solaris uses */
77 /* but I can't including it here, besides the follow will almost definitely */
78 /* have the same effect */
79 /* The defines are of course heuristics, this fails for NetBSD */
80 #if defined(__sun__) && defined(__sparc__) && !defined(__svr4__)
81 #define isnanf(X) isnan(X)
82 #endif
83 
84 /* Linux (and presumably Hurd too as Linux is GNU libc based) */
85 /* Sorry I haven't confirmed this cpp symbol yet */
86 #if defined(linux)
87 #define isnanf(X) __isnanf(X)
88 #endif
89 
90 /* OS/2 with gcc EMX */
91 #if defined(__EMX__)
92 #define isnanf(X) isnan(X)
93 #define finite(X) isfinite(X)
94 #endif
95 
96 /* AIX */
97 #if defined(_AIX)
98 #define isnanf(X) isnan(X)
99 #endif
100 
101 /* Apple OSX */
102 #if defined(__APPLE__)
103 #define isnanf(X) isnan((double)(X))
104 /* on some previous versions of OSX we seemed to need the following */
105 /* but not on 10.4 */
106 /* #define isnan(X) __isnan(X) */
107 #endif
108 
109 /* FreeBSD *and other 4.4 based systems require anything, isnanf is defined */
110 #if defined(__FreeBSD__)
111 
112 #endif
113 
114 /* Cygwin (at least cygwin 1.7 with gcc 4.3.4) */
115 #if defined(__CYGWIN__)
116 #if __GNUG__ > 3
117 #define isnanf(X) isnan(X)
118 #endif
119 #endif
120 
121 /* WIN32 has stupid names for things */
122 #if defined(SYSTEM_IS_WIN32)
123 #define isfinite(X) _finite(X)
124 #define finite(X) _finite(X)
125 #define round(X) win32_round(X)
126  inline double win32_round(double d) { return (d>0.0)?floor(d+0.5):ceil(d-0.5);}
127 #endif
128 
129 /* These are making assumptions about the under lying architecture */
130 /* that could be wrong (though most probably in a conservative way) */
131 #ifndef MAXFLOAT
132 #define MAXFLOAT ((float)3.0e+37)
133 #endif
134 #ifndef FLT_MAX
135 #define FLT_MAX ((float)3.0e+37)
136 #endif
137 #ifndef MINFLOAT
138 #define MINFLOAT ((float)1e-37)
139 #endif
140 #ifndef FLT_MAX
141 #define FLT_MIN ((float)1e-37)
142 #endif
143 
144 #ifndef PI
145 #define PI 3.14159265358979323846
146 #endif
147 #ifndef M_PI
148 #define M_PI PI
149 #endif
150 
151 #ifndef RAND_MAX
152 #define RAND_MAX 32767
153 #endif
154 
155 #define SAFE_LOG_ZERO -9538
156 
157 #define EST_NINT(X) ((int)((X)+0.5))
158 
159 inline double safe_log(const double x)
160 {
161  double l;
162  if (x == 0)
163  return SAFE_LOG_ZERO;
164  l=log(x);
165  if (l<SAFE_LOG_ZERO)
166  return SAFE_LOG_ZERO;
167  else
168  return l;
169 }
170 
171 inline double safe_exp(const double x)
172 {
173  if(x<=SAFE_LOG_ZERO)
174  return 0;
175  else
176  return exp(x);
177 }
178 
179 inline double safe_log10(const double x)
180 {
181  double l;
182  if (x == 0)
183  return SAFE_LOG_ZERO;
184  l=log10(x);
185  if(l<SAFE_LOG_ZERO)
186  return SAFE_LOG_ZERO;
187  else
188  return l;
189 }
190 
191 inline double safe_exp10(const double x)
192 {
193  if(x<=SAFE_LOG_ZERO)
194  return 0;
195  else
196  return pow(10.0,x);
197 }
198 
199 
200 #ifdef __cplusplus
201 }
202 #endif
203 
204 
205 #endif /*__EST_CUTILS_H__ */