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