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>
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<__traitand<__is_arithmetic<_Tp>,
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); }
372 pow(double __x, int __i)
373 { return __builtin_powi(__x, __i); }
376 pow(float __x, int __n)
377 { return __builtin_powif(__x, __n); }
380 pow(long double __x, int __n)
381 { return __builtin_powil(__x, __n); }
383 template<typename _Tp, typename _Up>
385 typename __gnu_cxx::__promote_2<
386 typename __gnu_cxx::__enable_if<__traitand<__is_arithmetic<_Tp>,
387 __is_arithmetic<_Up> >::__value,
388 _Tp>::__type, _Up>::__type
389 pow(_Tp __x, _Up __y)
391 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
392 return pow(__type(__x), __type(__y));
399 { return __builtin_sinf(__x); }
403 { return __builtin_sinl(__x); }
405 template<typename _Tp>
406 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
409 { return __builtin_sin(__x); }
415 { return __builtin_sinhf(__x); }
418 sinh(long double __x)
419 { return __builtin_sinhl(__x); }
421 template<typename _Tp>
422 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
425 { return __builtin_sinh(__x); }
431 { return __builtin_sqrtf(__x); }
434 sqrt(long double __x)
435 { return __builtin_sqrtl(__x); }
437 template<typename _Tp>
438 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
441 { return __builtin_sqrt(__x); }
447 { return __builtin_tanf(__x); }
451 { return __builtin_tanl(__x); }
453 template<typename _Tp>
454 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
457 { return __builtin_tan(__x); }
463 { return __builtin_tanhf(__x); }
466 tanh(long double __x)
467 { return __builtin_tanhl(__x); }
469 template<typename _Tp>
470 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
473 { return __builtin_tanh(__x); }
475 _GLIBCXX_END_NAMESPACE
477 #if _GLIBCXX_USE_C99_MATH
478 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
479 // These are possible macros imported from C99-land. For strict
480 // conformance, remove possible C99-injected names from the global
481 // namespace, and sequester them in the __gnu_cxx extension namespace.
483 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
485 template<typename _Tp>
487 __capture_fpclassify(_Tp __f) { return fpclassify(__f); }
489 _GLIBCXX_END_NAMESPACE
491 // Only undefine the C99 FP macros, if actually captured for namespace movement
499 #undef isgreaterequal
505 _GLIBCXX_BEGIN_NAMESPACE(std)
507 template<typename _Tp>
508 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
512 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
513 return ::__gnu_cxx::__capture_fpclassify(__type(__f));
516 template<typename _Tp>
517 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
521 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
522 return __builtin_isfinite(__type(__f));
525 template<typename _Tp>
526 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
530 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
531 return __builtin_isinf(__type(__f));
534 template<typename _Tp>
535 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
539 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
540 return __builtin_isnan(__type(__f));
543 template<typename _Tp>
544 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
548 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
549 return __builtin_isnormal(__type(__f));
552 template<typename _Tp>
553 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
557 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
558 return __builtin_signbit(__type(__f));
561 template<typename _Tp>
562 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
564 isgreater(_Tp __f1, _Tp __f2)
566 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
567 return __builtin_isgreater(__type(__f1), __type(__f2));
570 template<typename _Tp>
571 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
573 isgreaterequal(_Tp __f1, _Tp __f2)
575 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
576 return __builtin_isgreaterequal(__type(__f1), __type(__f2));
579 template<typename _Tp>
580 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
582 isless(_Tp __f1, _Tp __f2)
584 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
585 return __builtin_isless(__type(__f1), __type(__f2));
588 template<typename _Tp>
589 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
591 islessequal(_Tp __f1, _Tp __f2)
593 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
594 return __builtin_islessequal(__type(__f1), __type(__f2));
597 template<typename _Tp>
598 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
600 islessgreater(_Tp __f1, _Tp __f2)
602 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
603 return __builtin_islessgreater(__type(__f1), __type(__f2));
606 template<typename _Tp>
607 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
609 isunordered(_Tp __f1, _Tp __f2)
611 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
612 return __builtin_isunordered(__type(__f1), __type(__f2));
615 _GLIBCXX_END_NAMESPACE
617 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
620 #ifndef _GLIBCXX_EXPORT_TEMPLATE
621 # include <bits/cmath.tcc>
624 #ifdef __GXX_EXPERIMENTAL_CXX0X__
625 # if defined(_GLIBCXX_INCLUDE_AS_TR1)
626 # error C++0x header cannot be included from TR1 header
628 # if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
629 # include <tr1_impl/cmath>
631 # define _GLIBCXX_INCLUDE_AS_CXX0X
632 # define _GLIBCXX_BEGIN_NAMESPACE_TR1
633 # define _GLIBCXX_END_NAMESPACE_TR1
634 # define _GLIBCXX_TR1
635 # include <tr1_impl/cmath>
637 # undef _GLIBCXX_END_NAMESPACE_TR1
638 # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
639 # undef _GLIBCXX_INCLUDE_AS_CXX0X