1 : // -*- C++ -*- C forwarding header.
2 :
3 : // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 : // 2006, 2007, 2008, 2009, 2010
5 : // Free Software Foundation, Inc.
6 : //
7 : // This file is part of the GNU ISO C++ Library. This library is free
8 : // software; you can redistribute it and/or modify it under the
9 : // terms of the GNU General Public License as published by the
10 : // Free Software Foundation; either version 3, or (at your option)
11 : // any later version.
12 :
13 : // This library is distributed in the hope that it will be useful,
14 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : // GNU General Public License for more details.
17 :
18 : // Under Section 7 of GPL version 3, you are granted additional
19 : // permissions described in the GCC Runtime Library Exception, version
20 : // 3.1, as published by the Free Software Foundation.
21 :
22 : // You should have received a copy of the GNU General Public License and
23 : // a copy of the GCC Runtime Library Exception along with this program;
24 : // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 : // <http://www.gnu.org/licenses/>.
26 :
27 : /** @file include/cmath
28 : * This is a Standard C++ Library file. You should @c \#include this file
29 : * in your programs, rather than any of the @a *.h implementation files.
30 : *
31 : * This is the C++ version of the Standard C Library header @c math.h,
32 : * and its contents are (mostly) the same as that header, but are all
33 : * contained in the namespace @c std (except for names which are defined
34 : * as macros in C).
35 : */
36 :
37 : //
38 : // ISO C++ 14882: 26.5 C library
39 : //
40 :
41 : #pragma GCC system_header
42 :
43 : #include <bits/c++config.h>
44 : #include <bits/cpp_type_traits.h>
45 : #include <ext/type_traits.h>
46 : #include <math.h>
47 :
48 : #ifndef _GLIBCXX_CMATH
49 : #define _GLIBCXX_CMATH 1
50 :
51 : // Get rid of those macros defined in <math.h> in lieu of real functions.
52 : #undef abs
53 : #undef div
54 : #undef acos
55 : #undef asin
56 : #undef atan
57 : #undef atan2
58 : #undef ceil
59 : #undef cos
60 : #undef cosh
61 : #undef exp
62 : #undef fabs
63 : #undef floor
64 : #undef fmod
65 : #undef frexp
66 : #undef ldexp
67 : #undef log
68 : #undef log10
69 : #undef modf
70 : #undef pow
71 : #undef sin
72 : #undef sinh
73 : #undef sqrt
74 : #undef tan
75 : #undef tanh
76 :
77 : _GLIBCXX_BEGIN_NAMESPACE(std)
78 :
79 : // Forward declaration of a helper function. This really should be
80 : // an `exported' forward declaration.
81 : template<typename _Tp>
82 : _Tp __cmath_power(_Tp, unsigned int);
83 :
84 : template<typename _Tp>
85 : inline _Tp
86 : __pow_helper(_Tp __x, int __n)
87 : {
88 : return __n < 0
89 : ? _Tp(1)/__cmath_power(__x, -__n)
90 : : __cmath_power(__x, __n);
91 : }
92 :
93 : inline double
94 : abs(double __x)
95 : { return __builtin_fabs(__x); }
96 :
97 : inline float
98 : abs(float __x)
99 : { return __builtin_fabsf(__x); }
100 :
101 : inline long double
102 : abs(long double __x)
103 : { return __builtin_fabsl(__x); }
104 :
105 : template<typename _Tp>
106 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
107 : double>::__type
108 0 : abs(_Tp __x)
109 0 : { return __builtin_fabs(__x); }
110 :
111 : using ::acos;
112 :
113 : inline float
114 : acos(float __x)
115 : { return __builtin_acosf(__x); }
116 :
117 : inline long double
118 : acos(long double __x)
119 : { return __builtin_acosl(__x); }
120 :
121 : template<typename _Tp>
122 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
123 : double>::__type
124 : acos(_Tp __x)
125 : { return __builtin_acos(__x); }
126 :
127 : using ::asin;
128 :
129 : inline float
130 : asin(float __x)
131 : { return __builtin_asinf(__x); }
132 :
133 : inline long double
134 : asin(long double __x)
135 : { return __builtin_asinl(__x); }
136 :
137 : template<typename _Tp>
138 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
139 : double>::__type
140 : asin(_Tp __x)
141 : { return __builtin_asin(__x); }
142 :
143 : using ::atan;
144 :
145 : inline float
146 : atan(float __x)
147 : { return __builtin_atanf(__x); }
148 :
149 : inline long double
150 : atan(long double __x)
151 : { return __builtin_atanl(__x); }
152 :
153 : template<typename _Tp>
154 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
155 : double>::__type
156 : atan(_Tp __x)
157 : { return __builtin_atan(__x); }
158 :
159 : using ::atan2;
160 :
161 : inline float
162 : atan2(float __y, float __x)
163 : { return __builtin_atan2f(__y, __x); }
164 :
165 : inline long double
166 : atan2(long double __y, long double __x)
167 : { return __builtin_atan2l(__y, __x); }
168 :
169 : template<typename _Tp, typename _Up>
170 : inline
171 : typename __gnu_cxx::__promote_2<
172 : typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
173 : && __is_arithmetic<_Up>::__value,
174 : _Tp>::__type, _Up>::__type
175 : atan2(_Tp __y, _Up __x)
176 : {
177 : typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
178 : return atan2(__type(__y), __type(__x));
179 : }
180 :
181 : using ::ceil;
182 :
183 : inline float
184 : ceil(float __x)
185 : { return __builtin_ceilf(__x); }
186 :
187 : inline long double
188 : ceil(long double __x)
189 : { return __builtin_ceill(__x); }
190 :
191 : template<typename _Tp>
192 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
193 : double>::__type
194 : ceil(_Tp __x)
195 : { return __builtin_ceil(__x); }
196 :
197 : using ::cos;
198 :
199 : inline float
200 0 : cos(float __x)
201 0 : { return __builtin_cosf(__x); }
202 :
203 : inline long double
204 : cos(long double __x)
205 : { return __builtin_cosl(__x); }
206 :
207 : template<typename _Tp>
208 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
209 : double>::__type
210 : cos(_Tp __x)
211 : { return __builtin_cos(__x); }
212 :
213 : using ::cosh;
214 :
215 : inline float
216 : cosh(float __x)
217 : { return __builtin_coshf(__x); }
218 :
219 : inline long double
220 : cosh(long double __x)
221 : { return __builtin_coshl(__x); }
222 :
223 : template<typename _Tp>
224 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
225 : double>::__type
226 : cosh(_Tp __x)
227 : { return __builtin_cosh(__x); }
228 :
229 : using ::exp;
230 :
231 : inline float
232 : exp(float __x)
233 : { return __builtin_expf(__x); }
234 :
235 : inline long double
236 : exp(long double __x)
237 : { return __builtin_expl(__x); }
238 :
239 : template<typename _Tp>
240 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
241 : double>::__type
242 : exp(_Tp __x)
243 : { return __builtin_exp(__x); }
244 :
245 : using ::fabs;
246 :
247 : inline float
248 0 : fabs(float __x)
249 0 : { return __builtin_fabsf(__x); }
250 :
251 : inline long double
252 : fabs(long double __x)
253 : { return __builtin_fabsl(__x); }
254 :
255 : template<typename _Tp>
256 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
257 : double>::__type
258 : fabs(_Tp __x)
259 : { return __builtin_fabs(__x); }
260 :
261 : using ::floor;
262 :
263 : inline float
264 : floor(float __x)
265 : { return __builtin_floorf(__x); }
266 :
267 : inline long double
268 : floor(long double __x)
269 : { return __builtin_floorl(__x); }
270 :
271 : template<typename _Tp>
272 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
273 : double>::__type
274 : floor(_Tp __x)
275 : { return __builtin_floor(__x); }
276 :
277 : using ::fmod;
278 :
279 : inline float
280 : fmod(float __x, float __y)
281 : { return __builtin_fmodf(__x, __y); }
282 :
283 : inline long double
284 : fmod(long double __x, long double __y)
285 : { return __builtin_fmodl(__x, __y); }
286 :
287 : using ::frexp;
288 :
289 : inline float
290 : frexp(float __x, int* __exp)
291 : { return __builtin_frexpf(__x, __exp); }
292 :
293 : inline long double
294 : frexp(long double __x, int* __exp)
295 : { return __builtin_frexpl(__x, __exp); }
296 :
297 : template<typename _Tp>
298 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
299 : double>::__type
300 : frexp(_Tp __x, int* __exp)
301 : { return __builtin_frexp(__x, __exp); }
302 :
303 : using ::ldexp;
304 :
305 : inline float
306 : ldexp(float __x, int __exp)
307 : { return __builtin_ldexpf(__x, __exp); }
308 :
309 : inline long double
310 : ldexp(long double __x, int __exp)
311 : { return __builtin_ldexpl(__x, __exp); }
312 :
313 : template<typename _Tp>
314 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
315 : double>::__type
316 : ldexp(_Tp __x, int __exp)
317 : { return __builtin_ldexp(__x, __exp); }
318 :
319 : using ::log;
320 :
321 : inline float
322 : log(float __x)
323 : { return __builtin_logf(__x); }
324 :
325 : inline long double
326 : log(long double __x)
327 : { return __builtin_logl(__x); }
328 :
329 : template<typename _Tp>
330 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
331 : double>::__type
332 : log(_Tp __x)
333 : { return __builtin_log(__x); }
334 :
335 : using ::log10;
336 :
337 : inline float
338 : log10(float __x)
339 : { return __builtin_log10f(__x); }
340 :
341 : inline long double
342 : log10(long double __x)
343 : { return __builtin_log10l(__x); }
344 :
345 : template<typename _Tp>
346 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
347 : double>::__type
348 : log10(_Tp __x)
349 : { return __builtin_log10(__x); }
350 :
351 : using ::modf;
352 :
353 : inline float
354 : modf(float __x, float* __iptr)
355 : { return __builtin_modff(__x, __iptr); }
356 :
357 : inline long double
358 : modf(long double __x, long double* __iptr)
359 : { return __builtin_modfl(__x, __iptr); }
360 :
361 : using ::pow;
362 :
363 : inline float
364 : pow(float __x, float __y)
365 : { return __builtin_powf(__x, __y); }
366 :
367 : inline long double
368 : pow(long double __x, long double __y)
369 : { return __builtin_powl(__x, __y); }
370 :
371 : #ifndef __GXX_EXPERIMENTAL_CXX0X__
372 : // _GLIBCXX_RESOLVE_LIB_DEFECTS
373 : // DR 550. What should the return type of pow(float,int) be?
374 : inline double
375 : pow(double __x, int __i)
376 : { return __builtin_powi(__x, __i); }
377 :
378 : inline float
379 : pow(float __x, int __n)
380 : { return __builtin_powif(__x, __n); }
381 :
382 : inline long double
383 : pow(long double __x, int __n)
384 : { return __builtin_powil(__x, __n); }
385 : #endif
386 :
387 : template<typename _Tp, typename _Up>
388 : inline
389 : typename __gnu_cxx::__promote_2<
390 : typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
391 : && __is_arithmetic<_Up>::__value,
392 : _Tp>::__type, _Up>::__type
393 : pow(_Tp __x, _Up __y)
394 : {
395 : typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
396 : return pow(__type(__x), __type(__y));
397 : }
398 :
399 : using ::sin;
400 :
401 : inline float
402 0 : sin(float __x)
403 0 : { return __builtin_sinf(__x); }
404 :
405 : inline long double
406 : sin(long double __x)
407 : { return __builtin_sinl(__x); }
408 :
409 : template<typename _Tp>
410 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
411 : double>::__type
412 : sin(_Tp __x)
413 : { return __builtin_sin(__x); }
414 :
415 : using ::sinh;
416 :
417 : inline float
418 : sinh(float __x)
419 : { return __builtin_sinhf(__x); }
420 :
421 : inline long double
422 : sinh(long double __x)
423 : { return __builtin_sinhl(__x); }
424 :
425 : template<typename _Tp>
426 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
427 : double>::__type
428 : sinh(_Tp __x)
429 : { return __builtin_sinh(__x); }
430 :
431 : using ::sqrt;
432 :
433 : inline float
434 : sqrt(float __x)
435 : { return __builtin_sqrtf(__x); }
436 :
437 : inline long double
438 : sqrt(long double __x)
439 : { return __builtin_sqrtl(__x); }
440 :
441 : template<typename _Tp>
442 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
443 : double>::__type
444 : sqrt(_Tp __x)
445 : { return __builtin_sqrt(__x); }
446 :
447 : using ::tan;
448 :
449 : inline float
450 0 : tan(float __x)
451 0 : { return __builtin_tanf(__x); }
452 :
453 : inline long double
454 : tan(long double __x)
455 : { return __builtin_tanl(__x); }
456 :
457 : template<typename _Tp>
458 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
459 : double>::__type
460 : tan(_Tp __x)
461 : { return __builtin_tan(__x); }
462 :
463 : using ::tanh;
464 :
465 : inline float
466 : tanh(float __x)
467 : { return __builtin_tanhf(__x); }
468 :
469 : inline long double
470 : tanh(long double __x)
471 : { return __builtin_tanhl(__x); }
472 :
473 : template<typename _Tp>
474 : inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
475 : double>::__type
476 : tanh(_Tp __x)
477 : { return __builtin_tanh(__x); }
478 :
479 : _GLIBCXX_END_NAMESPACE
480 :
481 : #if _GLIBCXX_USE_C99_MATH
482 : #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
483 :
484 : // These are possible macros imported from C99-land.
485 : #undef fpclassify
486 : #undef isfinite
487 : #undef isinf
488 : #undef isnan
489 : #undef isnormal
490 : #undef signbit
491 : #undef isgreater
492 : #undef isgreaterequal
493 : #undef isless
494 : #undef islessequal
495 : #undef islessgreater
496 : #undef isunordered
497 :
498 : _GLIBCXX_BEGIN_NAMESPACE(std)
499 :
500 : template<typename _Tp>
501 : inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
502 : int>::__type
503 : fpclassify(_Tp __f)
504 : {
505 : typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
506 : return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
507 : FP_SUBNORMAL, FP_ZERO, __type(__f));
508 : }
509 :
510 : template<typename _Tp>
511 : inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
512 : int>::__type
513 : isfinite(_Tp __f)
514 : {
515 : typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
516 : return __builtin_isfinite(__type(__f));
517 : }
518 :
519 : template<typename _Tp>
520 : inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
521 : int>::__type
522 : isinf(_Tp __f)
523 : {
524 : typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
525 : return __builtin_isinf(__type(__f));
526 : }
527 :
528 : template<typename _Tp>
529 : inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
530 : int>::__type
531 : isnan(_Tp __f)
532 : {
533 : typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
534 : return __builtin_isnan(__type(__f));
535 : }
536 :
537 : template<typename _Tp>
538 : inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
539 : int>::__type
540 : isnormal(_Tp __f)
541 : {
542 : typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
543 : return __builtin_isnormal(__type(__f));
544 : }
545 :
546 : template<typename _Tp>
547 : inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
548 : int>::__type
549 : signbit(_Tp __f)
550 : {
551 : typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
552 : return __builtin_signbit(__type(__f));
553 : }
554 :
555 : template<typename _Tp>
556 : inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
557 : int>::__type
558 : isgreater(_Tp __f1, _Tp __f2)
559 : {
560 : typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
561 : return __builtin_isgreater(__type(__f1), __type(__f2));
562 : }
563 :
564 : template<typename _Tp>
565 : inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
566 : int>::__type
567 : isgreaterequal(_Tp __f1, _Tp __f2)
568 : {
569 : typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
570 : return __builtin_isgreaterequal(__type(__f1), __type(__f2));
571 : }
572 :
573 : template<typename _Tp>
574 : inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
575 : int>::__type
576 : isless(_Tp __f1, _Tp __f2)
577 : {
578 : typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
579 : return __builtin_isless(__type(__f1), __type(__f2));
580 : }
581 :
582 : template<typename _Tp>
583 : inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
584 : int>::__type
585 : islessequal(_Tp __f1, _Tp __f2)
586 : {
587 : typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
588 : return __builtin_islessequal(__type(__f1), __type(__f2));
589 : }
590 :
591 : template<typename _Tp>
592 : inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
593 : int>::__type
594 : islessgreater(_Tp __f1, _Tp __f2)
595 : {
596 : typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
597 : return __builtin_islessgreater(__type(__f1), __type(__f2));
598 : }
599 :
600 : template<typename _Tp>
601 : inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
602 : int>::__type
603 : isunordered(_Tp __f1, _Tp __f2)
604 : {
605 : typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
606 : return __builtin_isunordered(__type(__f1), __type(__f2));
607 : }
608 :
609 : _GLIBCXX_END_NAMESPACE
610 :
611 : #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
612 : #endif
613 :
614 : #ifndef _GLIBCXX_EXPORT_TEMPLATE
615 : # include <bits/cmath.tcc>
616 : #endif
617 :
618 : #ifdef __GXX_EXPERIMENTAL_CXX0X__
619 : # if defined(_GLIBCXX_INCLUDE_AS_TR1)
620 : # error C++0x header cannot be included from TR1 header
621 : # endif
622 : # if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
623 : # include <tr1_impl/cmath>
624 : # else
625 : # define _GLIBCXX_INCLUDE_AS_CXX0X
626 : # define _GLIBCXX_BEGIN_NAMESPACE_TR1
627 : # define _GLIBCXX_END_NAMESPACE_TR1
628 : # define _GLIBCXX_TR1
629 : # include <tr1_impl/cmath>
630 : # undef _GLIBCXX_TR1
631 : # undef _GLIBCXX_END_NAMESPACE_TR1
632 : # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
633 : # undef _GLIBCXX_INCLUDE_AS_CXX0X
634 : # endif
635 : #endif
636 :
637 : #endif
|