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