1 // -*- C++ -*- C forwarding header.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010
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 @a *.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); }
105 template<typename _Tp>
106 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
109 { return __builtin_fabs(__x); }
115 { return __builtin_acosf(__x); }
118 acos(long double __x)
119 { return __builtin_acosl(__x); }
121 template<typename _Tp>
122 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
125 { return __builtin_acos(__x); }
131 { return __builtin_asinf(__x); }
134 asin(long double __x)
135 { return __builtin_asinl(__x); }
137 template<typename _Tp>
138 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
141 { return __builtin_asin(__x); }
147 { return __builtin_atanf(__x); }
150 atan(long double __x)
151 { return __builtin_atanl(__x); }
153 template<typename _Tp>
154 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
157 { return __builtin_atan(__x); }
162 atan2(float __y, float __x)
163 { return __builtin_atan2f(__y, __x); }
166 atan2(long double __y, long double __x)
167 { return __builtin_atan2l(__y, __x); }
169 template<typename _Tp, typename _Up>
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)
177 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
178 return atan2(__type(__y), __type(__x));
185 { return __builtin_ceilf(__x); }
188 ceil(long double __x)
189 { return __builtin_ceill(__x); }
191 template<typename _Tp>
192 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
195 { return __builtin_ceil(__x); }
201 { return __builtin_cosf(__x); }
205 { return __builtin_cosl(__x); }
207 template<typename _Tp>
208 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
211 { return __builtin_cos(__x); }
217 { return __builtin_coshf(__x); }
220 cosh(long double __x)
221 { return __builtin_coshl(__x); }
223 template<typename _Tp>
224 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
227 { return __builtin_cosh(__x); }
233 { return __builtin_expf(__x); }
237 { return __builtin_expl(__x); }
239 template<typename _Tp>
240 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
243 { return __builtin_exp(__x); }
249 { return __builtin_fabsf(__x); }
252 fabs(long double __x)
253 { return __builtin_fabsl(__x); }
255 template<typename _Tp>
256 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
259 { return __builtin_fabs(__x); }
265 { return __builtin_floorf(__x); }
268 floor(long double __x)
269 { return __builtin_floorl(__x); }
271 template<typename _Tp>
272 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
275 { return __builtin_floor(__x); }
280 fmod(float __x, float __y)
281 { return __builtin_fmodf(__x, __y); }
284 fmod(long double __x, long double __y)
285 { return __builtin_fmodl(__x, __y); }
290 frexp(float __x, int* __exp)
291 { return __builtin_frexpf(__x, __exp); }
294 frexp(long double __x, int* __exp)
295 { return __builtin_frexpl(__x, __exp); }
297 template<typename _Tp>
298 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
300 frexp(_Tp __x, int* __exp)
301 { return __builtin_frexp(__x, __exp); }
306 ldexp(float __x, int __exp)
307 { return __builtin_ldexpf(__x, __exp); }
310 ldexp(long double __x, int __exp)
311 { return __builtin_ldexpl(__x, __exp); }
313 template<typename _Tp>
314 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
316 ldexp(_Tp __x, int __exp)
317 { return __builtin_ldexp(__x, __exp); }
323 { return __builtin_logf(__x); }
327 { return __builtin_logl(__x); }
329 template<typename _Tp>
330 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
333 { return __builtin_log(__x); }
339 { return __builtin_log10f(__x); }
342 log10(long double __x)
343 { return __builtin_log10l(__x); }
345 template<typename _Tp>
346 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
349 { return __builtin_log10(__x); }
354 modf(float __x, float* __iptr)
355 { return __builtin_modff(__x, __iptr); }
358 modf(long double __x, long double* __iptr)
359 { return __builtin_modfl(__x, __iptr); }
364 pow(float __x, float __y)
365 { return __builtin_powf(__x, __y); }
368 pow(long double __x, long double __y)
369 { return __builtin_powl(__x, __y); }
371 #ifndef __GXX_EXPERIMENTAL_CXX0X__
372 // _GLIBCXX_RESOLVE_LIB_DEFECTS
373 // DR 550. What should the return type of pow(float,int) be?
375 pow(double __x, int __i)
376 { return __builtin_powi(__x, __i); }
379 pow(float __x, int __n)
380 { return __builtin_powif(__x, __n); }
383 pow(long double __x, int __n)
384 { return __builtin_powil(__x, __n); }
387 template<typename _Tp, typename _Up>
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)
395 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
396 return pow(__type(__x), __type(__y));
403 { return __builtin_sinf(__x); }
407 { return __builtin_sinl(__x); }
409 template<typename _Tp>
410 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
413 { return __builtin_sin(__x); }
419 { return __builtin_sinhf(__x); }
422 sinh(long double __x)
423 { return __builtin_sinhl(__x); }
425 template<typename _Tp>
426 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
429 { return __builtin_sinh(__x); }
435 { return __builtin_sqrtf(__x); }
438 sqrt(long double __x)
439 { return __builtin_sqrtl(__x); }
441 template<typename _Tp>
442 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
445 { return __builtin_sqrt(__x); }
451 { return __builtin_tanf(__x); }
455 { return __builtin_tanl(__x); }
457 template<typename _Tp>
458 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
461 { return __builtin_tan(__x); }
467 { return __builtin_tanhf(__x); }
470 tanh(long double __x)
471 { return __builtin_tanhl(__x); }
473 template<typename _Tp>
474 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
477 { return __builtin_tanh(__x); }
479 _GLIBCXX_END_NAMESPACE
481 #if _GLIBCXX_USE_C99_MATH
482 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
484 // These are possible macros imported from C99-land.
492 #undef isgreaterequal
498 _GLIBCXX_BEGIN_NAMESPACE(std)
500 #ifdef __GXX_EXPERIMENTAL_CXX0X__
502 fpclassify(float __x)
503 { return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
504 FP_SUBNORMAL, FP_ZERO, __x); }
507 fpclassify(double __x)
508 { return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
509 FP_SUBNORMAL, FP_ZERO, __x); }
512 fpclassify(long double __x)
513 { return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
514 FP_SUBNORMAL, FP_ZERO, __x); }
516 template<typename _Tp>
517 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
520 { return __x != 0 ? FP_NORMAL : FP_ZERO; }
524 { return __builtin_isfinite(__x); }
528 { return __builtin_isfinite(__x); }
531 isfinite(long double __x)
532 { return __builtin_isfinite(__x); }
534 template<typename _Tp>
535 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
542 { return __builtin_isinf(__x); }
546 { return __builtin_isinf(__x); }
549 isinf(long double __x)
550 { return __builtin_isinf(__x); }
552 template<typename _Tp>
553 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
560 { return __builtin_isnan(__x); }
564 { return __builtin_isnan(__x); }
567 isnan(long double __x)
568 { return __builtin_isnan(__x); }
570 template<typename _Tp>
571 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
578 { return __builtin_isnormal(__x); }
582 { return __builtin_isnormal(__x); }
585 isnormal(long double __x)
586 { return __builtin_isnormal(__x); }
588 template<typename _Tp>
589 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
592 { return __x != 0 ? true : false; }
596 { return __builtin_signbit(__x); }
600 { return __builtin_signbit(__x); }
603 signbit(long double __x)
604 { return __builtin_signbit(__x); }
606 template<typename _Tp>
607 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
610 { return __x < 0 ? true : false; }
613 isgreater(float __x, float __y)
614 { return __builtin_isgreater(__x, __y); }
617 isgreater(double __x, double __y)
618 { return __builtin_isgreater(__x, __y); }
621 isgreater(long double __x, long double __y)
622 { return __builtin_isgreater(__x, __y); }
624 template<typename _Tp, typename _Up>
626 __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
627 && __is_arithmetic<_Up>::__value), bool>::__type
628 isgreater(_Tp __x, _Up __y)
630 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
631 return __builtin_isgreater(__type(__x), __type(__y));
635 isgreaterequal(float __x, float __y)
636 { return __builtin_isgreaterequal(__x, __y); }
639 isgreaterequal(double __x, double __y)
640 { return __builtin_isgreaterequal(__x, __y); }
643 isgreaterequal(long double __x, long double __y)
644 { return __builtin_isgreaterequal(__x, __y); }
646 template<typename _Tp, typename _Up>
648 __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
649 && __is_arithmetic<_Up>::__value), bool>::__type
650 isgreaterequal(_Tp __x, _Up __y)
652 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
653 return __builtin_isgreaterequal(__type(__x), __type(__y));
657 isless(float __x, float __y)
658 { return __builtin_isless(__x, __y); }
661 isless(double __x, double __y)
662 { return __builtin_isless(__x, __y); }
665 isless(long double __x, long double __y)
666 { return __builtin_isless(__x, __y); }
668 template<typename _Tp, typename _Up>
670 __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
671 && __is_arithmetic<_Up>::__value), bool>::__type
672 isless(_Tp __x, _Up __y)
674 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
675 return __builtin_isless(__type(__x), __type(__y));
679 islessequal(float __x, float __y)
680 { return __builtin_islessequal(__x, __y); }
683 islessequal(double __x, double __y)
684 { return __builtin_islessequal(__x, __y); }
687 islessequal(long double __x, long double __y)
688 { return __builtin_islessequal(__x, __y); }
690 template<typename _Tp, typename _Up>
692 __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
693 && __is_arithmetic<_Up>::__value), bool>::__type
694 islessequal(_Tp __x, _Up __y)
696 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
697 return __builtin_islessequal(__type(__x), __type(__y));
701 islessgreater(float __x, float __y)
702 { return __builtin_islessgreater(__x, __y); }
705 islessgreater(double __x, double __y)
706 { return __builtin_islessgreater(__x, __y); }
709 islessgreater(long double __x, long double __y)
710 { return __builtin_islessgreater(__x, __y); }
712 template<typename _Tp, typename _Up>
714 __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
715 && __is_arithmetic<_Up>::__value), bool>::__type
716 islessgreater(_Tp __x, _Up __y)
718 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
719 return __builtin_islessgreater(__type(__x), __type(__y));
723 isunordered(float __x, float __y)
724 { return __builtin_isunordered(__x, __y); }
727 isunordered(double __x, double __y)
728 { return __builtin_isunordered(__x, __y); }
731 isunordered(long double __x, long double __y)
732 { return __builtin_isunordered(__x, __y); }
734 template<typename _Tp, typename _Up>
736 __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
737 && __is_arithmetic<_Up>::__value), bool>::__type
738 isunordered(_Tp __x, _Up __y)
740 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
741 return __builtin_isunordered(__type(__x), __type(__y));
746 template<typename _Tp>
747 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
751 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
752 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
753 FP_SUBNORMAL, FP_ZERO, __type(__f));
756 template<typename _Tp>
757 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
761 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
762 return __builtin_isfinite(__type(__f));
765 template<typename _Tp>
766 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
770 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
771 return __builtin_isinf(__type(__f));
774 template<typename _Tp>
775 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
779 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
780 return __builtin_isnan(__type(__f));
783 template<typename _Tp>
784 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
788 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
789 return __builtin_isnormal(__type(__f));
792 template<typename _Tp>
793 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
797 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
798 return __builtin_signbit(__type(__f));
801 template<typename _Tp>
802 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
804 isgreater(_Tp __f1, _Tp __f2)
806 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
807 return __builtin_isgreater(__type(__f1), __type(__f2));
810 template<typename _Tp>
811 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
813 isgreaterequal(_Tp __f1, _Tp __f2)
815 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
816 return __builtin_isgreaterequal(__type(__f1), __type(__f2));
819 template<typename _Tp>
820 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
822 isless(_Tp __f1, _Tp __f2)
824 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
825 return __builtin_isless(__type(__f1), __type(__f2));
828 template<typename _Tp>
829 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
831 islessequal(_Tp __f1, _Tp __f2)
833 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
834 return __builtin_islessequal(__type(__f1), __type(__f2));
837 template<typename _Tp>
838 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
840 islessgreater(_Tp __f1, _Tp __f2)
842 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
843 return __builtin_islessgreater(__type(__f1), __type(__f2));
846 template<typename _Tp>
847 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
849 isunordered(_Tp __f1, _Tp __f2)
851 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
852 return __builtin_isunordered(__type(__f1), __type(__f2));
857 _GLIBCXX_END_NAMESPACE
859 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
862 #ifndef _GLIBCXX_EXPORT_TEMPLATE
863 # include <bits/cmath.tcc>
866 #ifdef __GXX_EXPERIMENTAL_CXX0X__
867 # if defined(_GLIBCXX_INCLUDE_AS_TR1)
868 # error C++0x header cannot be included from TR1 header
870 # if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
871 # include <tr1_impl/cmath>
873 # define _GLIBCXX_INCLUDE_AS_CXX0X
874 # define _GLIBCXX_BEGIN_NAMESPACE_TR1
875 # define _GLIBCXX_END_NAMESPACE_TR1
876 # define _GLIBCXX_TR1
877 # include <tr1_impl/cmath>
879 # undef _GLIBCXX_END_NAMESPACE_TR1
880 # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
881 # undef _GLIBCXX_INCLUDE_AS_CXX0X