# cmath

Go to the documentation of this file.

    00001 // -*- C++ -*- C forwarding header.
    00002
    00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
    00004 // 2006, 2007, 2008, 2009
    00005 // Free Software Foundation, Inc.
    00006 //
    00007 // This file is part of the GNU ISO C++ Library.  This library is free
    00008 // software; you can redistribute it and/or modify it under the
    00009 // terms of the GNU General Public License as published by the
    00010 // Free Software Foundation; either version 3, or (at your option)
    00011 // any later version.
    00012
    00013 // This library is distributed in the hope that it will be useful,
    00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00016 // GNU General Public License for more details.
    00017
    00018 // Under Section 7 of GPL version 3, you are granted additional
    00019 // permissions described in the GCC Runtime Library Exception, version
    00020 // 3.1, as published by the Free Software Foundation.
    00021
    00022 // You should have received a copy of the GNU General Public License and
    00023 // a copy of the GCC Runtime Library Exception along with this program;
    00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    00025 // <http://www.gnu.org/licenses/>.
    00026
    00027 /** @file include/cmath
    00028  *  This is a Standard C++ Library file.  You should @c #include this file
    00029  *  in your programs, rather than any of the "*.h" implementation files.
    00030  *
    00031  *  This is the C++ version of the Standard C Library header @c math.h,
    00032  *  and its contents are (mostly) the same as that header, but are all
    00033  *  contained in the namespace @c std (except for names which are defined
    00034  *  as macros in C).
    00035  */
    00036
    00037 //
    00038 // ISO C++ 14882: 26.5  C library
    00039 //
    00040
    00041 #pragma GCC system_header
    00042
    00043 #include <bits/c++config.h>
    00044 #include <bits/cpp_type_traits.h>
    00045 #include <ext/type_traits.h>
    00046 #include <math.h>
    00047
    00048 #ifndef _GLIBCXX_CMATH
    00049 #define _GLIBCXX_CMATH 1
    00050
    00051 // Get rid of those macros defined in <math.h> in lieu of real functions.
    00052 #undef abs
    00053 #undef div
    00054 #undef acos
    00055 #undef asin
    00056 #undef atan
    00057 #undef atan2
    00058 #undef ceil
    00059 #undef cos
    00060 #undef cosh
    00061 #undef exp
    00062 #undef fabs
    00063 #undef floor
    00064 #undef fmod
    00065 #undef frexp
    00066 #undef ldexp
    00067 #undef log
    00068 #undef log10
    00069 #undef modf
    00070 #undef pow
    00071 #undef sin
    00072 #undef sinh
    00073 #undef sqrt
    00074 #undef tan
    00075 #undef tanh
    00076
    00077 _GLIBCXX_BEGIN_NAMESPACE(std)
    00078
    00079   // Forward declaration of a helper function.  This really should be
    00080   // an `exported' forward declaration.
    00081   template<typename _Tp>
    00082     _Tp __cmath_power(_Tp, unsigned int);
    00083
    00084   template<typename _Tp>
    00085     inline _Tp
    00086     __pow_helper(_Tp __x, int __n)
    00087     {
    00088       return __n < 0
    00089         ? _Tp(1)/__cmath_power(__x, -__n)
    00090         : __cmath_power(__x, __n);
    00091     }
    00092
    00093   inline double
    00094   abs(double __x)
    00095   { return __builtin_fabs(__x); }
    00096
    00097   inline float
    00098   abs(float __x)
    00099   { return __builtin_fabsf(__x); }
    00100
    00101   inline long double
    00102   abs(long double __x)
    00103   { return __builtin_fabsl(__x); }
    00104
    00105   using ::acos;
    00106
    00107   inline float
    00108   acos(float __x)
    00109   { return __builtin_acosf(__x); }
    00110
    00111   inline long double
    00112   acos(long double __x)
    00113   { return __builtin_acosl(__x); }
    00114
    00115   template<typename _Tp>
    00116     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00117                        double>::__type
    00118     acos(_Tp __x)
    00119     { return __builtin_acos(__x); }
    00120
    00121   using ::asin;
    00122
    00123   inline float
    00124   asin(float __x)
    00125   { return __builtin_asinf(__x); }
    00126
    00127   inline long double
    00128   asin(long double __x)
    00129   { return __builtin_asinl(__x); }
    00130
    00131   template<typename _Tp>
    00132     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00133                        double>::__type
    00134     asin(_Tp __x)
    00135     { return __builtin_asin(__x); }
    00136
    00137   using ::atan;
    00138
    00139   inline float
    00140   atan(float __x)
    00141   { return __builtin_atanf(__x); }
    00142
    00143   inline long double
    00144   atan(long double __x)
    00145   { return __builtin_atanl(__x); }
    00146
    00147   template<typename _Tp>
    00148     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00149                        double>::__type
    00150     atan(_Tp __x)
    00151     { return __builtin_atan(__x); }
    00152
    00153   using ::atan2;
    00154
    00155   inline float
    00156   atan2(float __y, float __x)
    00157   { return __builtin_atan2f(__y, __x); }
    00158
    00159   inline long double
    00160   atan2(long double __y, long double __x)
    00161   { return __builtin_atan2l(__y, __x); }
    00162
    00163   template<typename _Tp, typename _Up>
    00164     inline
    00165     typename __gnu_cxx::__promote_2<
    00166     typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
    00167                     && __is_arithmetic<_Up>::__value,
    00168                     _Tp>::__type, _Up>::__type
    00169     atan2(_Tp __y, _Up __x)
    00170     {
    00171       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
    00172       return atan2(__type(__y), __type(__x));
    00173     }
    00174
    00175   using ::ceil;
    00176
    00177   inline float
    00178   ceil(float __x)
    00179   { return __builtin_ceilf(__x); }
    00180
    00181   inline long double
    00182   ceil(long double __x)
    00183   { return __builtin_ceill(__x); }
    00184
    00185   template<typename _Tp>
    00186     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00187                        double>::__type
    00188     ceil(_Tp __x)
    00189     { return __builtin_ceil(__x); }
    00190
    00191   using ::cos;
    00192
    00193   inline float
    00194   cos(float __x)
    00195   { return __builtin_cosf(__x); }
    00196
    00197   inline long double
    00198   cos(long double __x)
    00199   { return __builtin_cosl(__x); }
    00200
    00201   template<typename _Tp>
    00202     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00203                        double>::__type
    00204     cos(_Tp __x)
    00205     { return __builtin_cos(__x); }
    00206
    00207   using ::cosh;
    00208
    00209   inline float
    00210   cosh(float __x)
    00211   { return __builtin_coshf(__x); }
    00212
    00213   inline long double
    00214   cosh(long double __x)
    00215   { return __builtin_coshl(__x); }
    00216
    00217   template<typename _Tp>
    00218     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00219                        double>::__type
    00220     cosh(_Tp __x)
    00221     { return __builtin_cosh(__x); }
    00222
    00223   using ::exp;
    00224
    00225   inline float
    00226   exp(float __x)
    00227   { return __builtin_expf(__x); }
    00228
    00229   inline long double
    00230   exp(long double __x)
    00231   { return __builtin_expl(__x); }
    00232
    00233   template<typename _Tp>
    00234     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00235                        double>::__type
    00236     exp(_Tp __x)
    00237     { return __builtin_exp(__x); }
    00238
    00239   using ::fabs;
    00240
    00241   inline float
    00242   fabs(float __x)
    00243   { return __builtin_fabsf(__x); }
    00244
    00245   inline long double
    00246   fabs(long double __x)
    00247   { return __builtin_fabsl(__x); }
    00248
    00249   template<typename _Tp>
    00250     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00251                        double>::__type
    00252     fabs(_Tp __x)
    00253     { return __builtin_fabs(__x); }
    00254
    00255   using ::floor;
    00256
    00257   inline float
    00258   floor(float __x)
    00259   { return __builtin_floorf(__x); }
    00260
    00261   inline long double
    00262   floor(long double __x)
    00263   { return __builtin_floorl(__x); }
    00264
    00265   template<typename _Tp>
    00266     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00267                        double>::__type
    00268     floor(_Tp __x)
    00269     { return __builtin_floor(__x); }
    00270
    00271   using ::fmod;
    00272
    00273   inline float
    00274   fmod(float __x, float __y)
    00275   { return __builtin_fmodf(__x, __y); }
    00276
    00277   inline long double
    00278   fmod(long double __x, long double __y)
    00279   { return __builtin_fmodl(__x, __y); }
    00280
    00281   using ::frexp;
    00282
    00283   inline float
    00284   frexp(float __x, int* __exp)
    00285   { return __builtin_frexpf(__x, __exp); }
    00286
    00287   inline long double
    00288   frexp(long double __x, int* __exp)
    00289   { return __builtin_frexpl(__x, __exp); }
    00290
    00291   template<typename _Tp>
    00292     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00293                        double>::__type
    00294     frexp(_Tp __x, int* __exp)
    00295     { return __builtin_frexp(__x, __exp); }
    00296
    00297   using ::ldexp;
    00298
    00299   inline float
    00300   ldexp(float __x, int __exp)
    00301   { return __builtin_ldexpf(__x, __exp); }
    00302
    00303   inline long double
    00304   ldexp(long double __x, int __exp)
    00305   { return __builtin_ldexpl(__x, __exp); }
    00306
    00307   template<typename _Tp>
    00308     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00309                        double>::__type
    00310   ldexp(_Tp __x, int __exp)
    00311   { return __builtin_ldexp(__x, __exp); }
    00312
    00313   using ::log;
    00314
    00315   inline float
    00316   log(float __x)
    00317   { return __builtin_logf(__x); }
    00318
    00319   inline long double
    00320   log(long double __x)
    00321   { return __builtin_logl(__x); }
    00322
    00323   template<typename _Tp>
    00324     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00325                        double>::__type
    00326     log(_Tp __x)
    00327     { return __builtin_log(__x); }
    00328
    00329   using ::log10;
    00330
    00331   inline float
    00332   log10(float __x)
    00333   { return __builtin_log10f(__x); }
    00334
    00335   inline long double
    00336   log10(long double __x)
    00337   { return __builtin_log10l(__x); }
    00338
    00339   template<typename _Tp>
    00340     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00341                        double>::__type
    00342     log10(_Tp __x)
    00343     { return __builtin_log10(__x); }
    00344
    00345   using ::modf;
    00346
    00347   inline float
    00348   modf(float __x, float* __iptr)
    00349   { return __builtin_modff(__x, __iptr); }
    00350
    00351   inline long double
    00352   modf(long double __x, long double* __iptr)
    00353   { return __builtin_modfl(__x, __iptr); }
    00354
    00355   using ::pow;
    00356
    00357   inline float
    00358   pow(float __x, float __y)
    00359   { return __builtin_powf(__x, __y); }
    00360
    00361   inline long double
    00362   pow(long double __x, long double __y)
    00363   { return __builtin_powl(__x, __y); }
    00364
    00365 #ifndef __GXX_EXPERIMENTAL_CXX0X__
    00366   // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00367   // DR 550. What should the return type of pow(float,int) be?
    00368   inline double
    00369   pow(double __x, int __i)
    00370   { return __builtin_powi(__x, __i); }
    00371
    00372   inline float
    00373   pow(float __x, int __n)
    00374   { return __builtin_powif(__x, __n); }
    00375
    00376   inline long double
    00377   pow(long double __x, int __n)
    00378   { return __builtin_powil(__x, __n); }
    00379 #endif
    00380
    00381   template<typename _Tp, typename _Up>
    00382     inline
    00383     typename __gnu_cxx::__promote_2<
    00384     typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
    00385                     && __is_arithmetic<_Up>::__value,
    00386                     _Tp>::__type, _Up>::__type
    00387     pow(_Tp __x, _Up __y)
    00388     {
    00389       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
    00390       return pow(__type(__x), __type(__y));
    00391     }
    00392
    00393   using ::sin;
    00394
    00395   inline float
    00396   sin(float __x)
    00397   { return __builtin_sinf(__x); }
    00398
    00399   inline long double
    00400   sin(long double __x)
    00401   { return __builtin_sinl(__x); }
    00402
    00403   template<typename _Tp>
    00404     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00405                        double>::__type
    00406     sin(_Tp __x)
    00407     { return __builtin_sin(__x); }
    00408
    00409   using ::sinh;
    00410
    00411   inline float
    00412   sinh(float __x)
    00413   { return __builtin_sinhf(__x); }
    00414
    00415   inline long double
    00416   sinh(long double __x)
    00417   { return __builtin_sinhl(__x); }
    00418
    00419   template<typename _Tp>
    00420     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00421                        double>::__type
    00422     sinh(_Tp __x)
    00423     { return __builtin_sinh(__x); }
    00424
    00425   using ::sqrt;
    00426
    00427   inline float
    00428   sqrt(float __x)
    00429   { return __builtin_sqrtf(__x); }
    00430
    00431   inline long double
    00432   sqrt(long double __x)
    00433   { return __builtin_sqrtl(__x); }
    00434
    00435   template<typename _Tp>
    00436     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00437                        double>::__type
    00438     sqrt(_Tp __x)
    00439     { return __builtin_sqrt(__x); }
    00440
    00441   using ::tan;
    00442
    00443   inline float
    00444   tan(float __x)
    00445   { return __builtin_tanf(__x); }
    00446
    00447   inline long double
    00448   tan(long double __x)
    00449   { return __builtin_tanl(__x); }
    00450
    00451   template<typename _Tp>
    00452     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00453                        double>::__type
    00454     tan(_Tp __x)
    00455     { return __builtin_tan(__x); }
    00456
    00457   using ::tanh;
    00458
    00459   inline float
    00460   tanh(float __x)
    00461   { return __builtin_tanhf(__x); }
    00462
    00463   inline long double
    00464   tanh(long double __x)
    00465   { return __builtin_tanhl(__x); }
    00466
    00467   template<typename _Tp>
    00468     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00469                        double>::__type
    00470     tanh(_Tp __x)
    00471     { return __builtin_tanh(__x); }
    00472
    00473 _GLIBCXX_END_NAMESPACE
    00474
    00475 #if _GLIBCXX_USE_C99_MATH
    00476 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
    00477
    00478 // These are possible macros imported from C99-land.
    00479 #undef fpclassify
    00480 #undef isfinite
    00481 #undef isinf
    00482 #undef isnan
    00483 #undef isnormal
    00484 #undef signbit
    00485 #undef isgreater
    00486 #undef isgreaterequal
    00487 #undef isless
    00488 #undef islessequal
    00489 #undef islessgreater
    00490 #undef isunordered
    00491
    00492 _GLIBCXX_BEGIN_NAMESPACE(std)
    00493
    00494   template<typename _Tp>
    00495     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00496                        int>::__type
    00497     fpclassify(_Tp __f)
    00498     {
    00499       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00500       return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
    00501                   FP_SUBNORMAL, FP_ZERO, __type(__f));
    00502     }
    00503
    00504   template<typename _Tp>
    00505     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00506                        int>::__type
    00507     isfinite(_Tp __f)
    00508     {
    00509       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00510       return __builtin_isfinite(__type(__f));
    00511     }
    00512
    00513   template<typename _Tp>
    00514     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00515                        int>::__type
    00516     isinf(_Tp __f)
    00517     {
    00518       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00519       return __builtin_isinf(__type(__f));
    00520     }
    00521
    00522   template<typename _Tp>
    00523     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00524                        int>::__type
    00525     isnan(_Tp __f)
    00526     {
    00527       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00528       return __builtin_isnan(__type(__f));
    00529     }
    00530
    00531   template<typename _Tp>
    00532     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00533                        int>::__type
    00534     isnormal(_Tp __f)
    00535     {
    00536       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00537       return __builtin_isnormal(__type(__f));
    00538     }
    00539
    00540   template<typename _Tp>
    00541     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00542                        int>::__type
    00543     signbit(_Tp __f)
    00544     {
    00545       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00546       return __builtin_signbit(__type(__f));
    00547     }
    00548
    00549   template<typename _Tp>
    00550     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00551                        int>::__type
    00552     isgreater(_Tp __f1, _Tp __f2)
    00553     {
    00554       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00555       return __builtin_isgreater(__type(__f1), __type(__f2));
    00556     }
    00557
    00558   template<typename _Tp>
    00559     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00560                        int>::__type
    00561     isgreaterequal(_Tp __f1, _Tp __f2)
    00562     {
    00563       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00564       return __builtin_isgreaterequal(__type(__f1), __type(__f2));
    00565     }
    00566
    00567   template<typename _Tp>
    00568     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00569                        int>::__type
    00570     isless(_Tp __f1, _Tp __f2)
    00571     {
    00572       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00573       return __builtin_isless(__type(__f1), __type(__f2));
    00574     }
    00575
    00576   template<typename _Tp>
    00577     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00578                        int>::__type
    00579     islessequal(_Tp __f1, _Tp __f2)
    00580     {
    00581       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00582       return __builtin_islessequal(__type(__f1), __type(__f2));
    00583     }
    00584
    00585   template<typename _Tp>
    00586     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00587                        int>::__type
    00588     islessgreater(_Tp __f1, _Tp __f2)
    00589     {
    00590       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00591       return __builtin_islessgreater(__type(__f1), __type(__f2));
    00592     }
    00593
    00594   template<typename _Tp>
    00595     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00596                        int>::__type
    00597     isunordered(_Tp __f1, _Tp __f2)
    00598     {
    00599       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00600       return __builtin_isunordered(__type(__f1), __type(__f2));
    00601     }
    00602
    00603 _GLIBCXX_END_NAMESPACE
    00604
    00605 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
    00606 #endif
    00607
    00608 #ifndef _GLIBCXX_EXPORT_TEMPLATE
    00609 # include <bits/cmath.tcc>
    00610 #endif
    00611
    00612 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00613 #  if defined(_GLIBCXX_INCLUDE_AS_TR1)
    00614 #    error C++0x header cannot be included from TR1 header
    00615 #  endif
    00616 #  if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
    00617 #    include <tr1_impl/cmath>
    00618 #  else
    00619 #    define _GLIBCXX_INCLUDE_AS_CXX0X
    00620 #    define _GLIBCXX_BEGIN_NAMESPACE_TR1
    00621 #    define _GLIBCXX_END_NAMESPACE_TR1
    00622 #    define _GLIBCXX_TR1
    00623 #    include <tr1_impl/cmath>
    00624 #    undef _GLIBCXX_TR1
    00625 #    undef _GLIBCXX_END_NAMESPACE_TR1
    00626 #    undef _GLIBCXX_BEGIN_NAMESPACE_TR1
    00627 #    undef _GLIBCXX_INCLUDE_AS_CXX0X
    00628 #  endif
    00629 #endif
    00630
    00631 #endif