# 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, 2010
    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 @a *.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   template<typename _Tp>
    00106     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00107                        double>::__type
    00108     abs(_Tp __x)
    00109     { return __builtin_fabs(__x); }
    00110
    00111   using ::acos;
    00112
    00113   inline float
    00114   acos(float __x)
    00115   { return __builtin_acosf(__x); }
    00116
    00117   inline long double
    00118   acos(long double __x)
    00119   { return __builtin_acosl(__x); }
    00120
    00121   template<typename _Tp>
    00122     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00123                        double>::__type
    00124     acos(_Tp __x)
    00125     { return __builtin_acos(__x); }
    00126
    00127   using ::asin;
    00128
    00129   inline float
    00130   asin(float __x)
    00131   { return __builtin_asinf(__x); }
    00132
    00133   inline long double
    00134   asin(long double __x)
    00135   { return __builtin_asinl(__x); }
    00136
    00137   template<typename _Tp>
    00138     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00139                        double>::__type
    00140     asin(_Tp __x)
    00141     { return __builtin_asin(__x); }
    00142
    00143   using ::atan;
    00144
    00145   inline float
    00146   atan(float __x)
    00147   { return __builtin_atanf(__x); }
    00148
    00149   inline long double
    00150   atan(long double __x)
    00151   { return __builtin_atanl(__x); }
    00152
    00153   template<typename _Tp>
    00154     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00155                        double>::__type
    00156     atan(_Tp __x)
    00157     { return __builtin_atan(__x); }
    00158
    00159   using ::atan2;
    00160
    00161   inline float
    00162   atan2(float __y, float __x)
    00163   { return __builtin_atan2f(__y, __x); }
    00164
    00165   inline long double
    00166   atan2(long double __y, long double __x)
    00167   { return __builtin_atan2l(__y, __x); }
    00168
    00169   template<typename _Tp, typename _Up>
    00170     inline
    00171     typename __gnu_cxx::__promote_2<
    00172     typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
    00173                     && __is_arithmetic<_Up>::__value,
    00174                     _Tp>::__type, _Up>::__type
    00175     atan2(_Tp __y, _Up __x)
    00176     {
    00177       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
    00178       return atan2(__type(__y), __type(__x));
    00179     }
    00180
    00181   using ::ceil;
    00182
    00183   inline float
    00184   ceil(float __x)
    00185   { return __builtin_ceilf(__x); }
    00186
    00187   inline long double
    00188   ceil(long double __x)
    00189   { return __builtin_ceill(__x); }
    00190
    00191   template<typename _Tp>
    00192     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00193                        double>::__type
    00194     ceil(_Tp __x)
    00195     { return __builtin_ceil(__x); }
    00196
    00197   using ::cos;
    00198
    00199   inline float
    00200   cos(float __x)
    00201   { return __builtin_cosf(__x); }
    00202
    00203   inline long double
    00204   cos(long double __x)
    00205   { return __builtin_cosl(__x); }
    00206
    00207   template<typename _Tp>
    00208     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00209                        double>::__type
    00210     cos(_Tp __x)
    00211     { return __builtin_cos(__x); }
    00212
    00213   using ::cosh;
    00214
    00215   inline float
    00216   cosh(float __x)
    00217   { return __builtin_coshf(__x); }
    00218
    00219   inline long double
    00220   cosh(long double __x)
    00221   { return __builtin_coshl(__x); }
    00222
    00223   template<typename _Tp>
    00224     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00225                        double>::__type
    00226     cosh(_Tp __x)
    00227     { return __builtin_cosh(__x); }
    00228
    00229   using ::exp;
    00230
    00231   inline float
    00232   exp(float __x)
    00233   { return __builtin_expf(__x); }
    00234
    00235   inline long double
    00236   exp(long double __x)
    00237   { return __builtin_expl(__x); }
    00238
    00239   template<typename _Tp>
    00240     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00241                        double>::__type
    00242     exp(_Tp __x)
    00243     { return __builtin_exp(__x); }
    00244
    00245   using ::fabs;
    00246
    00247   inline float
    00248   fabs(float __x)
    00249   { return __builtin_fabsf(__x); }
    00250
    00251   inline long double
    00252   fabs(long double __x)
    00253   { return __builtin_fabsl(__x); }
    00254
    00255   template<typename _Tp>
    00256     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00257                        double>::__type
    00258     fabs(_Tp __x)
    00259     { return __builtin_fabs(__x); }
    00260
    00261   using ::floor;
    00262
    00263   inline float
    00264   floor(float __x)
    00265   { return __builtin_floorf(__x); }
    00266
    00267   inline long double
    00268   floor(long double __x)
    00269   { return __builtin_floorl(__x); }
    00270
    00271   template<typename _Tp>
    00272     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00273                        double>::__type
    00274     floor(_Tp __x)
    00275     { return __builtin_floor(__x); }
    00276
    00277   using ::fmod;
    00278
    00279   inline float
    00280   fmod(float __x, float __y)
    00281   { return __builtin_fmodf(__x, __y); }
    00282
    00283   inline long double
    00284   fmod(long double __x, long double __y)
    00285   { return __builtin_fmodl(__x, __y); }
    00286
    00287   using ::frexp;
    00288
    00289   inline float
    00290   frexp(float __x, int* __exp)
    00291   { return __builtin_frexpf(__x, __exp); }
    00292
    00293   inline long double
    00294   frexp(long double __x, int* __exp)
    00295   { return __builtin_frexpl(__x, __exp); }
    00296
    00297   template<typename _Tp>
    00298     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00299                        double>::__type
    00300     frexp(_Tp __x, int* __exp)
    00301     { return __builtin_frexp(__x, __exp); }
    00302
    00303   using ::ldexp;
    00304
    00305   inline float
    00306   ldexp(float __x, int __exp)
    00307   { return __builtin_ldexpf(__x, __exp); }
    00308
    00309   inline long double
    00310   ldexp(long double __x, int __exp)
    00311   { return __builtin_ldexpl(__x, __exp); }
    00312
    00313   template<typename _Tp>
    00314     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00315                        double>::__type
    00316   ldexp(_Tp __x, int __exp)
    00317   { return __builtin_ldexp(__x, __exp); }
    00318
    00319   using ::log;
    00320
    00321   inline float
    00322   log(float __x)
    00323   { return __builtin_logf(__x); }
    00324
    00325   inline long double
    00326   log(long double __x)
    00327   { return __builtin_logl(__x); }
    00328
    00329   template<typename _Tp>
    00330     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00331                        double>::__type
    00332     log(_Tp __x)
    00333     { return __builtin_log(__x); }
    00334
    00335   using ::log10;
    00336
    00337   inline float
    00338   log10(float __x)
    00339   { return __builtin_log10f(__x); }
    00340
    00341   inline long double
    00342   log10(long double __x)
    00343   { return __builtin_log10l(__x); }
    00344
    00345   template<typename _Tp>
    00346     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00347                        double>::__type
    00348     log10(_Tp __x)
    00349     { return __builtin_log10(__x); }
    00350
    00351   using ::modf;
    00352
    00353   inline float
    00354   modf(float __x, float* __iptr)
    00355   { return __builtin_modff(__x, __iptr); }
    00356
    00357   inline long double
    00358   modf(long double __x, long double* __iptr)
    00359   { return __builtin_modfl(__x, __iptr); }
    00360
    00361   using ::pow;
    00362
    00363   inline float
    00364   pow(float __x, float __y)
    00365   { return __builtin_powf(__x, __y); }
    00366
    00367   inline long double
    00368   pow(long double __x, long double __y)
    00369   { return __builtin_powl(__x, __y); }
    00370
    00371 #ifndef __GXX_EXPERIMENTAL_CXX0X__
    00372   // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00373   // DR 550. What should the return type of pow(float,int) be?
    00374   inline double
    00375   pow(double __x, int __i)
    00376   { return __builtin_powi(__x, __i); }
    00377
    00378   inline float
    00379   pow(float __x, int __n)
    00380   { return __builtin_powif(__x, __n); }
    00381
    00382   inline long double
    00383   pow(long double __x, int __n)
    00384   { return __builtin_powil(__x, __n); }
    00385 #endif
    00386
    00387   template<typename _Tp, typename _Up>
    00388     inline
    00389     typename __gnu_cxx::__promote_2<
    00390     typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
    00391                     && __is_arithmetic<_Up>::__value,
    00392                     _Tp>::__type, _Up>::__type
    00393     pow(_Tp __x, _Up __y)
    00394     {
    00395       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
    00396       return pow(__type(__x), __type(__y));
    00397     }
    00398
    00399   using ::sin;
    00400
    00401   inline float
    00402   sin(float __x)
    00403   { return __builtin_sinf(__x); }
    00404
    00405   inline long double
    00406   sin(long double __x)
    00407   { return __builtin_sinl(__x); }
    00408
    00409   template<typename _Tp>
    00410     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00411                        double>::__type
    00412     sin(_Tp __x)
    00413     { return __builtin_sin(__x); }
    00414
    00415   using ::sinh;
    00416
    00417   inline float
    00418   sinh(float __x)
    00419   { return __builtin_sinhf(__x); }
    00420
    00421   inline long double
    00422   sinh(long double __x)
    00423   { return __builtin_sinhl(__x); }
    00424
    00425   template<typename _Tp>
    00426     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00427                        double>::__type
    00428     sinh(_Tp __x)
    00429     { return __builtin_sinh(__x); }
    00430
    00431   using ::sqrt;
    00432
    00433   inline float
    00434   sqrt(float __x)
    00435   { return __builtin_sqrtf(__x); }
    00436
    00437   inline long double
    00438   sqrt(long double __x)
    00439   { return __builtin_sqrtl(__x); }
    00440
    00441   template<typename _Tp>
    00442     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00443                        double>::__type
    00444     sqrt(_Tp __x)
    00445     { return __builtin_sqrt(__x); }
    00446
    00447   using ::tan;
    00448
    00449   inline float
    00450   tan(float __x)
    00451   { return __builtin_tanf(__x); }
    00452
    00453   inline long double
    00454   tan(long double __x)
    00455   { return __builtin_tanl(__x); }
    00456
    00457   template<typename _Tp>
    00458     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00459                        double>::__type
    00460     tan(_Tp __x)
    00461     { return __builtin_tan(__x); }
    00462
    00463   using ::tanh;
    00464
    00465   inline float
    00466   tanh(float __x)
    00467   { return __builtin_tanhf(__x); }
    00468
    00469   inline long double
    00470   tanh(long double __x)
    00471   { return __builtin_tanhl(__x); }
    00472
    00473   template<typename _Tp>
    00474     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    00475                        double>::__type
    00476     tanh(_Tp __x)
    00477     { return __builtin_tanh(__x); }
    00478
    00479 _GLIBCXX_END_NAMESPACE
    00480
    00481 #if _GLIBCXX_USE_C99_MATH
    00482 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
    00483
    00484 // These are possible macros imported from C99-land.
    00485 #undef fpclassify
    00486 #undef isfinite
    00487 #undef isinf
    00488 #undef isnan
    00489 #undef isnormal
    00490 #undef signbit
    00491 #undef isgreater
    00492 #undef isgreaterequal
    00493 #undef isless
    00494 #undef islessequal
    00495 #undef islessgreater
    00496 #undef isunordered
    00497
    00498 _GLIBCXX_BEGIN_NAMESPACE(std)
    00499
    00500   template<typename _Tp>
    00501     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00502                        int>::__type
    00503     fpclassify(_Tp __f)
    00504     {
    00505       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00506       return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
    00507                   FP_SUBNORMAL, FP_ZERO, __type(__f));
    00508     }
    00509
    00510   template<typename _Tp>
    00511     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00512                        int>::__type
    00513     isfinite(_Tp __f)
    00514     {
    00515       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00516       return __builtin_isfinite(__type(__f));
    00517     }
    00518
    00519   template<typename _Tp>
    00520     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00521                        int>::__type
    00522     isinf(_Tp __f)
    00523     {
    00524       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00525       return __builtin_isinf(__type(__f));
    00526     }
    00527
    00528   template<typename _Tp>
    00529     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00530                        int>::__type
    00531     isnan(_Tp __f)
    00532     {
    00533       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00534       return __builtin_isnan(__type(__f));
    00535     }
    00536
    00537   template<typename _Tp>
    00538     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00539                        int>::__type
    00540     isnormal(_Tp __f)
    00541     {
    00542       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00543       return __builtin_isnormal(__type(__f));
    00544     }
    00545
    00546   template<typename _Tp>
    00547     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00548                        int>::__type
    00549     signbit(_Tp __f)
    00550     {
    00551       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00552       return __builtin_signbit(__type(__f));
    00553     }
    00554
    00555   template<typename _Tp>
    00556     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00557                        int>::__type
    00558     isgreater(_Tp __f1, _Tp __f2)
    00559     {
    00560       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00561       return __builtin_isgreater(__type(__f1), __type(__f2));
    00562     }
    00563
    00564   template<typename _Tp>
    00565     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00566                        int>::__type
    00567     isgreaterequal(_Tp __f1, _Tp __f2)
    00568     {
    00569       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00570       return __builtin_isgreaterequal(__type(__f1), __type(__f2));
    00571     }
    00572
    00573   template<typename _Tp>
    00574     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00575                        int>::__type
    00576     isless(_Tp __f1, _Tp __f2)
    00577     {
    00578       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00579       return __builtin_isless(__type(__f1), __type(__f2));
    00580     }
    00581
    00582   template<typename _Tp>
    00583     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00584                        int>::__type
    00585     islessequal(_Tp __f1, _Tp __f2)
    00586     {
    00587       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00588       return __builtin_islessequal(__type(__f1), __type(__f2));
    00589     }
    00590
    00591   template<typename _Tp>
    00592     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00593                        int>::__type
    00594     islessgreater(_Tp __f1, _Tp __f2)
    00595     {
    00596       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00597       return __builtin_islessgreater(__type(__f1), __type(__f2));
    00598     }
    00599
    00600   template<typename _Tp>
    00601     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    00602                        int>::__type
    00603     isunordered(_Tp __f1, _Tp __f2)
    00604     {
    00605       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    00606       return __builtin_isunordered(__type(__f1), __type(__f2));
    00607     }
    00608
    00609 _GLIBCXX_END_NAMESPACE
    00610
    00611 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
    00612 #endif
    00613
    00614 #ifndef _GLIBCXX_EXPORT_TEMPLATE
    00615 # include <bits/cmath.tcc>
    00616 #endif
    00617
    00618 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00619 #  if defined(_GLIBCXX_INCLUDE_AS_TR1)
    00620 #    error C++0x header cannot be included from TR1 header
    00621 #  endif
    00622 #  if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
    00623 #    include <tr1_impl/cmath>
    00624 #  else
    00625 #    define _GLIBCXX_INCLUDE_AS_CXX0X
    00626 #    define _GLIBCXX_BEGIN_NAMESPACE_TR1
    00627 #    define _GLIBCXX_END_NAMESPACE_TR1
    00628 #    define _GLIBCXX_TR1
    00629 #    include <tr1_impl/cmath>
    00630 #    undef _GLIBCXX_TR1
    00631 #    undef _GLIBCXX_END_NAMESPACE_TR1
    00632 #    undef _GLIBCXX_BEGIN_NAMESPACE_TR1
    00633 #    undef _GLIBCXX_INCLUDE_AS_CXX0X
    00634 #  endif
    00635 #endif
    00636
    00637 #endif