1 // -*- C++ -*- C forwarding header.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009
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 3, 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 // 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.
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/>.
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 "*.h" implementation files.
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
38 // ISO C++ 14882: 26.5 C library
41 #pragma GCC system_header
43 #include <bits/c++config.h>
44 #include <bits/cpp_type_traits.h>
45 #include <ext/type_traits.h>
48 #ifndef _GLIBCXX_CMATH
49 #define _GLIBCXX_CMATH 1
51 // Get rid of those macros defined in <math.h> in lieu of real functions.
77 _GLIBCXX_BEGIN_NAMESPACE(std)
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);
84 template<typename _Tp>
86 __pow_helper(_Tp __x, int __n)
89 ? _Tp(1)/__cmath_power(__x, -__n)
90 : __cmath_power(__x, __n);
95 { return __builtin_fabs(__x); }
99 { return __builtin_fabsf(__x); }
103 { return __builtin_fabsl(__x); }
109 { return __builtin_acosf(__x); }
112 acos(long double __x)
113 { return __builtin_acosl(__x); }
115 template<typename _Tp>
116 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
119 { return __builtin_acos(__x); }
125 { return __builtin_asinf(__x); }
128 asin(long double __x)
129 { return __builtin_asinl(__x); }
131 template<typename _Tp>
132 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
135 { return __builtin_asin(__x); }
141 { return __builtin_atanf(__x); }
144 atan(long double __x)
145 { return __builtin_atanl(__x); }
147 template<typename _Tp>
148 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
151 { return __builtin_atan(__x); }
156 atan2(float __y, float __x)
157 { return __builtin_atan2f(__y, __x); }
160 atan2(long double __y, long double __x)
161 { return __builtin_atan2l(__y, __x); }
163 template<typename _Tp, typename _Up>
165 typename __gnu_cxx::__promote_2<
166 typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
167 && __is_arithmetic<_Up>::__value,
168 _Tp>::__type, _Up>::__type
169 atan2(_Tp __y, _Up __x)
171 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
172 return atan2(__type(__y), __type(__x));
179 { return __builtin_ceilf(__x); }
182 ceil(long double __x)
183 { return __builtin_ceill(__x); }
185 template<typename _Tp>
186 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
189 { return __builtin_ceil(__x); }
195 { return __builtin_cosf(__x); }
199 { return __builtin_cosl(__x); }
201 template<typename _Tp>
202 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
205 { return __builtin_cos(__x); }
211 { return __builtin_coshf(__x); }
214 cosh(long double __x)
215 { return __builtin_coshl(__x); }
217 template<typename _Tp>
218 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
221 { return __builtin_cosh(__x); }
227 { return __builtin_expf(__x); }
231 { return __builtin_expl(__x); }
233 template<typename _Tp>
234 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
237 { return __builtin_exp(__x); }
243 { return __builtin_fabsf(__x); }
246 fabs(long double __x)
247 { return __builtin_fabsl(__x); }
249 template<typename _Tp>
250 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
253 { return __builtin_fabs(__x); }
259 { return __builtin_floorf(__x); }
262 floor(long double __x)
263 { return __builtin_floorl(__x); }
265 template<typename _Tp>
266 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
269 { return __builtin_floor(__x); }
274 fmod(float __x, float __y)
275 { return __builtin_fmodf(__x, __y); }
278 fmod(long double __x, long double __y)
279 { return __builtin_fmodl(__x, __y); }
284 frexp(float __x, int* __exp)
285 { return __builtin_frexpf(__x, __exp); }
288 frexp(long double __x, int* __exp)
289 { return __builtin_frexpl(__x, __exp); }
291 template<typename _Tp>
292 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
294 frexp(_Tp __x, int* __exp)
295 { return __builtin_frexp(__x, __exp); }
300 ldexp(float __x, int __exp)
301 { return __builtin_ldexpf(__x, __exp); }
304 ldexp(long double __x, int __exp)
305 { return __builtin_ldexpl(__x, __exp); }
307 template<typename _Tp>
308 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
310 ldexp(_Tp __x, int __exp)
311 { return __builtin_ldexp(__x, __exp); }
317 { return __builtin_logf(__x); }
321 { return __builtin_logl(__x); }
323 template<typename _Tp>
324 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
327 { return __builtin_log(__x); }
333 { return __builtin_log10f(__x); }
336 log10(long double __x)
337 { return __builtin_log10l(__x); }
339 template<typename _Tp>
340 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
343 { return __builtin_log10(__x); }
348 modf(float __x, float* __iptr)
349 { return __builtin_modff(__x, __iptr); }
352 modf(long double __x, long double* __iptr)
353 { return __builtin_modfl(__x, __iptr); }
358 pow(float __x, float __y)
359 { return __builtin_powf(__x, __y); }
362 pow(long double __x, long double __y)
363 { return __builtin_powl(__x, __y); }
365 #ifndef __GXX_EXPERIMENTAL_CXX0X__
366 // _GLIBCXX_RESOLVE_LIB_DEFECTS
367 // DR 550. What should the return type of pow(float,int) be?
369 pow(double __x, int __i)
370 { return __builtin_powi(__x, __i); }
373 pow(float __x, int __n)
374 { return __builtin_powif(__x, __n); }
377 pow(long double __x, int __n)
378 { return __builtin_powil(__x, __n); }
381 template<typename _Tp, typename _Up>
383 typename __gnu_cxx::__promote_2<
384 typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
385 && __is_arithmetic<_Up>::__value,
386 _Tp>::__type, _Up>::__type
387 pow(_Tp __x, _Up __y)
389 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
390 return pow(__type(__x), __type(__y));
397 { return __builtin_sinf(__x); }
401 { return __builtin_sinl(__x); }
403 template<typename _Tp>
404 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
407 { return __builtin_sin(__x); }
413 { return __builtin_sinhf(__x); }
416 sinh(long double __x)
417 { return __builtin_sinhl(__x); }
419 template<typename _Tp>
420 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
423 { return __builtin_sinh(__x); }
429 { return __builtin_sqrtf(__x); }
432 sqrt(long double __x)
433 { return __builtin_sqrtl(__x); }
435 template<typename _Tp>
436 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
439 { return __builtin_sqrt(__x); }
445 { return __builtin_tanf(__x); }
449 { return __builtin_tanl(__x); }
451 template<typename _Tp>
452 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
455 { return __builtin_tan(__x); }
461 { return __builtin_tanhf(__x); }
464 tanh(long double __x)
465 { return __builtin_tanhl(__x); }
467 template<typename _Tp>
468 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
471 { return __builtin_tanh(__x); }
473 _GLIBCXX_END_NAMESPACE
475 #if _GLIBCXX_USE_C99_MATH
476 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
478 // These are possible macros imported from C99-land.
486 #undef isgreaterequal
492 _GLIBCXX_BEGIN_NAMESPACE(std)
494 template<typename _Tp>
495 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
499 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
500 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
501 FP_SUBNORMAL, FP_ZERO, __type(__f));
504 template<typename _Tp>
505 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
509 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
510 return __builtin_isfinite(__type(__f));
513 template<typename _Tp>
514 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
518 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
519 return __builtin_isinf(__type(__f));
522 template<typename _Tp>
523 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
527 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
528 return __builtin_isnan(__type(__f));
531 template<typename _Tp>
532 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
536 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
537 return __builtin_isnormal(__type(__f));
540 template<typename _Tp>
541 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
545 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
546 return __builtin_signbit(__type(__f));
549 template<typename _Tp>
550 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
552 isgreater(_Tp __f1, _Tp __f2)
554 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
555 return __builtin_isgreater(__type(__f1), __type(__f2));
558 template<typename _Tp>
559 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
561 isgreaterequal(_Tp __f1, _Tp __f2)
563 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
564 return __builtin_isgreaterequal(__type(__f1), __type(__f2));
567 template<typename _Tp>
568 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
570 isless(_Tp __f1, _Tp __f2)
572 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
573 return __builtin_isless(__type(__f1), __type(__f2));
576 template<typename _Tp>
577 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
579 islessequal(_Tp __f1, _Tp __f2)
581 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
582 return __builtin_islessequal(__type(__f1), __type(__f2));
585 template<typename _Tp>
586 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
588 islessgreater(_Tp __f1, _Tp __f2)
590 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
591 return __builtin_islessgreater(__type(__f1), __type(__f2));
594 template<typename _Tp>
595 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
597 isunordered(_Tp __f1, _Tp __f2)
599 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
600 return __builtin_isunordered(__type(__f1), __type(__f2));
603 _GLIBCXX_END_NAMESPACE
605 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
608 #ifndef _GLIBCXX_EXPORT_TEMPLATE
609 # include <bits/cmath.tcc>
612 #ifdef __GXX_EXPERIMENTAL_CXX0X__
613 # if defined(_GLIBCXX_INCLUDE_AS_TR1)
614 # error C++0x header cannot be included from TR1 header
616 # if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
617 # include <tr1_impl/cmath>
619 # define _GLIBCXX_INCLUDE_AS_CXX0X
620 # define _GLIBCXX_BEGIN_NAMESPACE_TR1
621 # define _GLIBCXX_END_NAMESPACE_TR1
622 # define _GLIBCXX_TR1
623 # include <tr1_impl/cmath>
625 # undef _GLIBCXX_END_NAMESPACE_TR1
626 # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
627 # undef _GLIBCXX_INCLUDE_AS_CXX0X