math.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /* Declarations for math functions.
  2. Copyright (C) 1991-2016 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /*
  16. * ISO C99 Standard: 7.12 Mathematics <math.h>
  17. */
  18. #ifndef _MATH_H
  19. #define _MATH_H 1
  20. #include <features.h>
  21. __BEGIN_DECLS
  22. /* Get machine-dependent vector math functions declarations. */
  23. #include <bits/math-vector.h>
  24. /* Get machine-dependent HUGE_VAL value (returned on overflow).
  25. On all IEEE754 machines, this is +Infinity. */
  26. #include <bits/huge_val.h>
  27. #ifdef __USE_ISOC99
  28. # include <bits/huge_valf.h>
  29. # include <bits/huge_vall.h>
  30. /* Get machine-dependent INFINITY value. */
  31. # include <bits/inf.h>
  32. /* Get machine-dependent NAN value (returned for some domain errors). */
  33. # include <bits/nan.h>
  34. #endif /* __USE_ISOC99 */
  35. /* Get general and ISO C99 specific information. */
  36. #include <bits/mathdef.h>
  37. /* The file <bits/mathcalls.h> contains the prototypes for all the
  38. actual math functions. These macros are used for those prototypes,
  39. so we can easily declare each function as both `name' and `__name',
  40. and can declare the float versions `namef' and `__namef'. */
  41. #define __SIMD_DECL(function) __CONCAT (__DECL_SIMD_, function)
  42. #define __MATHCALL_VEC(function, suffix, args) \
  43. __SIMD_DECL (__MATH_PRECNAME (function, suffix)) \
  44. __MATHCALL (function, suffix, args)
  45. #define __MATHDECL_VEC(type, function,suffix, args) \
  46. __SIMD_DECL (__MATH_PRECNAME (function, suffix)) \
  47. __MATHDECL(type, function,suffix, args)
  48. #define __MATHCALL(function,suffix, args) \
  49. __MATHDECL (_Mdouble_,function,suffix, args)
  50. #define __MATHDECL(type, function,suffix, args) \
  51. __MATHDECL_1(type, function,suffix, args); \
  52. __MATHDECL_1(type, __CONCAT(__,function),suffix, args)
  53. #define __MATHCALLX(function,suffix, args, attrib) \
  54. __MATHDECLX (_Mdouble_,function,suffix, args, attrib)
  55. #define __MATHDECLX(type, function,suffix, args, attrib) \
  56. __MATHDECL_1(type, function,suffix, args) __attribute__ (attrib); \
  57. __MATHDECL_1(type, __CONCAT(__,function),suffix, args) __attribute__ (attrib)
  58. #define __MATHDECL_1(type, function,suffix, args) \
  59. extern type __MATH_PRECNAME(function,suffix) args __THROW
  60. #define _Mdouble_ double
  61. #define __MATH_PRECNAME(name,r) __CONCAT(name,r)
  62. #define __MATH_DECLARING_DOUBLE 1
  63. #define _Mdouble_BEGIN_NAMESPACE __BEGIN_NAMESPACE_STD
  64. #define _Mdouble_END_NAMESPACE __END_NAMESPACE_STD
  65. #include <bits/mathcalls.h>
  66. #undef _Mdouble_
  67. #undef _Mdouble_BEGIN_NAMESPACE
  68. #undef _Mdouble_END_NAMESPACE
  69. #undef __MATH_PRECNAME
  70. #undef __MATH_DECLARING_DOUBLE
  71. #ifdef __USE_ISOC99
  72. /* Include the file of declarations again, this time using `float'
  73. instead of `double' and appending f to each function name. */
  74. # ifndef _Mfloat_
  75. # define _Mfloat_ float
  76. # endif
  77. # define _Mdouble_ _Mfloat_
  78. # define __MATH_PRECNAME(name,r) name##f##r
  79. # define __MATH_DECLARING_DOUBLE 0
  80. # define _Mdouble_BEGIN_NAMESPACE __BEGIN_NAMESPACE_C99
  81. # define _Mdouble_END_NAMESPACE __END_NAMESPACE_C99
  82. # include <bits/mathcalls.h>
  83. # undef _Mdouble_
  84. # undef _Mdouble_BEGIN_NAMESPACE
  85. # undef _Mdouble_END_NAMESPACE
  86. # undef __MATH_PRECNAME
  87. # undef __MATH_DECLARING_DOUBLE
  88. # if !(defined __NO_LONG_DOUBLE_MATH && defined _LIBC) \
  89. || defined __LDBL_COMPAT \
  90. || defined _LIBC_TEST
  91. # ifdef __LDBL_COMPAT
  92. # ifdef __USE_ISOC99
  93. extern float __nldbl_nexttowardf (float __x, long double __y)
  94. __THROW __attribute__ ((__const__));
  95. # ifdef __REDIRECT_NTH
  96. extern float __REDIRECT_NTH (nexttowardf, (float __x, long double __y),
  97. __nldbl_nexttowardf)
  98. __attribute__ ((__const__));
  99. extern double __REDIRECT_NTH (nexttoward, (double __x, long double __y),
  100. nextafter) __attribute__ ((__const__));
  101. extern long double __REDIRECT_NTH (nexttowardl,
  102. (long double __x, long double __y),
  103. nextafter) __attribute__ ((__const__));
  104. # endif
  105. # endif
  106. # undef __MATHDECL_1
  107. # define __MATHDECL_2(type, function,suffix, args, alias) \
  108. extern type __REDIRECT_NTH(__MATH_PRECNAME(function,suffix), \
  109. args, alias)
  110. # define __MATHDECL_1(type, function,suffix, args) \
  111. __MATHDECL_2(type, function,suffix, args, __CONCAT(function,suffix))
  112. # endif
  113. /* Include the file of declarations again, this time using `long double'
  114. instead of `double' and appending l to each function name. */
  115. # ifndef _Mlong_double_
  116. # define _Mlong_double_ long double
  117. # endif
  118. # define _Mdouble_ _Mlong_double_
  119. # define __MATH_PRECNAME(name,r) name##l##r
  120. # define __MATH_DECLARING_DOUBLE 0
  121. # define _Mdouble_BEGIN_NAMESPACE __BEGIN_NAMESPACE_C99
  122. # define _Mdouble_END_NAMESPACE __END_NAMESPACE_C99
  123. # define __MATH_DECLARE_LDOUBLE 1
  124. # include <bits/mathcalls.h>
  125. # undef _Mdouble_
  126. # undef _Mdouble_BEGIN_NAMESPACE
  127. # undef _Mdouble_END_NAMESPACE
  128. # undef __MATH_PRECNAME
  129. # undef __MATH_DECLARING_DOUBLE
  130. # endif /* !(__NO_LONG_DOUBLE_MATH && _LIBC) || __LDBL_COMPAT */
  131. #endif /* Use ISO C99. */
  132. #undef __MATHDECL_1
  133. #undef __MATHDECL
  134. #undef __MATHCALL
  135. #if defined __USE_MISC || defined __USE_XOPEN
  136. /* This variable is used by `gamma' and `lgamma'. */
  137. extern int signgam;
  138. #endif
  139. /* ISO C99 defines some generic macros which work on any data type. */
  140. #ifdef __USE_ISOC99
  141. /* Get the architecture specific values describing the floating-point
  142. evaluation. The following symbols will get defined:
  143. float_t floating-point type at least as wide as `float' used
  144. to evaluate `float' expressions
  145. double_t floating-point type at least as wide as `double' used
  146. to evaluate `double' expressions
  147. FLT_EVAL_METHOD
  148. Defined to
  149. 0 if `float_t' is `float' and `double_t' is `double'
  150. 1 if `float_t' and `double_t' are `double'
  151. 2 if `float_t' and `double_t' are `long double'
  152. else `float_t' and `double_t' are unspecified
  153. INFINITY representation of the infinity value of type `float'
  154. FP_FAST_FMA
  155. FP_FAST_FMAF
  156. FP_FAST_FMAL
  157. If defined it indicates that the `fma' function
  158. generally executes about as fast as a multiply and an add.
  159. This macro is defined only iff the `fma' function is
  160. implemented directly with a hardware multiply-add instructions.
  161. FP_ILOGB0 Expands to a value returned by `ilogb (0.0)'.
  162. FP_ILOGBNAN Expands to a value returned by `ilogb (NAN)'.
  163. DECIMAL_DIG Number of decimal digits supported by conversion between
  164. decimal and all internal floating-point formats.
  165. */
  166. /* All floating-point numbers can be put in one of these categories. */
  167. enum
  168. {
  169. FP_NAN =
  170. # define FP_NAN 0
  171. FP_NAN,
  172. FP_INFINITE =
  173. # define FP_INFINITE 1
  174. FP_INFINITE,
  175. FP_ZERO =
  176. # define FP_ZERO 2
  177. FP_ZERO,
  178. FP_SUBNORMAL =
  179. # define FP_SUBNORMAL 3
  180. FP_SUBNORMAL,
  181. FP_NORMAL =
  182. # define FP_NORMAL 4
  183. FP_NORMAL
  184. };
  185. /* GCC bug 66462 means we cannot use the math builtins with -fsignaling-nan,
  186. so disable builtins if this is enabled. When fixed in a newer GCC,
  187. the __SUPPORT_SNAN__ check may be skipped for those versions. */
  188. /* Return number of classification appropriate for X. */
  189. # if __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__ \
  190. && !defined __OPTIMIZE_SIZE__
  191. # define fpclassify(x) __builtin_fpclassify (FP_NAN, FP_INFINITE, \
  192. FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
  193. # elif defined __NO_LONG_DOUBLE_MATH
  194. # define fpclassify(x) \
  195. (sizeof (x) == sizeof (float) ? __fpclassifyf (x) : __fpclassify (x))
  196. # else
  197. # define fpclassify(x) \
  198. (sizeof (x) == sizeof (float) \
  199. ? __fpclassifyf (x) \
  200. : sizeof (x) == sizeof (double) \
  201. ? __fpclassify (x) : __fpclassifyl (x))
  202. # endif
  203. /* Return nonzero value if sign of X is negative. */
  204. # if __GNUC_PREREQ (4,0)
  205. # define signbit(x) \
  206. (sizeof (x) == sizeof (float) \
  207. ? __builtin_signbitf (x) \
  208. : sizeof (x) == sizeof (double) \
  209. ? __builtin_signbit (x) : __builtin_signbitl (x))
  210. # else
  211. # ifdef __NO_LONG_DOUBLE_MATH
  212. # define signbit(x) \
  213. (sizeof (x) == sizeof (float) ? __signbitf (x) : __signbit (x))
  214. # else
  215. # define signbit(x) \
  216. (sizeof (x) == sizeof (float) \
  217. ? __signbitf (x) \
  218. : sizeof (x) == sizeof (double) \
  219. ? __signbit (x) : __signbitl (x))
  220. # endif
  221. # endif
  222. /* Return nonzero value if X is not +-Inf or NaN. */
  223. # if __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__
  224. # define isfinite(x) __builtin_isfinite (x)
  225. # elif defined __NO_LONG_DOUBLE_MATH
  226. # define isfinite(x) \
  227. (sizeof (x) == sizeof (float) ? __finitef (x) : __finite (x))
  228. # else
  229. # define isfinite(x) \
  230. (sizeof (x) == sizeof (float) \
  231. ? __finitef (x) \
  232. : sizeof (x) == sizeof (double) \
  233. ? __finite (x) : __finitel (x))
  234. # endif
  235. /* Return nonzero value if X is neither zero, subnormal, Inf, nor NaN. */
  236. # if __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__
  237. # define isnormal(x) __builtin_isnormal (x)
  238. # else
  239. # define isnormal(x) (fpclassify (x) == FP_NORMAL)
  240. # endif
  241. /* Return nonzero value if X is a NaN. We could use `fpclassify' but
  242. we already have this functions `__isnan' and it is faster. */
  243. # if __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__
  244. # define isnan(x) __builtin_isnan (x)
  245. # elif defined __NO_LONG_DOUBLE_MATH
  246. # define isnan(x) \
  247. (sizeof (x) == sizeof (float) ? __isnanf (x) : __isnan (x))
  248. # else
  249. # define isnan(x) \
  250. (sizeof (x) == sizeof (float) \
  251. ? __isnanf (x) \
  252. : sizeof (x) == sizeof (double) \
  253. ? __isnan (x) : __isnanl (x))
  254. # endif
  255. /* Return nonzero value if X is positive or negative infinity. */
  256. # if __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__
  257. # define isinf(x) __builtin_isinf_sign (x)
  258. # elif defined __NO_LONG_DOUBLE_MATH
  259. # define isinf(x) \
  260. (sizeof (x) == sizeof (float) ? __isinff (x) : __isinf (x))
  261. # else
  262. # define isinf(x) \
  263. (sizeof (x) == sizeof (float) \
  264. ? __isinff (x) \
  265. : sizeof (x) == sizeof (double) \
  266. ? __isinf (x) : __isinfl (x))
  267. # endif
  268. /* Bitmasks for the math_errhandling macro. */
  269. # define MATH_ERRNO 1 /* errno set by math functions. */
  270. # define MATH_ERREXCEPT 2 /* Exceptions raised by math functions. */
  271. /* By default all functions support both errno and exception handling.
  272. In gcc's fast math mode and if inline functions are defined this
  273. might not be true. */
  274. # ifndef __FAST_MATH__
  275. # define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
  276. # endif
  277. #endif /* Use ISO C99. */
  278. #ifdef __USE_GNU
  279. /* Return nonzero value if X is a signaling NaN. */
  280. # ifdef __NO_LONG_DOUBLE_MATH
  281. # define issignaling(x) \
  282. (sizeof (x) == sizeof (float) ? __issignalingf (x) : __issignaling (x))
  283. # else
  284. # define issignaling(x) \
  285. (sizeof (x) == sizeof (float) \
  286. ? __issignalingf (x) \
  287. : sizeof (x) == sizeof (double) \
  288. ? __issignaling (x) : __issignalingl (x))
  289. # endif
  290. #endif /* Use GNU. */
  291. #ifdef __USE_MISC
  292. /* Support for various different standard error handling behaviors. */
  293. typedef enum
  294. {
  295. _IEEE_ = -1, /* According to IEEE 754/IEEE 854. */
  296. _SVID_, /* According to System V, release 4. */
  297. _XOPEN_, /* Nowadays also Unix98. */
  298. _POSIX_,
  299. _ISOC_ /* Actually this is ISO C99. */
  300. } _LIB_VERSION_TYPE;
  301. /* This variable can be changed at run-time to any of the values above to
  302. affect floating point error handling behavior (it may also be necessary
  303. to change the hardware FPU exception settings). */
  304. extern _LIB_VERSION_TYPE _LIB_VERSION;
  305. #endif
  306. #ifdef __USE_MISC
  307. /* In SVID error handling, `matherr' is called with this description
  308. of the exceptional condition.
  309. We have a problem when using C++ since `exception' is a reserved
  310. name in C++. */
  311. # ifdef __cplusplus
  312. struct __exception
  313. # else
  314. struct exception
  315. # endif
  316. {
  317. int type;
  318. char *name;
  319. double arg1;
  320. double arg2;
  321. double retval;
  322. };
  323. # ifdef __cplusplus
  324. extern int matherr (struct __exception *__exc) throw ();
  325. # else
  326. extern int matherr (struct exception *__exc);
  327. # endif
  328. # define X_TLOSS 1.41484755040568800000e+16
  329. /* Types of exceptions in the `type' field. */
  330. # define DOMAIN 1
  331. # define SING 2
  332. # define OVERFLOW 3
  333. # define UNDERFLOW 4
  334. # define TLOSS 5
  335. # define PLOSS 6
  336. /* SVID mode specifies returning this large value instead of infinity. */
  337. # define HUGE 3.40282347e+38F
  338. #else /* !Misc. */
  339. # ifdef __USE_XOPEN
  340. /* X/Open wants another strange constant. */
  341. # define MAXFLOAT 3.40282347e+38F
  342. # endif
  343. #endif /* Misc. */
  344. /* Some useful constants. */
  345. #if defined __USE_MISC || defined __USE_XOPEN
  346. # define M_E 2.7182818284590452354 /* e */
  347. # define M_LOG2E 1.4426950408889634074 /* log_2 e */
  348. # define M_LOG10E 0.43429448190325182765 /* log_10 e */
  349. # define M_LN2 0.69314718055994530942 /* log_e 2 */
  350. # define M_LN10 2.30258509299404568402 /* log_e 10 */
  351. # define M_PI 3.14159265358979323846 /* pi */
  352. # define M_PI_2 1.57079632679489661923 /* pi/2 */
  353. # define M_PI_4 0.78539816339744830962 /* pi/4 */
  354. # define M_1_PI 0.31830988618379067154 /* 1/pi */
  355. # define M_2_PI 0.63661977236758134308 /* 2/pi */
  356. # define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
  357. # define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
  358. # define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
  359. #endif
  360. /* The above constants are not adequate for computation using `long double's.
  361. Therefore we provide as an extension constants with similar names as a
  362. GNU extension. Provide enough digits for the 128-bit IEEE quad. */
  363. #ifdef __USE_GNU
  364. # define M_El 2.718281828459045235360287471352662498L /* e */
  365. # define M_LOG2El 1.442695040888963407359924681001892137L /* log_2 e */
  366. # define M_LOG10El 0.434294481903251827651128918916605082L /* log_10 e */
  367. # define M_LN2l 0.693147180559945309417232121458176568L /* log_e 2 */
  368. # define M_LN10l 2.302585092994045684017991454684364208L /* log_e 10 */
  369. # define M_PIl 3.141592653589793238462643383279502884L /* pi */
  370. # define M_PI_2l 1.570796326794896619231321691639751442L /* pi/2 */
  371. # define M_PI_4l 0.785398163397448309615660845819875721L /* pi/4 */
  372. # define M_1_PIl 0.318309886183790671537767526745028724L /* 1/pi */
  373. # define M_2_PIl 0.636619772367581343075535053490057448L /* 2/pi */
  374. # define M_2_SQRTPIl 1.128379167095512573896158903121545172L /* 2/sqrt(pi) */
  375. # define M_SQRT2l 1.414213562373095048801688724209698079L /* sqrt(2) */
  376. # define M_SQRT1_2l 0.707106781186547524400844362104849039L /* 1/sqrt(2) */
  377. #endif
  378. /* When compiling in strict ISO C compatible mode we must not use the
  379. inline functions since they, among other things, do not set the
  380. `errno' variable correctly. */
  381. #if defined __STRICT_ANSI__ && !defined __NO_MATH_INLINES
  382. # define __NO_MATH_INLINES 1
  383. #endif
  384. #if defined __USE_ISOC99 && __GNUC_PREREQ(2,97)
  385. /* ISO C99 defines some macros to compare number while taking care for
  386. unordered numbers. Many FPUs provide special instructions to support
  387. these operations. Generic support in GCC for these as builtins went
  388. in before 3.0.0, but not all cpus added their patterns. We define
  389. versions that use the builtins here, and <bits/mathinline.h> will
  390. undef/redefine as appropriate for the specific GCC version in use. */
  391. # define isgreater(x, y) __builtin_isgreater(x, y)
  392. # define isgreaterequal(x, y) __builtin_isgreaterequal(x, y)
  393. # define isless(x, y) __builtin_isless(x, y)
  394. # define islessequal(x, y) __builtin_islessequal(x, y)
  395. # define islessgreater(x, y) __builtin_islessgreater(x, y)
  396. # define isunordered(u, v) __builtin_isunordered(u, v)
  397. #endif
  398. /* Get machine-dependent inline versions (if there are any). */
  399. #ifdef __USE_EXTERN_INLINES
  400. # include <bits/mathinline.h>
  401. #endif
  402. /* Define special entry points to use when the compiler got told to
  403. only expect finite results. */
  404. #if defined __FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0
  405. # include <bits/math-finite.h>
  406. #endif
  407. #ifdef __USE_ISOC99
  408. /* If we've still got undefined comparison macros, provide defaults. */
  409. /* Return nonzero value if X is greater than Y. */
  410. # ifndef isgreater
  411. # define isgreater(x, y) \
  412. (__extension__ \
  413. ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
  414. !isunordered (__x, __y) && __x > __y; }))
  415. # endif
  416. /* Return nonzero value if X is greater than or equal to Y. */
  417. # ifndef isgreaterequal
  418. # define isgreaterequal(x, y) \
  419. (__extension__ \
  420. ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
  421. !isunordered (__x, __y) && __x >= __y; }))
  422. # endif
  423. /* Return nonzero value if X is less than Y. */
  424. # ifndef isless
  425. # define isless(x, y) \
  426. (__extension__ \
  427. ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
  428. !isunordered (__x, __y) && __x < __y; }))
  429. # endif
  430. /* Return nonzero value if X is less than or equal to Y. */
  431. # ifndef islessequal
  432. # define islessequal(x, y) \
  433. (__extension__ \
  434. ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
  435. !isunordered (__x, __y) && __x <= __y; }))
  436. # endif
  437. /* Return nonzero value if either X is less than Y or Y is less than X. */
  438. # ifndef islessgreater
  439. # define islessgreater(x, y) \
  440. (__extension__ \
  441. ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
  442. !isunordered (__x, __y) && (__x < __y || __y < __x); }))
  443. # endif
  444. /* Return nonzero value if arguments are unordered. */
  445. # ifndef isunordered
  446. # define isunordered(u, v) \
  447. (__extension__ \
  448. ({ __typeof__(u) __u = (u); __typeof__(v) __v = (v); \
  449. fpclassify (__u) == FP_NAN || fpclassify (__v) == FP_NAN; }))
  450. # endif
  451. #endif
  452. __END_DECLS
  453. #endif /* math.h */