Back to index

glibc  2.9
math.h
Go to the documentation of this file.
00001 /* Declarations for math functions.
00002    Copyright (C) 1991-1993, 1995-1999, 2001, 2002, 2004, 2006
00003    Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 /*
00022  *     ISO C99 Standard: 7.12 Mathematics <math.h>
00023  */
00024 
00025 #ifndef       _MATH_H
00026 #define       _MATH_H       1
00027 
00028 #include <features.h>
00029 
00030 __BEGIN_DECLS
00031 
00032 /* Get machine-dependent HUGE_VAL value (returned on overflow).
00033    On all IEEE754 machines, this is +Infinity.  */
00034 #include <bits/huge_val.h>
00035 #ifdef __USE_ISOC99
00036 # include <bits/huge_valf.h>
00037 # include <bits/huge_vall.h>
00038 
00039 /* Get machine-dependent INFINITY value.  */
00040 # include <bits/inf.h>
00041 
00042 /* Get machine-dependent NAN value (returned for some domain errors).  */
00043 # include <bits/nan.h>
00044 #endif /* __USE_ISOC99 */
00045 
00046 /* Get general and ISO C99 specific information.  */
00047 #include <bits/mathdef.h>
00048 
00049 /* The file <bits/mathcalls.h> contains the prototypes for all the
00050    actual math functions.  These macros are used for those prototypes,
00051    so we can easily declare each function as both `name' and `__name',
00052    and can declare the float versions `namef' and `__namef'.  */
00053 
00054 #define __MATHCALL(function,suffix, args) \
00055   __MATHDECL (_Mdouble_,function,suffix, args)
00056 #define __MATHDECL(type, function,suffix, args) \
00057   __MATHDECL_1(type, function,suffix, args); \
00058   __MATHDECL_1(type, __CONCAT(__,function),suffix, args)
00059 #define __MATHCALLX(function,suffix, args, attrib)      \
00060   __MATHDECLX (_Mdouble_,function,suffix, args, attrib)
00061 #define __MATHDECLX(type, function,suffix, args, attrib) \
00062   __MATHDECL_1(type, function,suffix, args) __attribute__ (attrib); \
00063   __MATHDECL_1(type, __CONCAT(__,function),suffix, args) __attribute__ (attrib)
00064 #define __MATHDECL_1(type, function,suffix, args) \
00065   extern type __MATH_PRECNAME(function,suffix) args __THROW
00066 
00067 #define _Mdouble_           double
00068 #define __MATH_PRECNAME(name,r)    __CONCAT(name,r)
00069 # define _Mdouble_BEGIN_NAMESPACE __BEGIN_NAMESPACE_STD
00070 # define _Mdouble_END_NAMESPACE   __END_NAMESPACE_STD
00071 #include <bits/mathcalls.h>
00072 #undef _Mdouble_
00073 #undef _Mdouble_BEGIN_NAMESPACE
00074 #undef _Mdouble_END_NAMESPACE
00075 #undef __MATH_PRECNAME
00076 
00077 #if defined __USE_MISC || defined __USE_ISOC99
00078 
00079 
00080 /* Include the file of declarations again, this time using `float'
00081    instead of `double' and appending f to each function name.  */
00082 
00083 # ifndef _Mfloat_
00084 #  define _Mfloat_          float
00085 # endif
00086 # define _Mdouble_          _Mfloat_
00087 # ifdef __STDC__
00088 #  define __MATH_PRECNAME(name,r) name##f##r
00089 # else
00090 #  define __MATH_PRECNAME(name,r) namefr
00091 # endif
00092 # define _Mdouble_BEGIN_NAMESPACE __BEGIN_NAMESPACE_C99
00093 # define _Mdouble_END_NAMESPACE   __END_NAMESPACE_C99
00094 # include <bits/mathcalls.h>
00095 # undef       _Mdouble_
00096 # undef _Mdouble_BEGIN_NAMESPACE
00097 # undef _Mdouble_END_NAMESPACE
00098 # undef       __MATH_PRECNAME
00099 
00100 # if (__STDC__ - 0 || __GNUC__ - 0) \
00101      && (!defined __NO_LONG_DOUBLE_MATH || defined __LDBL_COMPAT)
00102 #  ifdef __LDBL_COMPAT
00103 
00104 #   ifdef __USE_ISOC99 
00105 extern float __nldbl_nexttowardf (float __x, long double __y)
00106                               __THROW __attribute__ ((__const__));
00107 #    ifdef __REDIRECT_NTH
00108 extern float __REDIRECT_NTH (nexttowardf, (float __x, long double __y),
00109                           __nldbl_nexttowardf)
00110      __attribute__ ((__const__));
00111 extern double __REDIRECT_NTH (nexttoward, (double __x, long double __y),
00112                            nextafter) __attribute__ ((__const__));
00113 extern long double __REDIRECT_NTH (nexttowardl,
00114                                (long double __x, long double __y),
00115                                nextafter) __attribute__ ((__const__));
00116 #    endif
00117 #   endif
00118 
00119 /* Include the file of declarations again, this time using `long double'
00120    instead of `double' and appending l to each function name.  */
00121 
00122 #   undef __MATHDECL_1
00123 #   define __MATHDECL_2(type, function,suffix, args, alias) \
00124   extern type __REDIRECT_NTH(__MATH_PRECNAME(function,suffix), \
00125                           args, alias)
00126 #   define __MATHDECL_1(type, function,suffix, args) \
00127   __MATHDECL_2(type, function,suffix, args, __CONCAT(function,suffix))
00128 #  endif
00129 
00130 #  ifndef _Mlong_double_
00131 #   define _Mlong_double_   long double
00132 #  endif
00133 #  define _Mdouble_         _Mlong_double_
00134 #  ifdef __STDC__
00135 #   define __MATH_PRECNAME(name,r) name##l##r
00136 #  else
00137 #   define __MATH_PRECNAME(name,r) namelr
00138 #  endif
00139 #  define _Mdouble_BEGIN_NAMESPACE __BEGIN_NAMESPACE_C99
00140 #  define _Mdouble_END_NAMESPACE   __END_NAMESPACE_C99
00141 #  include <bits/mathcalls.h>
00142 #  undef _Mdouble_
00143 # undef _Mdouble_BEGIN_NAMESPACE
00144 # undef _Mdouble_END_NAMESPACE
00145 #  undef __MATH_PRECNAME
00146 
00147 # endif /* __STDC__ || __GNUC__ */
00148 
00149 #endif /* Use misc or ISO C99.  */
00150 #undef __MATHDECL_1
00151 #undef __MATHDECL
00152 #undef __MATHCALL
00153 
00154 
00155 #if defined __USE_MISC || defined __USE_XOPEN
00156 /* This variable is used by `gamma' and `lgamma'.  */
00157 extern int signgam;
00158 #endif
00159 
00160 
00161 /* ISO C99 defines some generic macros which work on any data type.  */
00162 #ifdef __USE_ISOC99
00163 
00164 /* Get the architecture specific values describing the floating-point
00165    evaluation.  The following symbols will get defined:
00166 
00167     float_t   floating-point type at least as wide as `float' used
00168               to evaluate `float' expressions
00169     double_t  floating-point type at least as wide as `double' used
00170               to evaluate `double' expressions
00171 
00172     FLT_EVAL_METHOD
00173               Defined to
00174                 0    if `float_t' is `float' and `double_t' is `double'
00175                 1    if `float_t' and `double_t' are `double'
00176                 2    if `float_t' and `double_t' are `long double'
00177                 else `float_t' and `double_t' are unspecified
00178 
00179     INFINITY  representation of the infinity value of type `float'
00180 
00181     FP_FAST_FMA
00182     FP_FAST_FMAF
00183     FP_FAST_FMAL
00184               If defined it indicates that the `fma' function
00185               generally executes about as fast as a multiply and an add.
00186               This macro is defined only iff the `fma' function is
00187               implemented directly with a hardware multiply-add instructions.
00188 
00189     FP_ILOGB0 Expands to a value returned by `ilogb (0.0)'.
00190     FP_ILOGBNAN      Expands to a value returned by `ilogb (NAN)'.
00191 
00192     DECIMAL_DIG      Number of decimal digits supported by conversion between
00193               decimal and all internal floating-point formats.
00194 
00195 */
00196 
00197 /* All floating-point numbers can be put in one of these categories.  */
00198 enum
00199   {
00200     FP_NAN,
00201 # define FP_NAN FP_NAN
00202     FP_INFINITE,
00203 # define FP_INFINITE FP_INFINITE
00204     FP_ZERO,
00205 # define FP_ZERO FP_ZERO
00206     FP_SUBNORMAL,
00207 # define FP_SUBNORMAL FP_SUBNORMAL
00208     FP_NORMAL
00209 # define FP_NORMAL FP_NORMAL
00210   };
00211 
00212 /* Return number of classification appropriate for X.  */
00213 # ifdef __NO_LONG_DOUBLE_MATH
00214 #  define fpclassify(x) \
00215      (sizeof (x) == sizeof (float) ? __fpclassifyf (x) : __fpclassify (x))
00216 # else
00217 #  define fpclassify(x) \
00218      (sizeof (x) == sizeof (float)                                   \
00219       ? __fpclassifyf (x)                                            \
00220       : sizeof (x) == sizeof (double)                                       \
00221       ? __fpclassify (x) : __fpclassifyl (x))
00222 # endif
00223 
00224 /* Return nonzero value if sign of X is negative.  */
00225 # ifdef __NO_LONG_DOUBLE_MATH
00226 #  define signbit(x) \
00227      (sizeof (x) == sizeof (float) ? __signbitf (x) : __signbit (x))
00228 # else
00229 #  define signbit(x) \
00230      (sizeof (x) == sizeof (float)                                   \
00231       ? __signbitf (x)                                                      \
00232       : sizeof (x) == sizeof (double)                                       \
00233       ? __signbit (x) : __signbitl (x))
00234 # endif
00235 
00236 /* Return nonzero value if X is not +-Inf or NaN.  */
00237 # ifdef __NO_LONG_DOUBLE_MATH
00238 #  define isfinite(x) \
00239      (sizeof (x) == sizeof (float) ? __finitef (x) : __finite (x))
00240 # else
00241 #  define isfinite(x) \
00242      (sizeof (x) == sizeof (float)                                   \
00243       ? __finitef (x)                                                       \
00244       : sizeof (x) == sizeof (double)                                       \
00245       ? __finite (x) : __finitel (x))
00246 # endif
00247 
00248 /* Return nonzero value if X is neither zero, subnormal, Inf, nor NaN.  */
00249 # define isnormal(x) (fpclassify (x) == FP_NORMAL)
00250 
00251 /* Return nonzero value if X is a NaN.  We could use `fpclassify' but
00252    we already have this functions `__isnan' and it is faster.  */
00253 # ifdef __NO_LONG_DOUBLE_MATH
00254 #  define isnan(x) \
00255      (sizeof (x) == sizeof (float) ? __isnanf (x) : __isnan (x))
00256 # else
00257 #  define isnan(x) \
00258      (sizeof (x) == sizeof (float)                                   \
00259       ? __isnanf (x)                                                 \
00260       : sizeof (x) == sizeof (double)                                       \
00261       ? __isnan (x) : __isnanl (x))
00262 # endif
00263 
00264 /* Return nonzero value is X is positive or negative infinity.  */
00265 # ifdef __NO_LONG_DOUBLE_MATH
00266 #  define isinf(x) \
00267      (sizeof (x) == sizeof (float) ? __isinff (x) : __isinf (x))
00268 # else
00269 #  define isinf(x) \
00270      (sizeof (x) == sizeof (float)                                   \
00271       ? __isinff (x)                                                 \
00272       : sizeof (x) == sizeof (double)                                       \
00273       ? __isinf (x) : __isinfl (x))
00274 # endif
00275 
00276 /* Bitmasks for the math_errhandling macro.  */
00277 # define MATH_ERRNO  1      /* errno set by math functions.  */
00278 # define MATH_ERREXCEPT     2      /* Exceptions raised by math functions.  */
00279 
00280 #endif /* Use ISO C99.  */
00281 
00282 #ifdef __USE_MISC
00283 /* Support for various different standard error handling behaviors.  */
00284 typedef enum
00285 {
00286   _IEEE_ = -1,       /* According to IEEE 754/IEEE 854.  */
00287   _SVID_,     /* According to System V, release 4.  */
00288   _XOPEN_,    /* Nowadays also Unix98.  */
00289   _POSIX_,
00290   _ISOC_      /* Actually this is ISO C99.  */
00291 } _LIB_VERSION_TYPE;
00292 
00293 /* This variable can be changed at run-time to any of the values above to
00294    affect floating point error handling behavior (it may also be necessary
00295    to change the hardware FPU exception settings).  */
00296 extern _LIB_VERSION_TYPE _LIB_VERSION;
00297 #endif
00298 
00299 
00300 #ifdef __USE_SVID
00301 /* In SVID error handling, `matherr' is called with this description
00302    of the exceptional condition.
00303 
00304    We have a problem when using C++ since `exception' is a reserved
00305    name in C++.  */
00306 # ifdef __cplusplus
00307 struct __exception
00308 # else
00309 struct exception
00310 # endif
00311   {
00312     int type;
00313     char *name;
00314     double arg1;
00315     double arg2;
00316     double retval;
00317   };
00318 
00319 # ifdef __cplusplus
00320 extern int matherr (struct __exception *__exc) throw ();
00321 # else
00322 extern int matherr (struct exception *__exc);
00323 # endif
00324 
00325 # define X_TLOSS     1.41484755040568800000e+16
00326 
00327 /* Types of exceptions in the `type' field.  */
00328 # define DOMAIN             1
00329 # define SING        2
00330 # define OVERFLOW    3
00331 # define UNDERFLOW   4
00332 # define TLOSS              5
00333 # define PLOSS              6
00334 
00335 /* SVID mode specifies returning this large value instead of infinity.  */
00336 # define HUGE        3.40282347e+38F
00337 
00338 #else  /* !SVID */
00339 
00340 # ifdef __USE_XOPEN
00341 /* X/Open wants another strange constant.  */
00342 #  define MAXFLOAT   3.40282347e+38F
00343 # endif
00344 
00345 #endif /* SVID */
00346 
00347 
00348 /* Some useful constants.  */
00349 #if defined __USE_BSD || defined __USE_XOPEN
00350 # define M_E         2.7182818284590452354       /* e */
00351 # define M_LOG2E     1.4426950408889634074       /* log_2 e */
00352 # define M_LOG10E    0.43429448190325182765      /* log_10 e */
00353 # define M_LN2              0.69314718055994530942      /* log_e 2 */
00354 # define M_LN10             2.30258509299404568402      /* log_e 10 */
00355 # define M_PI        3.14159265358979323846      /* pi */
00356 # define M_PI_2             1.57079632679489661923      /* pi/2 */
00357 # define M_PI_4             0.78539816339744830962      /* pi/4 */
00358 # define M_1_PI             0.31830988618379067154      /* 1/pi */
00359 # define M_2_PI             0.63661977236758134308      /* 2/pi */
00360 # define M_2_SQRTPI  1.12837916709551257390      /* 2/sqrt(pi) */
00361 # define M_SQRT2     1.41421356237309504880      /* sqrt(2) */
00362 # define M_SQRT1_2   0.70710678118654752440      /* 1/sqrt(2) */
00363 #endif
00364 
00365 /* The above constants are not adequate for computation using `long double's.
00366    Therefore we provide as an extension constants with similar names as a
00367    GNU extension.  Provide enough digits for the 128-bit IEEE quad.  */
00368 #ifdef __USE_GNU
00369 # define M_El        2.7182818284590452353602874713526625L  /* e */
00370 # define M_LOG2El    1.4426950408889634073599246810018921L  /* log_2 e */
00371 # define M_LOG10El   0.4342944819032518276511289189166051L  /* log_10 e */
00372 # define M_LN2l             0.6931471805599453094172321214581766L  /* log_e 2 */
00373 # define M_LN10l     2.3025850929940456840179914546843642L  /* log_e 10 */
00374 # define M_PIl              3.1415926535897932384626433832795029L  /* pi */
00375 # define M_PI_2l     1.5707963267948966192313216916397514L  /* pi/2 */
00376 # define M_PI_4l     0.7853981633974483096156608458198757L  /* pi/4 */
00377 # define M_1_PIl     0.3183098861837906715377675267450287L  /* 1/pi */
00378 # define M_2_PIl     0.6366197723675813430755350534900574L  /* 2/pi */
00379 # define M_2_SQRTPIl 1.1283791670955125738961589031215452L  /* 2/sqrt(pi) */
00380 # define M_SQRT2l    1.4142135623730950488016887242096981L  /* sqrt(2) */
00381 # define M_SQRT1_2l  0.7071067811865475244008443621048490L  /* 1/sqrt(2) */
00382 #endif
00383 
00384 
00385 /* When compiling in strict ISO C compatible mode we must not use the
00386    inline functions since they, among other things, do not set the
00387    `errno' variable correctly.  */
00388 #if defined __STRICT_ANSI__ && !defined __NO_MATH_INLINES
00389 # define __NO_MATH_INLINES  1
00390 #endif
00391 
00392 #if defined __USE_ISOC99 && __GNUC_PREREQ(2,97)
00393 /* ISO C99 defines some macros to compare number while taking care for
00394    unordered numbers.  Many FPUs provide special instructions to support
00395    these operations.  Generic support in GCC for these as builtins went
00396    in before 3.0.0, but not all cpus added their patterns.  We define
00397    versions that use the builtins here, and <bits/mathinline.h> will
00398    undef/redefine as appropriate for the specific GCC version in use.  */
00399 # define isgreater(x, y)    __builtin_isgreater(x, y)
00400 # define isgreaterequal(x, y)      __builtin_isgreaterequal(x, y)
00401 # define isless(x, y)              __builtin_isless(x, y)
00402 # define islessequal(x, y)  __builtin_islessequal(x, y)
00403 # define islessgreater(x, y)       __builtin_islessgreater(x, y)
00404 # define isunordered(u, v)  __builtin_isunordered(u, v)
00405 #endif
00406 
00407 /* Get machine-dependent inline versions (if there are any).  */
00408 #ifdef __USE_EXTERN_INLINES
00409 # include <bits/mathinline.h>
00410 #endif
00411 
00412 #ifdef __USE_ISOC99
00413 /* If we've still got undefined comparison macros, provide defaults.  */
00414 
00415 /* Return nonzero value if X is greater than Y.  */
00416 # ifndef isgreater
00417 #  define isgreater(x, y) \
00418   (__extension__                                                     \
00419    ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y);                     \
00420       !isunordered (__x, __y) && __x > __y; }))
00421 # endif
00422 
00423 /* Return nonzero value if X is greater than or equal to Y.  */
00424 # ifndef isgreaterequal
00425 #  define isgreaterequal(x, y) \
00426   (__extension__                                                     \
00427    ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y);                     \
00428       !isunordered (__x, __y) && __x >= __y; }))
00429 # endif
00430 
00431 /* Return nonzero value if X is less than Y.  */
00432 # ifndef isless
00433 #  define isless(x, y) \
00434   (__extension__                                                     \
00435    ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y);                     \
00436       !isunordered (__x, __y) && __x < __y; }))
00437 # endif
00438 
00439 /* Return nonzero value if X is less than or equal to Y.  */
00440 # ifndef islessequal
00441 #  define islessequal(x, y) \
00442   (__extension__                                                     \
00443    ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y);                     \
00444       !isunordered (__x, __y) && __x <= __y; }))
00445 # endif
00446 
00447 /* Return nonzero value if either X is less than Y or Y is less than X.  */
00448 # ifndef islessgreater
00449 #  define islessgreater(x, y) \
00450   (__extension__                                                     \
00451    ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y);                     \
00452       !isunordered (__x, __y) && (__x < __y || __y < __x); }))
00453 # endif
00454 
00455 /* Return nonzero value if arguments are unordered.  */
00456 # ifndef isunordered
00457 #  define isunordered(u, v) \
00458   (__extension__                                                     \
00459    ({ __typeof__(u) __u = (u); __typeof__(v) __v = (v);                     \
00460       fpclassify (__u) == FP_NAN || fpclassify (__v) == FP_NAN; }))
00461 # endif
00462 
00463 #endif
00464 
00465 __END_DECLS
00466 
00467 
00468 #endif /* math.h  */