1 // -*- C++ -*- C forwarding header.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 26.5 C library
36 * This is a Standard C++ Library file. You should @c #include this file
37 * in your programs, rather than any of the "*.h" implementation files.
39 * This is the C++ version of the Standard C Library header @c math.h,
40 * and its contents are (mostly) the same as that header, but are all
41 * contained in the namespace @c std.
44 #ifndef _GLIBCXX_CMATH
45 #define _GLIBCXX_CMATH 1
47 #pragma GCC system_header
49 #include <bits/c++config.h>
50 #include <bits/cpp_type_traits.h>
54 // Get rid of those macros defined in <math.h> in lieu of real functions.
80 // ...and in the darkness bind them...
83 namespace __c99_binding
85 #if _GLIBCXX_USE_C99_FLOAT_TRANSCENDENTALS_CHECK || \
86 _GLIBCXX_USE_C99_FLOAT_TRANSCENDENTALS_DYNAMIC
87 extern "C" float (acosf)(float);
88 extern "C" float (asinf)(float);
89 extern "C" float (atanf)(float);
90 extern "C" float (atan2f)(float, float);
91 extern "C" float (ceilf)(float);
92 extern "C" float (coshf)(float);
93 extern "C" float (expf)(float);
94 extern "C" float (floorf)(float);
95 extern "C" float (fmodf)(float, float);
96 extern "C" float (frexpf)(float, int*);
97 extern "C" float (ldexpf)(float, int);
98 extern "C" float (logf)(float);
99 extern "C" float (log10f)(float);
100 extern "C" float (modff)(float, float*);
101 extern "C" float (powf)(float, float);
102 extern "C" float (sinhf)(float);
103 extern "C" float (tanf)(float);
104 extern "C" float (tanhf)(float);
106 #if !_GLIBCXX_USE_C99_FLOAT_TRANSCENDENTALS_DYNAMIC
107 #if _GLIBCXX_HAVE_ACOSF
110 #if _GLIBCXX_HAVE_ASINF
113 #if _GLIBCXX_HAVE_ATANF
116 #if _GLIBCXX_HAVE_ATAN2F
119 #if _GLIBCXX_HAVE_CEILF
122 #if _GLIBCXX_HAVE_COSHF
125 #if _GLIBCXX_HAVE_EXPF
128 #if _GLIBCXX_HAVE_FLOORF
131 #if _GLIBCXX_HAVE_FMODF
134 #if _GLIBCXX_HAVE_FREXPF
137 #if _GLIBCXX_HAVE_LDEXPF
140 #if _GLIBCXX_HAVE_LOGF
143 #if _GLIBCXX_HAVE_LOG10F
146 #if _GLIBCXX_HAVE_MODFF
149 #if _GLIBCXX_HAVE_POWF
152 #if _GLIBCXX_HAVE_SINHF
155 #if _GLIBCXX_HAVE_TANF
158 #if _GLIBCXX_HAVE_TANHF
161 #endif /* _GLIBCXX_USE_C99_FLOAT_TRANSCENDENTALS_DYNAMIC */
167 // Forward declaration of a helper function. This really should be
168 // an `exported' forward declaration.
169 template<typename _Tp> _Tp __cmath_power(_Tp, unsigned int);
173 { return __builtin_fabs(__x); }
177 { return __builtin_fabsf(__x); }
181 { return __builtin_fabsl(__x); }
183 #if _GLIBCXX_HAVE_ACOSF
185 acos(float __x) { return __gnu_cxx::__c99_binding::acosf(__x); }
188 acos(float __x) { return ::acos(static_cast<double>(__x)); }
193 #if _GLIBCXX_HAVE_ACOSL
195 acos(long double __x) { return ::acosl(__x); }
198 acos(long double __x) { return ::acos(static_cast<double>(__x)); }
201 template<typename _Tp>
202 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
205 return ::acos(static_cast<double>(__x));
210 #if _GLIBCXX_HAVE_ASINF
212 asin(float __x) { return __gnu_cxx::__c99_binding::asinf(__x); }
215 asin(float __x) { return ::asin(static_cast<double>(__x)); }
218 #if _GLIBCXX_HAVE_ASINL
220 asin(long double __x) { return ::asinl(__x); }
223 asin(long double __x) { return ::asin(static_cast<double>(__x)); }
226 template<typename _Tp>
227 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
229 { return ::asin(static_cast<double>(__x)); }
233 #if _GLIBCXX_HAVE_ATANF
235 atan(float __x) { return __gnu_cxx::__c99_binding::atanf(__x); }
238 atan(float __x) { return ::atan(static_cast<double>(__x)); }
241 #if _GLIBCXX_HAVE_ATANL
243 atan(long double __x) { return ::atanl(__x); }
246 atan(long double __x) { return ::atan(static_cast<double>(__x)); }
249 template<typename _Tp>
250 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
252 { return ::atan(static_cast<double>(__x)); }
256 #if _GLIBCXX_HAVE_ATAN2F
258 atan2(float __y, float __x) { return __gnu_cxx::__c99_binding::atan2f(__y, __x); }
261 atan2(float __y, float __x)
262 { return ::atan2(static_cast<double>(__y), static_cast<double>(__x)); }
265 #if _GLIBCXX_HAVE_ATAN2L
267 atan2(long double __y, long double __x) { return ::atan2l(__y, __x); }
270 atan2(long double __y, long double __x)
271 { return ::atan2(static_cast<double>(__y), static_cast<double>(__x)); }
274 template<typename _Tp, typename _Up>
275 inline typename __enable_if<double, __is_integer<_Tp>::_M_type
276 && __is_integer<_Up>::_M_type>::_M_type
277 atan2(_Tp __x, _Up __y)
278 { return ::atan2(static_cast<double>(__x), static_cast<double>(__y)); }
282 #if _GLIBCXX_HAVE_CEILF
284 ceil(float __x) { return __gnu_cxx::__c99_binding::ceilf(__x); }
287 ceil(float __x) { return ::ceil(static_cast<double>(__x)); }
290 #if _GLIBCXX_HAVE_CEILL
292 ceil(long double __x) { return ::ceill(__x); }
295 ceil(long double __x) { return ::ceil(static_cast<double>(__x)); }
298 template<typename _Tp>
299 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
301 { return ::ceil(static_cast<double>(__x)); }
307 { return __builtin_cosf(__x); }
311 { return __builtin_cosl(__x); }
313 template<typename _Tp>
314 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
316 { return __builtin_cos(__x); }
320 #if _GLIBCXX_HAVE_COSHF
322 cosh(float __x) { return __gnu_cxx::__c99_binding::coshf(__x); }
325 cosh(float __x) { return ::cosh(static_cast<double>(__x)); }
328 #if _GLIBCXX_HAVE_COSHL
330 cosh(long double __x) { return ::coshl(__x); }
333 cosh(long double __x) { return ::cosh(static_cast<double>(__x)); }
336 template<typename _Tp>
337 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
339 { return ::cosh(static_cast<double>(__x)); }
343 #if _GLIBCXX_HAVE_EXPF
345 exp(float __x) { return __gnu_cxx::__c99_binding::expf(__x); }
348 exp(float __x) { return ::exp(static_cast<double>(__x)); }
351 #if _GLIBCXX_HAVE_EXPL
353 exp(long double __x) { return ::expl(__x); }
356 exp(long double __x) { return ::exp(static_cast<double>(__x)); }
359 template<typename _Tp>
360 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
362 { return ::exp(static_cast<double>(__x)); }
368 { return __builtin_fabsf(__x); }
371 fabs(long double __x)
372 { return __builtin_fabsl(__x); }
374 template<typename _Tp>
375 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
377 { return __builtin_fabs(__x); }
381 #if _GLIBCXX_HAVE_FLOORF
383 floor(float __x) { return __gnu_cxx::__c99_binding::floorf(__x); }
386 floor(float __x) { return ::floor(static_cast<double>(__x)); }
389 #if _GLIBCXX_HAVE_FLOORL
391 floor(long double __x) { return ::floorl(__x); }
394 floor(long double __x) { return ::floor(static_cast<double>(__x)); }
397 template<typename _Tp>
398 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
400 { return ::floor(static_cast<double>(__x)); }
404 #if _GLIBCXX_HAVE_FMODF
406 fmod(float __x, float __y) { return __gnu_cxx::__c99_binding::fmodf(__x, __y); }
409 fmod(float __x, float __y)
410 { return ::fmod(static_cast<double>(__x), static_cast<double>(__y)); }
413 #if _GLIBCXX_HAVE_FMODL
415 fmod(long double __x, long double __y) { return ::fmodl(__x, __y); }
418 fmod(long double __x, long double __y)
419 { return ::fmod(static_cast<double>(__x), static_cast<double>(__y)); }
424 #if _GLIBCXX_HAVE_FREXPF
426 frexp(float __x, int* __exp) { return __gnu_cxx::__c99_binding::frexpf(__x, __exp); }
429 frexp(float __x, int* __exp) { return ::frexp(__x, __exp); }
432 #if _GLIBCXX_HAVE_FREXPL
434 frexp(long double __x, int* __exp) { return ::frexpl(__x, __exp); }
437 frexp(long double __x, int* __exp)
438 { return ::frexp(static_cast<double>(__x), __exp); }
441 template<typename _Tp>
442 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
443 frexp(_Tp __x, int* __exp)
444 { return ::frexp(static_cast<double>(__x), __exp); }
448 #if _GLIBCXX_HAVE_LDEXPF
450 ldexp(float __x, int __exp) { return __gnu_cxx::__c99_binding::ldexpf(__x, __exp); }
453 ldexp(float __x, int __exp)
454 { return ::ldexp(static_cast<double>(__x), __exp); }
457 #if _GLIBCXX_HAVE_LDEXPL
459 ldexp(long double __x, int __exp) { return ::ldexpl(__x, __exp); }
462 ldexp(long double __x, int __exp)
463 { return ::ldexp(static_cast<double>(__x), __exp); }
466 template<typename _Tp>
467 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
468 ldexp(_Tp __x, int __exp)
469 { return ::ldexp(static_cast<double>(__x), __exp); }
473 #if _GLIBCXX_HAVE_LOGF
475 log(float __x) { return __gnu_cxx::__c99_binding::logf(__x); }
477 inline float log(float __x)
478 { return ::log(static_cast<double>(__x)); }
481 #if _GLIBCXX_HAVE_LOGL
483 log(long double __x) { return ::logl(__x); }
486 log(long double __x) { return ::log(static_cast<double>(__x)); }
489 template<typename _Tp>
490 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
492 { return ::log(static_cast<double>(__x)); }
496 #if _GLIBCXX_HAVE_LOG10F
498 log10(float __x) { return __gnu_cxx::__c99_binding::log10f(__x); }
501 log10(float __x) { return ::log10(static_cast<double>(__x)); }
504 #if _GLIBCXX_HAVE_LOG10L
506 log10(long double __x) { return ::log10l(__x); }
509 log10(long double __x) { return ::log10(static_cast<double>(__x)); }
512 template<typename _Tp>
513 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
515 { return ::log10(static_cast<double>(__x)); }
519 #if _GLIBCXX_HAVE_MODFF
521 modf(float __x, float* __iptr) { return __gnu_cxx::__c99_binding::modff(__x, __iptr); }
524 modf(float __x, float* __iptr)
527 double __res = ::modf(static_cast<double>(__x), &__tmp);
528 *__iptr = static_cast<float>(__tmp);
533 #if _GLIBCXX_HAVE_MODFL
535 modf(long double __x, long double* __iptr) { return ::modfl(__x, __iptr); }
538 modf(long double __x, long double* __iptr)
541 double __res = ::modf(static_cast<double>(__x), &__tmp);
542 * __iptr = static_cast<long double>(__tmp);
547 template<typename _Tp>
549 __pow_helper(_Tp __x, int __n)
552 ? _Tp(1)/__cmath_power(__x, -__n)
553 : __cmath_power(__x, __n);
558 #if _GLIBCXX_HAVE_POWF
560 pow(float __x, float __y) { return __gnu_cxx::__c99_binding::powf(__x, __y); }
563 pow(float __x, float __y)
564 { return ::pow(static_cast<double>(__x), static_cast<double>(__y)); }
567 #if _GLIBCXX_HAVE_POWL
569 pow(long double __x, long double __y) { return ::powl(__x, __y); }
572 pow(long double __x, long double __y)
573 { return ::pow(static_cast<double>(__x), static_cast<double>(__y)); }
577 pow(double __x, int __i)
578 { return __pow_helper(__x, __i); }
581 pow(float __x, int __n)
582 { return __pow_helper(__x, __n); }
585 pow(long double __x, int __n)
586 { return __pow_helper(__x, __n); }
592 { return __builtin_sinf(__x); }
596 { return __builtin_sinl(__x); }
598 template<typename _Tp>
599 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
601 { return __builtin_sin(__x); }
605 #if _GLIBCXX_HAVE_SINHF
607 sinh(float __x) { return __gnu_cxx::__c99_binding::sinhf(__x); }
610 sinh(float __x) { return ::sinh(static_cast<double>(__x)); }
613 #if _GLIBCXX_HAVE_SINHL
615 sinh(long double __x) { return ::sinhl(__x); }
618 sinh(long double __x) { return ::sinh(static_cast<double>(__x)); }
621 template<typename _Tp>
622 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
624 { return ::sinh(static_cast<_Tp>(__x)); }
630 { return __builtin_sqrtf(__x); }
633 sqrt(long double __x)
634 { return __builtin_sqrtl(__x); }
636 template<typename _Tp>
637 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
639 { return __builtin_sqrt(__x); }
643 #if _GLIBCXX_HAVE_TANF
645 tan(float __x) { return __gnu_cxx::__c99_binding::tanf(__x); }
648 tan(float __x) { return ::tan(static_cast<double>(__x)); }
651 #if _GLIBCXX_HAVE_TANL
653 tan(long double __x) { return ::tanl(__x); }
656 tan(long double __x) { return ::tan(static_cast<double>(__x)); }
659 template<typename _Tp>
660 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
662 { return ::tan(static_cast<double>(__x)); }
666 #if _GLIBCXX_HAVE_TANHF
668 tanh(float __x) { return __gnu_cxx::__c99_binding::tanhf(__x); }
671 tanh(float __x) { return ::tanh(static_cast<double>(__x)); }
674 #if _GLIBCXX_HAVE_TANHL
676 tanh(long double __x) { return ::tanhl(__x); }
679 tanh(long double __x) { return ::tanh(static_cast<double>(__x)); }
682 template<typename _Tp>
683 inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
685 { return ::tanh(static_cast<double>(__x)); }
690 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
691 // These are possible macros imported from C99-land. For strict
692 // conformance, remove possible C99-injected names from the global
693 // namespace, and sequester them in the __gnu_cxx extension namespace.
696 template<typename _Tp>
698 __capture_fpclassify(_Tp __f) { return fpclassify(__f); }
700 template<typename _Tp>
702 __capture_isfinite(_Tp __f) { return isfinite(__f); }
704 template<typename _Tp>
706 __capture_isinf(_Tp __f) { return isinf(__f); }
708 template<typename _Tp>
710 __capture_isnan(_Tp __f) { return isnan(__f); }
712 template<typename _Tp>
714 __capture_isnormal(_Tp __f) { return isnormal(__f); }
716 template<typename _Tp>
718 __capture_signbit(_Tp __f) { return signbit(__f); }
720 template<typename _Tp>
722 __capture_isgreater(_Tp __f1, _Tp __f2)
723 { return isgreater(__f1, __f2); }
725 template<typename _Tp>
727 __capture_isgreaterequal(_Tp __f1, _Tp __f2)
728 { return isgreaterequal(__f1, __f2); }
730 template<typename _Tp>
732 __capture_isless(_Tp __f1, _Tp __f2) { return isless(__f1, __f2); }
734 template<typename _Tp>
736 __capture_islessequal(_Tp __f1, _Tp __f2)
737 { return islessequal(__f1, __f2); }
739 template<typename _Tp>
741 __capture_islessgreater(_Tp __f1, _Tp __f2)
742 { return islessgreater(__f1, __f2); }
744 template<typename _Tp>
746 __capture_isunordered(_Tp __f1, _Tp __f2)
747 { return isunordered(__f1, __f2); }
750 // Only undefine the C99 FP macros, if actually captured for namespace movement
758 #undef isgreaterequal
763 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
767 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
770 template<typename _Tp>
772 fpclassify(_Tp __f) { return __capture_fpclassify(__f); }
774 template<typename _Tp>
776 isfinite(_Tp __f) { return __capture_isfinite(__f); }
778 template<typename _Tp>
780 isinf(_Tp __f) { return __capture_isinf(__f); }
782 template<typename _Tp>
784 isnan(_Tp __f) { return __capture_isnan(__f); }
786 template<typename _Tp>
788 isnormal(_Tp __f) { return __capture_isnormal(__f); }
790 template<typename _Tp>
792 signbit(_Tp __f) { return __capture_signbit(__f); }
794 template<typename _Tp>
796 isgreater(_Tp __f1, _Tp __f2) { return __capture_isgreater(__f1, __f2); }
798 template<typename _Tp>
800 isgreaterequal(_Tp __f1, _Tp __f2)
801 { return __capture_isgreaterequal(__f1, __f2); }
803 template<typename _Tp>
805 isless(_Tp __f1, _Tp __f2) { return __capture_isless(__f1, __f2); }
807 template<typename _Tp>
809 islessequal(_Tp __f1, _Tp __f2)
810 { return __capture_islessequal(__f1, __f2); }
812 template<typename _Tp>
814 islessgreater(_Tp __f1, _Tp __f2)
815 { return __capture_islessgreater(__f1, __f2); }
817 template<typename _Tp>
819 isunordered(_Tp __f1, _Tp __f2)
820 { return __capture_isunordered(__f1, __f2); }
825 using __gnu_cxx::fpclassify;
826 using __gnu_cxx::isfinite;
827 using __gnu_cxx::isinf;
828 using __gnu_cxx::isnan;
829 using __gnu_cxx::isnormal;
830 using __gnu_cxx::signbit;
831 using __gnu_cxx::isgreater;
832 using __gnu_cxx::isgreaterequal;
833 using __gnu_cxx::isless;
834 using __gnu_cxx::islessequal;
835 using __gnu_cxx::islessgreater;
836 using __gnu_cxx::isunordered;
838 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
841 #ifdef _GLIBCXX_NO_TEMPLATE_EXPORT
843 # include <bits/cmath.tcc>