1 // -*- C++ -*- C forwarding header.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 // Free Software Foundation, Inc.
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 2, or (at your option)
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.
18 // You should have received a copy of the GNU General Public License
19 // along with this library; see the file COPYING. If not, write to
20 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301, USA.
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 /** @file include/cmath
33 * This is a Standard C++ Library file. You should @c #include this file
34 * in your programs, rather than any of the "*.h" implementation files.
36 * This is the C++ version of the Standard C Library header @c math.h,
37 * and its contents are (mostly) the same as that header, but are all
38 * contained in the namespace @c std (except for names which are defined
43 // ISO C++ 14882: 26.5 C library
46 #pragma GCC system_header
48 #include <bits/c++config.h>
49 #include <bits/cpp_type_traits.h>
50 #include <ext/type_traits.h>
51 #include_next <math.h>
53 #ifndef _GLIBCXX_CMATH
54 #define _GLIBCXX_CMATH 1
56 // Get rid of those macros defined in <math.h> in lieu of real functions.
82 _GLIBCXX_BEGIN_NAMESPACE(std)
84 // Forward declaration of a helper function. This really should be
85 // an `exported' forward declaration.
86 template<typename _Tp>
87 _Tp __cmath_power(_Tp, unsigned int);
89 template<typename _Tp>
91 __pow_helper(_Tp __x, int __n)
94 ? _Tp(1)/__cmath_power(__x, -__n)
95 : __cmath_power(__x, __n);
100 { return __builtin_fabs(__x); }
104 { return __builtin_fabsf(__x); }
108 { return __builtin_fabsl(__x); }
114 { return __builtin_acosf(__x); }
117 acos(long double __x)
118 { return __builtin_acosl(__x); }
120 template<typename _Tp>
121 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
124 { return __builtin_acos(__x); }
130 { return __builtin_asinf(__x); }
133 asin(long double __x)
134 { return __builtin_asinl(__x); }
136 template<typename _Tp>
137 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
140 { return __builtin_asin(__x); }
146 { return __builtin_atanf(__x); }
149 atan(long double __x)
150 { return __builtin_atanl(__x); }
152 template<typename _Tp>
153 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
156 { return __builtin_atan(__x); }
161 atan2(float __y, float __x)
162 { return __builtin_atan2f(__y, __x); }
165 atan2(long double __y, long double __x)
166 { return __builtin_atan2l(__y, __x); }
168 template<typename _Tp, typename _Up>
170 typename __gnu_cxx::__promote_2<
171 typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
172 && __is_arithmetic<_Up>::__value,
173 _Tp>::__type, _Up>::__type
174 atan2(_Tp __y, _Up __x)
176 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
177 return atan2(__type(__y), __type(__x));
184 { return __builtin_ceilf(__x); }
187 ceil(long double __x)
188 { return __builtin_ceill(__x); }
190 template<typename _Tp>
191 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
194 { return __builtin_ceil(__x); }
200 { return __builtin_cosf(__x); }
204 { return __builtin_cosl(__x); }
206 template<typename _Tp>
207 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
210 { return __builtin_cos(__x); }
216 { return __builtin_coshf(__x); }
219 cosh(long double __x)
220 { return __builtin_coshl(__x); }
222 template<typename _Tp>
223 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
226 { return __builtin_cosh(__x); }
232 { return __builtin_expf(__x); }
236 { return __builtin_expl(__x); }
238 template<typename _Tp>
239 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
242 { return __builtin_exp(__x); }
248 { return __builtin_fabsf(__x); }
251 fabs(long double __x)
252 { return __builtin_fabsl(__x); }
254 template<typename _Tp>
255 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
258 { return __builtin_fabs(__x); }
264 { return __builtin_floorf(__x); }
267 floor(long double __x)
268 { return __builtin_floorl(__x); }
270 template<typename _Tp>
271 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
274 { return __builtin_floor(__x); }
279 fmod(float __x, float __y)
280 { return __builtin_fmodf(__x, __y); }
283 fmod(long double __x, long double __y)
284 { return __builtin_fmodl(__x, __y); }
289 frexp(float __x, int* __exp)
290 { return __builtin_frexpf(__x, __exp); }
293 frexp(long double __x, int* __exp)
294 { return __builtin_frexpl(__x, __exp); }
296 template<typename _Tp>
297 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
299 frexp(_Tp __x, int* __exp)
300 { return __builtin_frexp(__x, __exp); }
305 ldexp(float __x, int __exp)
306 { return __builtin_ldexpf(__x, __exp); }
309 ldexp(long double __x, int __exp)
310 { return __builtin_ldexpl(__x, __exp); }
312 template<typename _Tp>
313 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
315 ldexp(_Tp __x, int __exp)
316 { return __builtin_ldexp(__x, __exp); }
322 { return __builtin_logf(__x); }
326 { return __builtin_logl(__x); }
328 template<typename _Tp>
329 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
332 { return __builtin_log(__x); }
338 { return __builtin_log10f(__x); }
341 log10(long double __x)
342 { return __builtin_log10l(__x); }
344 template<typename _Tp>
345 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
348 { return __builtin_log10(__x); }
353 modf(float __x, float* __iptr)
354 { return __builtin_modff(__x, __iptr); }
357 modf(long double __x, long double* __iptr)
358 { return __builtin_modfl(__x, __iptr); }
363 pow(float __x, float __y)
364 { return __builtin_powf(__x, __y); }
367 pow(long double __x, long double __y)
368 { return __builtin_powl(__x, __y); }
370 #ifndef __GXX_EXPERIMENTAL_CXX0X__
371 // _GLIBCXX_RESOLVE_LIB_DEFECTS
372 // DR 550. What should the return type of pow(float,int) be?
374 pow(double __x, int __i)
375 { return __builtin_powi(__x, __i); }
378 pow(float __x, int __n)
379 { return __builtin_powif(__x, __n); }
382 pow(long double __x, int __n)
383 { return __builtin_powil(__x, __n); }
386 template<typename _Tp, typename _Up>
388 typename __gnu_cxx::__promote_2<
389 typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
390 && __is_arithmetic<_Up>::__value,
391 _Tp>::__type, _Up>::__type
392 pow(_Tp __x, _Up __y)
394 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
395 return pow(__type(__x), __type(__y));
402 { return __builtin_sinf(__x); }
406 { return __builtin_sinl(__x); }
408 template<typename _Tp>
409 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
412 { return __builtin_sin(__x); }
418 { return __builtin_sinhf(__x); }
421 sinh(long double __x)
422 { return __builtin_sinhl(__x); }
424 template<typename _Tp>
425 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
428 { return __builtin_sinh(__x); }
434 { return __builtin_sqrtf(__x); }
437 sqrt(long double __x)
438 { return __builtin_sqrtl(__x); }
440 template<typename _Tp>
441 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
444 { return __builtin_sqrt(__x); }
450 { return __builtin_tanf(__x); }
454 { return __builtin_tanl(__x); }
456 template<typename _Tp>
457 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
460 { return __builtin_tan(__x); }
466 { return __builtin_tanhf(__x); }
469 tanh(long double __x)
470 { return __builtin_tanhl(__x); }
472 template<typename _Tp>
473 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
476 { return __builtin_tanh(__x); }
478 _GLIBCXX_END_NAMESPACE
480 #if _GLIBCXX_USE_C99_MATH
481 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
483 // These are possible macros imported from C99-land.
491 #undef isgreaterequal
497 _GLIBCXX_BEGIN_NAMESPACE(std)
499 template<typename _Tp>
500 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
504 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
505 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
506 FP_SUBNORMAL, FP_ZERO, __type(__f));
509 template<typename _Tp>
510 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
514 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
515 return __builtin_isfinite(__type(__f));
518 template<typename _Tp>
519 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
523 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
524 return __builtin_isinf(__type(__f));
527 template<typename _Tp>
528 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
532 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
533 return __builtin_isnan(__type(__f));
536 template<typename _Tp>
537 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
541 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
542 return __builtin_isnormal(__type(__f));
545 template<typename _Tp>
546 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
550 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
551 return __builtin_signbit(__type(__f));
554 template<typename _Tp>
555 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
557 isgreater(_Tp __f1, _Tp __f2)
559 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
560 return __builtin_isgreater(__type(__f1), __type(__f2));
563 template<typename _Tp>
564 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
566 isgreaterequal(_Tp __f1, _Tp __f2)
568 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
569 return __builtin_isgreaterequal(__type(__f1), __type(__f2));
572 template<typename _Tp>
573 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
575 isless(_Tp __f1, _Tp __f2)
577 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
578 return __builtin_isless(__type(__f1), __type(__f2));
581 template<typename _Tp>
582 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
584 islessequal(_Tp __f1, _Tp __f2)
586 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
587 return __builtin_islessequal(__type(__f1), __type(__f2));
590 template<typename _Tp>
591 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
593 islessgreater(_Tp __f1, _Tp __f2)
595 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
596 return __builtin_islessgreater(__type(__f1), __type(__f2));
599 template<typename _Tp>
600 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
602 isunordered(_Tp __f1, _Tp __f2)
604 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
605 return __builtin_isunordered(__type(__f1), __type(__f2));
608 _GLIBCXX_END_NAMESPACE
610 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
613 #ifndef _GLIBCXX_EXPORT_TEMPLATE
614 # include <bits/cmath.tcc>
617 #ifdef __GXX_EXPERIMENTAL_CXX0X__
618 # if defined(_GLIBCXX_INCLUDE_AS_TR1)
619 # error C++0x header cannot be included from TR1 header
621 # if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
622 # include <tr1_impl/cmath>
624 # define _GLIBCXX_INCLUDE_AS_CXX0X
625 # define _GLIBCXX_BEGIN_NAMESPACE_TR1
626 # define _GLIBCXX_END_NAMESPACE_TR1
627 # define _GLIBCXX_TR1
628 # include <tr1_impl/cmath>
630 # undef _GLIBCXX_END_NAMESPACE_TR1
631 # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
632 # undef _GLIBCXX_INCLUDE_AS_CXX0X