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)
81 { return __builtin_fabs(__x); }
85 { return __builtin_fabsf(__x); }
89 { return __builtin_fabsl(__x); }
91 template<typename _Tp>
92 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
95 { return __builtin_fabs(__x); }
101 { return __builtin_acosf(__x); }
104 acos(long double __x)
105 { return __builtin_acosl(__x); }
107 template<typename _Tp>
108 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
111 { return __builtin_acos(__x); }
117 { return __builtin_asinf(__x); }
120 asin(long double __x)
121 { return __builtin_asinl(__x); }
123 template<typename _Tp>
124 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
127 { return __builtin_asin(__x); }
133 { return __builtin_atanf(__x); }
136 atan(long double __x)
137 { return __builtin_atanl(__x); }
139 template<typename _Tp>
140 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
143 { return __builtin_atan(__x); }
148 atan2(float __y, float __x)
149 { return __builtin_atan2f(__y, __x); }
152 atan2(long double __y, long double __x)
153 { return __builtin_atan2l(__y, __x); }
155 template<typename _Tp, typename _Up>
157 typename __gnu_cxx::__promote_2<
158 typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
159 && __is_arithmetic<_Up>::__value,
160 _Tp>::__type, _Up>::__type
161 atan2(_Tp __y, _Up __x)
163 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
164 return atan2(__type(__y), __type(__x));
171 { return __builtin_ceilf(__x); }
174 ceil(long double __x)
175 { return __builtin_ceill(__x); }
177 template<typename _Tp>
178 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
181 { return __builtin_ceil(__x); }
187 { return __builtin_cosf(__x); }
191 { return __builtin_cosl(__x); }
193 template<typename _Tp>
194 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
197 { return __builtin_cos(__x); }
203 { return __builtin_coshf(__x); }
206 cosh(long double __x)
207 { return __builtin_coshl(__x); }
209 template<typename _Tp>
210 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
213 { return __builtin_cosh(__x); }
219 { return __builtin_expf(__x); }
223 { return __builtin_expl(__x); }
225 template<typename _Tp>
226 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
229 { return __builtin_exp(__x); }
235 { return __builtin_fabsf(__x); }
238 fabs(long double __x)
239 { return __builtin_fabsl(__x); }
241 template<typename _Tp>
242 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
245 { return __builtin_fabs(__x); }
251 { return __builtin_floorf(__x); }
254 floor(long double __x)
255 { return __builtin_floorl(__x); }
257 template<typename _Tp>
258 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
261 { return __builtin_floor(__x); }
266 fmod(float __x, float __y)
267 { return __builtin_fmodf(__x, __y); }
270 fmod(long double __x, long double __y)
271 { return __builtin_fmodl(__x, __y); }
276 frexp(float __x, int* __exp)
277 { return __builtin_frexpf(__x, __exp); }
280 frexp(long double __x, int* __exp)
281 { return __builtin_frexpl(__x, __exp); }
283 template<typename _Tp>
284 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
286 frexp(_Tp __x, int* __exp)
287 { return __builtin_frexp(__x, __exp); }
292 ldexp(float __x, int __exp)
293 { return __builtin_ldexpf(__x, __exp); }
296 ldexp(long double __x, int __exp)
297 { return __builtin_ldexpl(__x, __exp); }
299 template<typename _Tp>
300 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
302 ldexp(_Tp __x, int __exp)
303 { return __builtin_ldexp(__x, __exp); }
309 { return __builtin_logf(__x); }
313 { return __builtin_logl(__x); }
315 template<typename _Tp>
316 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
319 { return __builtin_log(__x); }
325 { return __builtin_log10f(__x); }
328 log10(long double __x)
329 { return __builtin_log10l(__x); }
331 template<typename _Tp>
332 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
335 { return __builtin_log10(__x); }
340 modf(float __x, float* __iptr)
341 { return __builtin_modff(__x, __iptr); }
344 modf(long double __x, long double* __iptr)
345 { return __builtin_modfl(__x, __iptr); }
350 pow(float __x, float __y)
351 { return __builtin_powf(__x, __y); }
354 pow(long double __x, long double __y)
355 { return __builtin_powl(__x, __y); }
357 #ifndef __GXX_EXPERIMENTAL_CXX0X__
358 // _GLIBCXX_RESOLVE_LIB_DEFECTS
359 // DR 550. What should the return type of pow(float,int) be?
361 pow(double __x, int __i)
362 { return __builtin_powi(__x, __i); }
365 pow(float __x, int __n)
366 { return __builtin_powif(__x, __n); }
369 pow(long double __x, int __n)
370 { return __builtin_powil(__x, __n); }
373 template<typename _Tp, typename _Up>
375 typename __gnu_cxx::__promote_2<
376 typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
377 && __is_arithmetic<_Up>::__value,
378 _Tp>::__type, _Up>::__type
379 pow(_Tp __x, _Up __y)
381 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
382 return pow(__type(__x), __type(__y));
389 { return __builtin_sinf(__x); }
393 { return __builtin_sinl(__x); }
395 template<typename _Tp>
396 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
399 { return __builtin_sin(__x); }
405 { return __builtin_sinhf(__x); }
408 sinh(long double __x)
409 { return __builtin_sinhl(__x); }
411 template<typename _Tp>
412 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
415 { return __builtin_sinh(__x); }
421 { return __builtin_sqrtf(__x); }
424 sqrt(long double __x)
425 { return __builtin_sqrtl(__x); }
427 template<typename _Tp>
428 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
431 { return __builtin_sqrt(__x); }
437 { return __builtin_tanf(__x); }
441 { return __builtin_tanl(__x); }
443 template<typename _Tp>
444 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
447 { return __builtin_tan(__x); }
453 { return __builtin_tanhf(__x); }
456 tanh(long double __x)
457 { return __builtin_tanhl(__x); }
459 template<typename _Tp>
460 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
463 { return __builtin_tanh(__x); }
465 _GLIBCXX_END_NAMESPACE
467 #if _GLIBCXX_USE_C99_MATH
468 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
470 // These are possible macros imported from C99-land.
478 #undef isgreaterequal
484 _GLIBCXX_BEGIN_NAMESPACE(std)
486 #ifdef __GXX_EXPERIMENTAL_CXX0X__
488 fpclassify(float __x)
489 { return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
490 FP_SUBNORMAL, FP_ZERO, __x); }
493 fpclassify(double __x)
494 { return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
495 FP_SUBNORMAL, FP_ZERO, __x); }
498 fpclassify(long double __x)
499 { return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
500 FP_SUBNORMAL, FP_ZERO, __x); }
502 template<typename _Tp>
503 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
506 { return __x != 0 ? FP_NORMAL : FP_ZERO; }
510 { return __builtin_isfinite(__x); }
514 { return __builtin_isfinite(__x); }
517 isfinite(long double __x)
518 { return __builtin_isfinite(__x); }
520 template<typename _Tp>
521 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
528 { return __builtin_isinf(__x); }
532 { return __builtin_isinf(__x); }
535 isinf(long double __x)
536 { return __builtin_isinf(__x); }
538 template<typename _Tp>
539 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
546 { return __builtin_isnan(__x); }
550 { return __builtin_isnan(__x); }
553 isnan(long double __x)
554 { return __builtin_isnan(__x); }
556 template<typename _Tp>
557 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
564 { return __builtin_isnormal(__x); }
568 { return __builtin_isnormal(__x); }
571 isnormal(long double __x)
572 { return __builtin_isnormal(__x); }
574 template<typename _Tp>
575 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
578 { return __x != 0 ? true : false; }
582 { return __builtin_signbit(__x); }
586 { return __builtin_signbit(__x); }
589 signbit(long double __x)
590 { return __builtin_signbit(__x); }
592 template<typename _Tp>
593 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
596 { return __x < 0 ? true : false; }
599 isgreater(float __x, float __y)
600 { return __builtin_isgreater(__x, __y); }
603 isgreater(double __x, double __y)
604 { return __builtin_isgreater(__x, __y); }
607 isgreater(long double __x, long double __y)
608 { return __builtin_isgreater(__x, __y); }
610 template<typename _Tp, typename _Up>
612 __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
613 && __is_arithmetic<_Up>::__value), bool>::__type
614 isgreater(_Tp __x, _Up __y)
616 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
617 return __builtin_isgreater(__type(__x), __type(__y));
621 isgreaterequal(float __x, float __y)
622 { return __builtin_isgreaterequal(__x, __y); }
625 isgreaterequal(double __x, double __y)
626 { return __builtin_isgreaterequal(__x, __y); }
629 isgreaterequal(long double __x, long double __y)
630 { return __builtin_isgreaterequal(__x, __y); }
632 template<typename _Tp, typename _Up>
634 __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
635 && __is_arithmetic<_Up>::__value), bool>::__type
636 isgreaterequal(_Tp __x, _Up __y)
638 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
639 return __builtin_isgreaterequal(__type(__x), __type(__y));
643 isless(float __x, float __y)
644 { return __builtin_isless(__x, __y); }
647 isless(double __x, double __y)
648 { return __builtin_isless(__x, __y); }
651 isless(long double __x, long double __y)
652 { return __builtin_isless(__x, __y); }
654 template<typename _Tp, typename _Up>
656 __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
657 && __is_arithmetic<_Up>::__value), bool>::__type
658 isless(_Tp __x, _Up __y)
660 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
661 return __builtin_isless(__type(__x), __type(__y));
665 islessequal(float __x, float __y)
666 { return __builtin_islessequal(__x, __y); }
669 islessequal(double __x, double __y)
670 { return __builtin_islessequal(__x, __y); }
673 islessequal(long double __x, long double __y)
674 { return __builtin_islessequal(__x, __y); }
676 template<typename _Tp, typename _Up>
678 __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
679 && __is_arithmetic<_Up>::__value), bool>::__type
680 islessequal(_Tp __x, _Up __y)
682 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
683 return __builtin_islessequal(__type(__x), __type(__y));
687 islessgreater(float __x, float __y)
688 { return __builtin_islessgreater(__x, __y); }
691 islessgreater(double __x, double __y)
692 { return __builtin_islessgreater(__x, __y); }
695 islessgreater(long double __x, long double __y)
696 { return __builtin_islessgreater(__x, __y); }
698 template<typename _Tp, typename _Up>
700 __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
701 && __is_arithmetic<_Up>::__value), bool>::__type
702 islessgreater(_Tp __x, _Up __y)
704 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
705 return __builtin_islessgreater(__type(__x), __type(__y));
709 isunordered(float __x, float __y)
710 { return __builtin_isunordered(__x, __y); }
713 isunordered(double __x, double __y)
714 { return __builtin_isunordered(__x, __y); }
717 isunordered(long double __x, long double __y)
718 { return __builtin_isunordered(__x, __y); }
720 template<typename _Tp, typename _Up>
722 __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
723 && __is_arithmetic<_Up>::__value), bool>::__type
724 isunordered(_Tp __x, _Up __y)
726 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
727 return __builtin_isunordered(__type(__x), __type(__y));
732 template<typename _Tp>
733 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
737 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
738 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
739 FP_SUBNORMAL, FP_ZERO, __type(__f));
742 template<typename _Tp>
743 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
747 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
748 return __builtin_isfinite(__type(__f));
751 template<typename _Tp>
752 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
756 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
757 return __builtin_isinf(__type(__f));
760 template<typename _Tp>
761 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
765 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
766 return __builtin_isnan(__type(__f));
769 template<typename _Tp>
770 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
774 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
775 return __builtin_isnormal(__type(__f));
778 template<typename _Tp>
779 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
783 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
784 return __builtin_signbit(__type(__f));
787 template<typename _Tp>
788 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
790 isgreater(_Tp __f1, _Tp __f2)
792 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
793 return __builtin_isgreater(__type(__f1), __type(__f2));
796 template<typename _Tp>
797 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
799 isgreaterequal(_Tp __f1, _Tp __f2)
801 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
802 return __builtin_isgreaterequal(__type(__f1), __type(__f2));
805 template<typename _Tp>
806 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
808 isless(_Tp __f1, _Tp __f2)
810 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
811 return __builtin_isless(__type(__f1), __type(__f2));
814 template<typename _Tp>
815 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
817 islessequal(_Tp __f1, _Tp __f2)
819 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
820 return __builtin_islessequal(__type(__f1), __type(__f2));
823 template<typename _Tp>
824 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
826 islessgreater(_Tp __f1, _Tp __f2)
828 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
829 return __builtin_islessgreater(__type(__f1), __type(__f2));
832 template<typename _Tp>
833 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
835 isunordered(_Tp __f1, _Tp __f2)
837 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
838 return __builtin_isunordered(__type(__f1), __type(__f2));
843 _GLIBCXX_END_NAMESPACE
845 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
848 #ifdef __GXX_EXPERIMENTAL_CXX0X__
849 # if defined(_GLIBCXX_INCLUDE_AS_TR1)
850 # error C++0x header cannot be included from TR1 header
852 # if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
853 # include <tr1_impl/cmath>
855 # define _GLIBCXX_INCLUDE_AS_CXX0X
856 # define _GLIBCXX_BEGIN_NAMESPACE_TR1
857 # define _GLIBCXX_END_NAMESPACE_TR1
858 # define _GLIBCXX_TR1
859 # include <tr1_impl/cmath>
861 # undef _GLIBCXX_END_NAMESPACE_TR1
862 # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
863 # undef _GLIBCXX_INCLUDE_AS_CXX0X