1 // -*- C++ -*- 2 //===---------------------------- cmath -----------------------------------===// 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is dual licensed under the MIT and the University of Illinois Open 7 // Source Licenses. See LICENSE.TXT for details. 8 // 9 //===----------------------------------------------------------------------===// 10 11 #ifndef _LIBCPP_CMATH 12 #define _LIBCPP_CMATH 13 14 /* 15 cmath synopsis 16 17 Macros: 18 19 HUGE_VAL 20 HUGE_VALF // C99 21 HUGE_VALL // C99 22 INFINITY // C99 23 NAN // C99 24 FP_INFINITE // C99 25 FP_NAN // C99 26 FP_NORMAL // C99 27 FP_SUBNORMAL // C99 28 FP_ZERO // C99 29 FP_FAST_FMA // C99 30 FP_FAST_FMAF // C99 31 FP_FAST_FMAL // C99 32 FP_ILOGB0 // C99 33 FP_ILOGBNAN // C99 34 MATH_ERRNO // C99 35 MATH_ERREXCEPT // C99 36 math_errhandling // C99 37 38 namespace std 39 { 40 41 Types: 42 43 float_t // C99 44 double_t // C99 45 46 // C90 47 48 floating_point abs(floating_point x); 49 50 floating_point acos (arithmetic x); 51 float acosf(float x); 52 long double acosl(long double x); 53 54 floating_point asin (arithmetic x); 55 float asinf(float x); 56 long double asinl(long double x); 57 58 floating_point atan (arithmetic x); 59 float atanf(float x); 60 long double atanl(long double x); 61 62 floating_point atan2 (arithmetic y, arithmetic x); 63 float atan2f(float y, float x); 64 long double atan2l(long double y, long double x); 65 66 floating_point ceil (arithmetic x); 67 float ceilf(float x); 68 long double ceill(long double x); 69 70 floating_point cos (arithmetic x); 71 float cosf(float x); 72 long double cosl(long double x); 73 74 floating_point cosh (arithmetic x); 75 float coshf(float x); 76 long double coshl(long double x); 77 78 floating_point exp (arithmetic x); 79 float expf(float x); 80 long double expl(long double x); 81 82 floating_point fabs (arithmetic x); 83 float fabsf(float x); 84 long double fabsl(long double x); 85 86 floating_point floor (arithmetic x); 87 float floorf(float x); 88 long double floorl(long double x); 89 90 floating_point fmod (arithmetic x, arithmetic y); 91 float fmodf(float x, float y); 92 long double fmodl(long double x, long double y); 93 94 floating_point frexp (arithmetic value, int* exp); 95 float frexpf(float value, int* exp); 96 long double frexpl(long double value, int* exp); 97 98 floating_point ldexp (arithmetic value, int exp); 99 float ldexpf(float value, int exp); 100 long double ldexpl(long double value, int exp); 101 102 floating_point log (arithmetic x); 103 float logf(float x); 104 long double logl(long double x); 105 106 floating_point log10 (arithmetic x); 107 float log10f(float x); 108 long double log10l(long double x); 109 110 floating_point modf (floating_point value, floating_point* iptr); 111 float modff(float value, float* iptr); 112 long double modfl(long double value, long double* iptr); 113 114 floating_point pow (arithmetic x, arithmetic y); 115 float powf(float x, float y); 116 long double powl(long double x, long double y); 117 118 floating_point sin (arithmetic x); 119 float sinf(float x); 120 long double sinl(long double x); 121 122 floating_point sinh (arithmetic x); 123 float sinhf(float x); 124 long double sinhl(long double x); 125 126 floating_point sqrt (arithmetic x); 127 float sqrtf(float x); 128 long double sqrtl(long double x); 129 130 floating_point tan (arithmetic x); 131 float tanf(float x); 132 long double tanl(long double x); 133 134 floating_point tanh (arithmetic x); 135 float tanhf(float x); 136 long double tanhl(long double x); 137 138 // C99 139 140 bool signbit(arithmetic x); 141 142 int fpclassify(arithmetic x); 143 144 bool isfinite(arithmetic x); 145 bool isinf(arithmetic x); 146 bool isnan(arithmetic x); 147 bool isnormal(arithmetic x); 148 149 bool isgreater(arithmetic x, arithmetic y); 150 bool isgreaterequal(arithmetic x, arithmetic y); 151 bool isless(arithmetic x, arithmetic y); 152 bool islessequal(arithmetic x, arithmetic y); 153 bool islessgreater(arithmetic x, arithmetic y); 154 bool isunordered(arithmetic x, arithmetic y); 155 156 floating_point acosh (arithmetic x); 157 float acoshf(float x); 158 long double acoshl(long double x); 159 160 floating_point asinh (arithmetic x); 161 float asinhf(float x); 162 long double asinhl(long double x); 163 164 floating_point atanh (arithmetic x); 165 float atanhf(float x); 166 long double atanhl(long double x); 167 168 floating_point cbrt (arithmetic x); 169 float cbrtf(float x); 170 long double cbrtl(long double x); 171 172 floating_point copysign (arithmetic x, arithmetic y); 173 float copysignf(float x, float y); 174 long double copysignl(long double x, long double y); 175 176 floating_point erf (arithmetic x); 177 float erff(float x); 178 long double erfl(long double x); 179 180 floating_point erfc (arithmetic x); 181 float erfcf(float x); 182 long double erfcl(long double x); 183 184 floating_point exp2 (arithmetic x); 185 float exp2f(float x); 186 long double exp2l(long double x); 187 188 floating_point expm1 (arithmetic x); 189 float expm1f(float x); 190 long double expm1l(long double x); 191 192 floating_point fdim (arithmetic x, arithmetic y); 193 float fdimf(float x, float y); 194 long double fdiml(long double x, long double y); 195 196 floating_point fma (arithmetic x, arithmetic y, arithmetic z); 197 float fmaf(float x, float y, float z); 198 long double fmal(long double x, long double y, long double z); 199 200 floating_point fmax (arithmetic x, arithmetic y); 201 float fmaxf(float x, float y); 202 long double fmaxl(long double x, long double y); 203 204 floating_point fmin (arithmetic x, arithmetic y); 205 float fminf(float x, float y); 206 long double fminl(long double x, long double y); 207 208 floating_point hypot (arithmetic x, arithmetic y); 209 float hypotf(float x, float y); 210 long double hypotl(long double x, long double y); 211 212 double hypot(double x, double y, double z); // C++17 213 float hypot(float x, float y, float z); // C++17 214 long double hypot(long double x, long double y, long double z); // C++17 215 216 int ilogb (arithmetic x); 217 int ilogbf(float x); 218 int ilogbl(long double x); 219 220 floating_point lgamma (arithmetic x); 221 float lgammaf(float x); 222 long double lgammal(long double x); 223 224 long long llrint (arithmetic x); 225 long long llrintf(float x); 226 long long llrintl(long double x); 227 228 long long llround (arithmetic x); 229 long long llroundf(float x); 230 long long llroundl(long double x); 231 232 floating_point log1p (arithmetic x); 233 float log1pf(float x); 234 long double log1pl(long double x); 235 236 floating_point log2 (arithmetic x); 237 float log2f(float x); 238 long double log2l(long double x); 239 240 floating_point logb (arithmetic x); 241 float logbf(float x); 242 long double logbl(long double x); 243 244 long lrint (arithmetic x); 245 long lrintf(float x); 246 long lrintl(long double x); 247 248 long lround (arithmetic x); 249 long lroundf(float x); 250 long lroundl(long double x); 251 252 double nan (const char* str); 253 float nanf(const char* str); 254 long double nanl(const char* str); 255 256 floating_point nearbyint (arithmetic x); 257 float nearbyintf(float x); 258 long double nearbyintl(long double x); 259 260 floating_point nextafter (arithmetic x, arithmetic y); 261 float nextafterf(float x, float y); 262 long double nextafterl(long double x, long double y); 263 264 floating_point nexttoward (arithmetic x, long double y); 265 float nexttowardf(float x, long double y); 266 long double nexttowardl(long double x, long double y); 267 268 floating_point remainder (arithmetic x, arithmetic y); 269 float remainderf(float x, float y); 270 long double remainderl(long double x, long double y); 271 272 floating_point remquo (arithmetic x, arithmetic y, int* pquo); 273 float remquof(float x, float y, int* pquo); 274 long double remquol(long double x, long double y, int* pquo); 275 276 floating_point rint (arithmetic x); 277 float rintf(float x); 278 long double rintl(long double x); 279 280 floating_point round (arithmetic x); 281 float roundf(float x); 282 long double roundl(long double x); 283 284 floating_point scalbln (arithmetic x, long ex); 285 float scalblnf(float x, long ex); 286 long double scalblnl(long double x, long ex); 287 288 floating_point scalbn (arithmetic x, int ex); 289 float scalbnf(float x, int ex); 290 long double scalbnl(long double x, int ex); 291 292 floating_point tgamma (arithmetic x); 293 float tgammaf(float x); 294 long double tgammal(long double x); 295 296 floating_point trunc (arithmetic x); 297 float truncf(float x); 298 long double truncl(long double x); 299 300 } // std 301 302 */ 303 304 #include <__config> 305 #include <math.h> 306 307 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 308 #pragma GCC system_header 309 #endif 310 311 _LIBCPP_BEGIN_NAMESPACE_STD 312 313 using ::signbit; 314 using ::fpclassify; 315 using ::isfinite; 316 using ::isinf; 317 using ::isnan; 318 using ::isnormal; 319 using ::isgreater; 320 using ::isgreaterequal; 321 using ::isless; 322 using ::islessequal; 323 using ::islessgreater; 324 using ::isunordered; 325 using ::isunordered; 326 327 using ::float_t; 328 using ::double_t; 329 330 #ifndef _AIX 331 using ::abs; 332 #endif 333 334 using ::acos; 335 using ::acosf; 336 using ::asin; 337 using ::asinf; 338 using ::atan; 339 using ::atanf; 340 using ::atan2; 341 using ::atan2f; 342 using ::ceil; 343 using ::ceilf; 344 using ::cos; 345 using ::cosf; 346 using ::cosh; 347 using ::coshf; 348 349 using ::exp; 350 using ::expf; 351 352 using ::fabs; 353 using ::fabsf; 354 using ::floor; 355 using ::floorf; 356 357 using ::fmod; 358 using ::fmodf; 359 360 using ::frexp; 361 using ::frexpf; 362 using ::ldexp; 363 using ::ldexpf; 364 365 using ::log; 366 using ::logf; 367 368 using ::log10; 369 using ::log10f; 370 using ::modf; 371 using ::modff; 372 373 using ::pow; 374 using ::powf; 375 376 using ::sin; 377 using ::sinf; 378 using ::sinh; 379 using ::sinhf; 380 381 using ::sqrt; 382 using ::sqrtf; 383 using ::tan; 384 using ::tanf; 385 386 using ::tanh; 387 using ::tanhf; 388 389 using ::acosh; 390 using ::acoshf; 391 using ::asinh; 392 using ::asinhf; 393 using ::atanh; 394 using ::atanhf; 395 using ::cbrt; 396 using ::cbrtf; 397 398 using ::copysign; 399 using ::copysignf; 400 401 using ::erf; 402 using ::erff; 403 using ::erfc; 404 using ::erfcf; 405 using ::exp2; 406 using ::exp2f; 407 using ::expm1; 408 using ::expm1f; 409 using ::fdim; 410 using ::fdimf; 411 using ::fmaf; 412 using ::fma; 413 using ::fmax; 414 using ::fmaxf; 415 using ::fmin; 416 using ::fminf; 417 using ::hypot; 418 using ::hypotf; 419 using ::ilogb; 420 using ::ilogbf; 421 using ::lgamma; 422 using ::lgammaf; 423 using ::llrint; 424 using ::llrintf; 425 using ::llround; 426 using ::llroundf; 427 using ::log1p; 428 using ::log1pf; 429 using ::log2; 430 using ::log2f; 431 using ::logb; 432 using ::logbf; 433 using ::lrint; 434 using ::lrintf; 435 using ::lround; 436 using ::lroundf; 437 438 using ::nan; 439 using ::nanf; 440 441 using ::nearbyint; 442 using ::nearbyintf; 443 using ::nextafter; 444 using ::nextafterf; 445 using ::nexttoward; 446 using ::nexttowardf; 447 using ::remainder; 448 using ::remainderf; 449 using ::remquo; 450 using ::remquof; 451 using ::rint; 452 using ::rintf; 453 using ::round; 454 using ::roundf; 455 using ::scalbln; 456 using ::scalblnf; 457 using ::scalbn; 458 using ::scalbnf; 459 using ::tgamma; 460 using ::tgammaf; 461 using ::trunc; 462 using ::truncf; 463 464 using ::acosl; 465 using ::asinl; 466 using ::atanl; 467 using ::atan2l; 468 using ::ceill; 469 using ::cosl; 470 using ::coshl; 471 using ::expl; 472 using ::fabsl; 473 using ::floorl; 474 using ::fmodl; 475 using ::frexpl; 476 using ::ldexpl; 477 using ::logl; 478 using ::log10l; 479 using ::modfl; 480 using ::powl; 481 using ::sinl; 482 using ::sinhl; 483 using ::sqrtl; 484 using ::tanl; 485 486 using ::tanhl; 487 using ::acoshl; 488 using ::asinhl; 489 using ::atanhl; 490 using ::cbrtl; 491 492 using ::copysignl; 493 494 using ::erfl; 495 using ::erfcl; 496 using ::exp2l; 497 using ::expm1l; 498 using ::fdiml; 499 using ::fmal; 500 using ::fmaxl; 501 using ::fminl; 502 using ::hypotl; 503 using ::ilogbl; 504 using ::lgammal; 505 using ::llrintl; 506 using ::llroundl; 507 using ::log1pl; 508 using ::log2l; 509 using ::logbl; 510 using ::lrintl; 511 using ::lroundl; 512 using ::nanl; 513 using ::nearbyintl; 514 using ::nextafterl; 515 using ::nexttowardl; 516 using ::remainderl; 517 using ::remquol; 518 using ::rintl; 519 using ::roundl; 520 using ::scalblnl; 521 using ::scalbnl; 522 using ::tgammal; 523 using ::truncl; 524 525 #if _LIBCPP_STD_VER > 14 526 inline _LIBCPP_INLINE_VISIBILITY float hypot( float x, float y, float z ) { return sqrt(x*x + y*y + z*z); } 527 inline _LIBCPP_INLINE_VISIBILITY double hypot( double x, double y, double z ) { return sqrt(x*x + y*y + z*z); } 528 inline _LIBCPP_INLINE_VISIBILITY long double hypot( long double x, long double y, long double z ) { return sqrt(x*x + y*y + z*z); } 529 530 template <class _A1, class _A2, class _A3> 531 inline _LIBCPP_INLINE_VISIBILITY 532 typename __lazy_enable_if 533 < 534 is_arithmetic<_A1>::value && 535 is_arithmetic<_A2>::value && 536 is_arithmetic<_A3>::value, 537 __promote<_A1, _A2, _A3> 538 >::type 539 hypot(_A1 __lcpp_x, _A2 __lcpp_y, _A3 __lcpp_z) _NOEXCEPT 540 { 541 typedef typename __promote<_A1, _A2, _A3>::type __result_type; 542 static_assert((!(is_same<_A1, __result_type>::value && 543 is_same<_A2, __result_type>::value && 544 is_same<_A3, __result_type>::value)), ""); 545 return hypot((__result_type)__lcpp_x, (__result_type)__lcpp_y, (__result_type)__lcpp_z); 546 } 547 #endif 548 549 template <class _A1> 550 _LIBCPP_ALWAYS_INLINE 551 _LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type 552 __libcpp_isnan_or_builtin(_A1 __lcpp_x) _NOEXCEPT 553 { 554 #if __has_builtin(__builtin_isnan) 555 return __builtin_isnan(__lcpp_x); 556 #else 557 return isnan(__lcpp_x); 558 #endif 559 } 560 561 template <class _A1> 562 _LIBCPP_ALWAYS_INLINE 563 _LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type 564 __libcpp_isnan_or_builtin(_A1 __lcpp_x) _NOEXCEPT 565 { 566 return isnan(__lcpp_x); 567 } 568 569 template <class _A1> 570 _LIBCPP_ALWAYS_INLINE 571 _LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type 572 __libcpp_isinf_or_builtin(_A1 __lcpp_x) _NOEXCEPT 573 { 574 #if __has_builtin(__builtin_isinf) 575 return __builtin_isinf(__lcpp_x); 576 #else 577 return isinf(__lcpp_x); 578 #endif 579 } 580 581 template <class _A1> 582 _LIBCPP_ALWAYS_INLINE 583 _LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type 584 __libcpp_isinf_or_builtin(_A1 __lcpp_x) _NOEXCEPT 585 { 586 return isinf(__lcpp_x); 587 } 588 589 template <class _A1> 590 _LIBCPP_ALWAYS_INLINE 591 _LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type 592 __libcpp_isfinite_or_builtin(_A1 __lcpp_x) _NOEXCEPT 593 { 594 #if __has_builtin(__builtin_isfinite) 595 return __builtin_isfinite(__lcpp_x); 596 #else 597 return isfinite(__lcpp_x); 598 #endif 599 } 600 601 template <class _A1> 602 _LIBCPP_ALWAYS_INLINE 603 _LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type 604 __libcpp_isfinite_or_builtin(_A1 __lcpp_x) _NOEXCEPT 605 { 606 return isfinite(__lcpp_x); 607 } 608 609 _LIBCPP_END_NAMESPACE_STD 610 611 #endif // _LIBCPP_CMATH 612