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