00001
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <errno.h>
00015 #include <math.h>
00016
00017 #include "CTB.hxx"
00018 #include "CTBnum.hxx"
00019 #include "CTBexceptionMath.hxx"
00020
00021
00026
00028
00034 double CTBsqrt(double x)
00035 {
00036 double f;
00037
00038 errno = 0;
00039 f = sqrt(x);
00040 if (errno == EDOM) throw CTBexceptionMathDomain(x,"CTBsqrt()");
00041
00042 return f;
00043 }
00044
00045
00047
00053 float CTBsqrt(float x)
00054 {
00055 float f;
00056
00057 errno = 0;
00058 f = sqrtf(x);
00059 if (errno == EDOM) throw CTBexceptionMathDomain(x,"CTBsqrt()");
00060
00061 return f;
00062 }
00063
00064
00066
00072 double CTBexp(double x)
00073 {
00074 double f;
00075
00076 errno = 0;
00077 f = exp(x);
00078 if (errno == ERANGE) throw CTBexceptionMathRange(x,"CTBexp()");
00079
00080 return f;
00081 }
00082
00083
00085
00091 float CTBexp(float x)
00092 {
00093 float f;
00094
00095 errno = 0;
00096 f = expf(x);
00097 if (errno == EDOM) throw CTBexceptionMathRange(x,"CTBexp()");
00098
00099 return f;
00100 }
00101
00102
00104
00111 double CTBlog(double x)
00112 {
00113 double f;
00114
00115 errno = 0;
00116 f = log(x);
00117 if (errno == EDOM) throw CTBexceptionMathDomain(x,"CTBlog()");
00118 if (errno == ERANGE) throw CTBexceptionMathRange(x,"CTBlog()");
00119
00120 return f;
00121 }
00122
00123
00125
00132 float CTBlog(float x)
00133 {
00134 float f;
00135
00136 errno = 0;
00137 f = logf(x);
00138 if (errno == EDOM) throw CTBexceptionMathDomain(x,"CTBlog()");
00139 if (errno == ERANGE) throw CTBexceptionMathRange(x,"CTBlog()");
00140
00141 return f;
00142 }
00143
00144
00146
00153 double CTBlog10(double x)
00154 {
00155 double f;
00156
00157 errno = 0;
00158 f = log10(x);
00159 if (errno == EDOM) throw CTBexceptionMathDomain(x,"CTBlog10()");
00160 if (errno == ERANGE) throw CTBexceptionMathRange(x,"CTBlog10()");
00161
00162 return f;
00163 }
00164
00165
00167
00174 float CTBlog10(float x)
00175 {
00176 float f;
00177
00178 errno = 0;
00179 f = log10f(x);
00180 if (errno == EDOM) throw CTBexceptionMathDomain(x,"CTBlog10()");
00181 if (errno == ERANGE) throw CTBexceptionMathRange(x,"CTBlog10()");
00182
00183 return f;
00184 }
00185
00186
00188
00194 double CTBasin(double x)
00195 {
00196 double f;
00197
00198 errno = 0;
00199 f = asin(x);
00200 if (errno == EDOM) throw CTBexceptionMathDomain(x,"CTBasin()");
00201
00202 return f;
00203 }
00204
00205
00207
00213 float CTBasin(float x)
00214 {
00215 float f;
00216
00217 errno = 0;
00218 f = asinf(x);
00219 if (errno == EDOM) throw CTBexceptionMathDomain(x,"CTBasin()");
00220
00221 return f;
00222 }
00223
00224
00226
00232 double CTBacos(double x)
00233 {
00234 double f;
00235
00236 errno = 0;
00237 f = acos(x);
00238 if (errno == EDOM) throw CTBexceptionMathDomain(x,"CTBacos()");
00239
00240 return f;
00241 }
00242
00243
00245
00251 float CTBacos(float x)
00252 {
00253 float f;
00254
00255 errno = 0;
00256 f = acosf(x);
00257 if (errno == EDOM) throw CTBexceptionMathDomain(x,"CTBacos()");
00258
00259 return f;
00260 }
00261
00262
00264
00267 double CTBpow(double x, int i_y)
00268 {
00269 double f = 1.;
00270 int i_nmul = CTBabs(i_y);
00271
00272 for (int i = 0; i < i_nmul; i++) f *= x;
00273 if (i_y < 0) f = 1./f;
00274
00275 return f;
00276 }
00277
00278
00280
00284 float CTBpow(float x, int i_y)
00285 {
00286 float f = 1.;
00287 int i_nmul = CTBabs(i_y);
00288
00289 for (int i = 0; i < i_nmul; i++) f *= x;
00290 if (i_y < 0) f = 1./f;
00291
00292 return f;
00293 }
00294
00295
00297
00301 CTBint CTBilog2(CTBint ix)
00302 {
00303 CTBint ir = 0;
00304 if (ix <= 0) return -1;
00305 while (ix > 1) {
00306 if (ix & 1) return -1;
00307 ix >>= 1;
00308 ir += 1;
00309 }
00310 return ir;
00311 }
00312
00313
00315
00327 double CTBpythag(double x, double y)
00328 {
00329 double hypo;
00330
00331 if (x == 0.) hypo = fabs(y);
00332 else if (y == 0.) hypo = fabs(x);
00333
00334 else {
00335 double absx = fabs(x);
00336 double absy = fabs(y);
00337
00338 if (absx > absy) {
00339 double fac = absy / absx;
00340 hypo = absx * sqrt(1. + fac*fac);
00341 } else {
00342 double fac = absx / absy;
00343 hypo = absy * sqrt(1. + fac*fac);
00344 }
00345 }
00346
00347 return hypo;
00348 }
00349
00350
00352
00356 float CTBpythag(float x, float y)
00357 {
00358 float hypo;
00359
00360 if (x == 0.) hypo = fabs(y);
00361 else if (y == 0.) hypo = fabs(x);
00362
00363 else {
00364 float absx = fabs(x);
00365 float absy = fabs(y);
00366
00367 if (absx > absy) {
00368 float fac = absy / absx;
00369 hypo = absx * sqrt(1. + fac*fac);
00370 } else {
00371 float fac = absx / absy;
00372 hypo = absy * sqrt(1. + fac*fac);
00373 }
00374 }
00375
00376 return hypo;
00377 }
00378
00379