Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 1 | #ifndef _MATH_H |
| 2 | #define _MATH_H |
| 3 | |
Rich Felker | 3ed8c9f | 2011-11-10 20:40:06 -0500 | [diff] [blame] | 4 | #ifdef __cplusplus |
| 5 | extern "C" { |
| 6 | #endif |
| 7 | |
Rich Felker | 414a4cd | 2012-02-15 21:47:55 -0500 | [diff] [blame] | 8 | #define __NEED_float_t |
| 9 | #define __NEED_double_t |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 10 | #define __NEED___uint16_t |
| 11 | #define __NEED___uint32_t |
| 12 | #define __NEED___uint64_t |
| 13 | #include <bits/alltypes.h> |
| 14 | |
Rich Felker | 405ce58 | 2012-03-02 22:35:37 -0500 | [diff] [blame] | 15 | #if 100*__GNUC__+__GNUC_MINOR__ >= 303 |
| 16 | #define NAN __builtin_nanf("") |
| 17 | #define INFINITY __builtin_inff() |
| 18 | #else |
| 19 | #define NAN (0.0f/0.0f) |
| 20 | #define INFINITY 1e40f |
| 21 | #endif |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 22 | |
| 23 | #define HUGE_VALF INFINITY |
| 24 | #define HUGE_VAL ((double)INFINITY) |
| 25 | #define HUGE_VALL ((long double)INFINITY) |
| 26 | |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 27 | #define MATH_ERRNO 1 |
Rich Felker | ed0e3a3 | 2012-04-18 11:41:04 -0400 | [diff] [blame] | 28 | #define MATH_ERREXCEPT 2 |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 29 | #define math_errhandling 2 |
| 30 | |
Rich Felker | 98c9af5 | 2012-05-05 22:22:46 -0400 | [diff] [blame] | 31 | #define FP_ILOGBNAN (-1-(int)(((unsigned)-1)>>1)) |
| 32 | #define FP_ILOGB0 FP_ILOGBNAN |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 33 | |
| 34 | #define FP_NAN 0 |
| 35 | #define FP_INFINITE 1 |
| 36 | #define FP_ZERO 2 |
| 37 | #define FP_SUBNORMAL 3 |
| 38 | #define FP_NORMAL 4 |
| 39 | |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 40 | int __fpclassify(double); |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 41 | int __fpclassifyf(float); |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 42 | int __fpclassifyl(long double); |
| 43 | |
Rich Felker | 93a18a1 | 2012-03-30 23:33:00 -0400 | [diff] [blame] | 44 | union __float_repr { float __f; __uint32_t __i; }; |
| 45 | union __double_repr { double __f; __uint64_t __i; }; |
| 46 | |
| 47 | #define __FLOAT_BITS(f) (((union __float_repr){ (float)(f) }).__i) |
| 48 | #define __DOUBLE_BITS(f) (((union __double_repr){ (double)(f) }).__i) |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 49 | |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 50 | #define fpclassify(x) ( \ |
| 51 | sizeof(x) == sizeof(float) ? __fpclassifyf(x) : \ |
| 52 | sizeof(x) == sizeof(double) ? __fpclassify(x) : \ |
| 53 | __fpclassifyl(x) ) |
| 54 | |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 55 | #define isinf(x) ( \ |
| 56 | sizeof(x) == sizeof(float) ? (__FLOAT_BITS(x) & 0x7fffffff) == 0x7f800000 : \ |
| 57 | sizeof(x) == sizeof(double) ? (__DOUBLE_BITS(x) & (__uint64_t)-1>>1) == (__uint64_t)0x7ff<<52 : \ |
| 58 | __fpclassifyl(x) == FP_INFINITE) |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 59 | |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 60 | #define isnan(x) ( \ |
| 61 | sizeof(x) == sizeof(float) ? (__FLOAT_BITS(x) & 0x7fffffff) > 0x7f800000 : \ |
| 62 | sizeof(x) == sizeof(double) ? (__DOUBLE_BITS(x) & (__uint64_t)-1>>1) > (__uint64_t)0x7ff<<52 : \ |
| 63 | __fpclassifyl(x) == FP_NAN) |
Rich Felker | e0037ef | 2011-06-08 16:33:04 -0400 | [diff] [blame] | 64 | |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 65 | #define isnormal(x) ( \ |
| 66 | sizeof(x) == sizeof(float) ? ((__FLOAT_BITS(x)+0x00800000) & 0x7fffffff) >= 0x01000000 : \ |
| 67 | sizeof(x) == sizeof(double) ? ((__DOUBLE_BITS(x)+((__uint64_t)1<<52)) & (__uint64_t)-1>>1) >= (__uint64_t)1<<53 : \ |
| 68 | __fpclassifyl(x) == FP_NORMAL) |
| 69 | |
| 70 | #define isfinite(x) ( \ |
| 71 | sizeof(x) == sizeof(float) ? (__FLOAT_BITS(x) & 0x7fffffff) < 0x7f800000 : \ |
| 72 | sizeof(x) == sizeof(double) ? (__DOUBLE_BITS(x) & (__uint64_t)-1>>1) < (__uint64_t)0x7ff<<52 : \ |
| 73 | __fpclassifyl(x) > FP_INFINITE) |
| 74 | |
| 75 | int __signbit(double); |
| 76 | int __signbitf(float); |
| 77 | int __signbitl(long double); |
| 78 | |
| 79 | #define signbit(x) ( \ |
Rich Felker | 4dbd941 | 2012-03-30 23:41:43 -0400 | [diff] [blame] | 80 | sizeof(x) == sizeof(float) ? (int)(__FLOAT_BITS(x)>>31) : \ |
| 81 | sizeof(x) == sizeof(double) ? (int)(__DOUBLE_BITS(x)>>63) : \ |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 82 | __signbitl(x) ) |
| 83 | |
| 84 | #define isunordered(x,y) (isnan((x)) ? ((void)(y),1) : isnan((y))) |
| 85 | |
Rich Felker | 9fcecd7 | 2012-03-02 11:38:39 -0500 | [diff] [blame] | 86 | #if __STDC_VERSION__ >= 199901L |
Rich Felker | db3e78c | 2012-03-02 00:36:26 -0500 | [diff] [blame] | 87 | inline |
| 88 | #endif |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 89 | static int __isrel(long double __x, long double __y, int __rel) |
Rich Felker | e0037ef | 2011-06-08 16:33:04 -0400 | [diff] [blame] | 90 | { |
| 91 | if (isunordered(__x, __y)) return 0; |
| 92 | if (__rel==-2) return __x < __y; |
| 93 | if (__rel==2) return __x > __y; |
| 94 | if (__rel==-1) return __x <= __y; |
| 95 | if (__rel==1) return __x >= __y; |
| 96 | return __x != __y; |
| 97 | } |
| 98 | |
| 99 | #define isless(x,y) __isrel((x), (y), -2) |
| 100 | #define islessequal(x,y) __isrel((x), (y), -1) |
| 101 | #define islessgreater(x,y) __isrel((x), (y), 0) |
| 102 | #define isgreaterequal(x,y) __isrel((x), (y), 1) |
| 103 | #define isgreater(x,y) __isrel((x), (y), 2) |
| 104 | |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 105 | double acos(double); |
| 106 | float acosf(float); |
| 107 | long double acosl(long double); |
| 108 | |
| 109 | double acosh(double); |
| 110 | float acoshf(float); |
| 111 | long double acoshl(long double); |
| 112 | |
| 113 | double asin(double); |
| 114 | float asinf(float); |
| 115 | long double asinl(long double); |
| 116 | |
| 117 | double asinh(double); |
| 118 | float asinhf(float); |
| 119 | long double asinhl(long double); |
| 120 | |
| 121 | double atan(double); |
| 122 | float atanf(float); |
| 123 | long double atanl(long double); |
| 124 | |
| 125 | double atan2(double, double); |
| 126 | float atan2f(float, float); |
| 127 | long double atan2l(long double, long double); |
| 128 | |
| 129 | double atanh(double); |
| 130 | float atanhf(float); |
| 131 | long double atanhl(long double); |
| 132 | |
| 133 | double cbrt(double); |
| 134 | float cbrtf(float); |
| 135 | long double cbrtl(long double); |
| 136 | |
| 137 | double ceil(double); |
| 138 | float ceilf(float); |
| 139 | long double ceill(long double); |
| 140 | |
| 141 | double copysign(double, double); |
| 142 | float copysignf(float, float); |
| 143 | long double copysignl(long double, long double); |
| 144 | |
| 145 | double cos(double); |
| 146 | float cosf(float); |
| 147 | long double cosl(long double); |
| 148 | |
| 149 | double cosh(double); |
| 150 | float coshf(float); |
| 151 | long double coshl(long double); |
| 152 | |
| 153 | double erf(double); |
| 154 | float erff(float); |
| 155 | long double erfl(long double); |
| 156 | |
| 157 | double erfc(double); |
| 158 | float erfcf(float); |
| 159 | long double erfcl(long double); |
| 160 | |
| 161 | double exp(double); |
| 162 | float expf(float); |
| 163 | long double expl(long double); |
| 164 | |
| 165 | double exp2(double); |
| 166 | float exp2f(float); |
| 167 | long double exp2l(long double); |
| 168 | |
| 169 | double expm1(double); |
| 170 | float expm1f(float); |
| 171 | long double expm1l(long double); |
| 172 | |
| 173 | double fabs(double); |
| 174 | float fabsf(float); |
| 175 | long double fabsl(long double); |
| 176 | |
| 177 | double fdim(double, double); |
| 178 | float fdimf(float, float); |
| 179 | long double fdiml(long double, long double); |
| 180 | |
| 181 | double floor(double); |
| 182 | float floorf(float); |
| 183 | long double floorl(long double); |
| 184 | |
| 185 | double fma(double, double, double); |
| 186 | float fmaf(float, float, float); |
| 187 | long double fmal(long double, long double, long double); |
| 188 | |
| 189 | double fmax(double, double); |
| 190 | float fmaxf(float, float); |
| 191 | long double fmaxl(long double, long double); |
| 192 | |
| 193 | double fmin(double, double); |
| 194 | float fminf(float, float); |
| 195 | long double fminl(long double, long double); |
| 196 | |
| 197 | double fmod(double, double); |
| 198 | float fmodf(float, float); |
| 199 | long double fmodl(long double, long double); |
| 200 | |
| 201 | double frexp(double, int *); |
| 202 | float frexpf(float value, int *); |
| 203 | long double frexpl(long double value, int *); |
| 204 | |
| 205 | double hypot(double, double); |
| 206 | float hypotf(float, float); |
| 207 | long double hypotl(long double, long double); |
| 208 | |
| 209 | int ilogb(double); |
| 210 | int ilogbf(float); |
| 211 | int ilogbl(long double); |
| 212 | |
| 213 | double ldexp(double, int); |
| 214 | float ldexpf(float, int); |
| 215 | long double ldexpl(long double, int); |
| 216 | |
| 217 | double lgamma(double); |
| 218 | float lgammaf(float); |
| 219 | long double lgammal(long double); |
| 220 | |
| 221 | long long llrint(double); |
| 222 | long long llrintf(float); |
| 223 | long long llrintl(long double); |
| 224 | |
| 225 | long long llround(double); |
| 226 | long long llroundf(float); |
| 227 | long long llroundl(long double); |
| 228 | |
| 229 | double log(double); |
| 230 | float logf(float); |
| 231 | long double logl(long double); |
| 232 | |
| 233 | double log10(double); |
| 234 | float log10f(float); |
| 235 | long double log10l(long double); |
| 236 | |
| 237 | double log1p(double); |
| 238 | float log1pf(float); |
| 239 | long double log1pl(long double); |
| 240 | |
| 241 | double log2(double); |
| 242 | float log2f(float); |
| 243 | long double log2l(long double); |
| 244 | |
| 245 | double logb(double); |
| 246 | float logbf(float); |
| 247 | long double logbl(long double); |
| 248 | |
| 249 | long lrint(double); |
| 250 | long lrintf(float); |
| 251 | long lrintl(long double); |
| 252 | |
| 253 | long lround(double); |
| 254 | long lroundf(float); |
| 255 | long lroundl(long double); |
| 256 | |
| 257 | double modf(double, double *); |
| 258 | float modff(float, float *); |
| 259 | long double modfl(long double, long double *); |
| 260 | |
| 261 | double nan(const char *); |
| 262 | float nanf(const char *); |
| 263 | long double nanl(const char *); |
| 264 | |
| 265 | double nearbyint(double); |
| 266 | float nearbyintf(float); |
| 267 | long double nearbyintl(long double); |
| 268 | |
| 269 | double nextafter(double, double); |
| 270 | float nextafterf(float, float); |
| 271 | long double nextafterl(long double, long double); |
| 272 | |
| 273 | double nexttoward(double, long double); |
| 274 | float nexttowardf(float, long double); |
| 275 | long double nexttowardl(long double, long double); |
| 276 | |
| 277 | double pow(double, double); |
| 278 | float powf(float, float); |
| 279 | long double powl(long double, long double); |
| 280 | |
| 281 | double remainder(double, double); |
| 282 | float remainderf(float, float); |
| 283 | long double remainderl(long double, long double); |
| 284 | |
| 285 | double remquo(double, double, int *); |
| 286 | float remquof(float, float, int *); |
| 287 | long double remquol(long double, long double, int *); |
| 288 | |
| 289 | double rint(double); |
| 290 | float rintf(float); |
| 291 | long double rintl(long double); |
| 292 | |
| 293 | double round(double); |
| 294 | float roundf(float); |
| 295 | long double roundl(long double); |
| 296 | |
| 297 | double scalbln(double, long); |
| 298 | float scalblnf(float, long); |
| 299 | long double scalblnl(long double, long); |
| 300 | |
| 301 | double scalbn(double, int); |
| 302 | float scalbnf(float, int); |
| 303 | long double scalbnl(long double, int); |
| 304 | |
| 305 | double sin(double); |
| 306 | float sinf(float); |
| 307 | long double sinl(long double); |
| 308 | |
| 309 | double sinh(double); |
| 310 | float sinhf(float); |
| 311 | long double sinhl(long double); |
| 312 | |
| 313 | double sqrt(double); |
| 314 | float sqrtf(float); |
| 315 | long double sqrtl(long double); |
| 316 | |
| 317 | double tan(double); |
| 318 | float tanf(float); |
| 319 | long double tanl(long double); |
| 320 | |
| 321 | double tanh(double); |
| 322 | float tanhf(float); |
| 323 | long double tanhl(long double); |
| 324 | |
| 325 | double tgamma(double); |
| 326 | float tgammaf(float); |
| 327 | long double tgammal(long double); |
| 328 | |
| 329 | double trunc(double); |
| 330 | float truncf(float); |
| 331 | long double truncl(long double); |
| 332 | |
Rich Felker | 419ae6d | 2012-05-22 21:52:08 -0400 | [diff] [blame] | 333 | #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) |
Rich Felker | 74eea62 | 2011-02-14 18:41:25 -0500 | [diff] [blame] | 334 | #define M_E 2.7182818284590452354 /* e */ |
| 335 | #define M_LOG2E 1.4426950408889634074 /* log_2 e */ |
| 336 | #define M_LOG10E 0.43429448190325182765 /* log_10 e */ |
| 337 | #define M_LN2 0.69314718055994530942 /* log_e 2 */ |
| 338 | #define M_LN10 2.30258509299404568402 /* log_e 10 */ |
| 339 | #define M_PI 3.14159265358979323846 /* pi */ |
| 340 | #define M_PI_2 1.57079632679489661923 /* pi/2 */ |
| 341 | #define M_PI_4 0.78539816339744830962 /* pi/4 */ |
| 342 | #define M_1_PI 0.31830988618379067154 /* 1/pi */ |
| 343 | #define M_2_PI 0.63661977236758134308 /* 2/pi */ |
| 344 | #define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */ |
| 345 | #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */ |
| 346 | #define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */ |
Rich Felker | 419ae6d | 2012-05-22 21:52:08 -0400 | [diff] [blame] | 347 | #endif |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 348 | |
Rich Felker | 419ae6d | 2012-05-22 21:52:08 -0400 | [diff] [blame] | 349 | #if defined(_XOPEN_SOURCE) |
| 350 | #define MAXFLOAT 3.40282347e+38F |
| 351 | #endif |
| 352 | |
| 353 | #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 354 | extern int signgam; |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 355 | |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 356 | double j0(double); |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 357 | double j1(double); |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 358 | double jn(int, double); |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 359 | |
| 360 | double y0(double); |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 361 | double y1(double); |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 362 | double yn(int, double); |
Rich Felker | 74eea62 | 2011-02-14 18:41:25 -0500 | [diff] [blame] | 363 | #endif |
| 364 | |
| 365 | #ifdef _GNU_SOURCE |
Rich Felker | 419ae6d | 2012-05-22 21:52:08 -0400 | [diff] [blame] | 366 | #define HUGE 3.40282347e+38F |
Rich Felker | 74eea62 | 2011-02-14 18:41:25 -0500 | [diff] [blame] | 367 | double scalb(double, double); |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 368 | float scalbf(float, float); |
| 369 | long double scalbl(long double, long double); |
nsz | 0144b45 | 2012-03-15 08:17:28 +0100 | [diff] [blame] | 370 | |
| 371 | void sincos(double, double*, double*); |
| 372 | void sincosf(float, float*, float*); |
| 373 | void sincosl(long double, long double*, long double*); |
nsz | 40305f7 | 2012-03-15 09:29:53 +0100 | [diff] [blame] | 374 | |
Rich Felker | 8e09221 | 2012-03-17 21:48:48 -0400 | [diff] [blame] | 375 | double gamma(double); |
| 376 | float gammaf(float); |
| 377 | long double gammal(long double); |
| 378 | |
nsz | 40305f7 | 2012-03-15 09:29:53 +0100 | [diff] [blame] | 379 | double lgamma_r(double, int*); |
| 380 | float lgammaf_r(float, int*); |
| 381 | long double lgammal_r(long double, int*); |
| 382 | |
| 383 | float j0f(float); |
| 384 | long double j0l(long double); |
| 385 | float j1f(float); |
| 386 | long double j1l(long double); |
| 387 | float jnf(int, float); |
| 388 | long double jnl(int, long double); |
| 389 | |
| 390 | float y0f(float); |
| 391 | long double y0l(long double); |
| 392 | float y1f(float); |
| 393 | long double y1l(long double); |
| 394 | float ynf(int, float); |
| 395 | long double ynl(int, long double); |
Rich Felker | f681975 | 2012-04-30 03:26:53 -0400 | [diff] [blame] | 396 | |
| 397 | double exp10(double); |
| 398 | float exp10f(float); |
| 399 | long double exp10l(long double); |
Rich Felker | a917c03 | 2012-05-01 00:07:37 -0400 | [diff] [blame] | 400 | double pow10(double); |
| 401 | float pow10f(float); |
| 402 | long double pow10l(long double); |
Rich Felker | 74eea62 | 2011-02-14 18:41:25 -0500 | [diff] [blame] | 403 | #endif |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 404 | |
Rich Felker | 3ed8c9f | 2011-11-10 20:40:06 -0500 | [diff] [blame] | 405 | #ifdef __cplusplus |
| 406 | } |
| 407 | #endif |
| 408 | |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 409 | #endif |