2 //===---------------------------- cmath -----------------------------------===//
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
8 //===----------------------------------------------------------------------===//
35 math_errhandling // C99
47 floating_point abs(floating_point x);
49 floating_point acos (arithmetic x);
51 long double acosl(long double x);
53 floating_point asin (arithmetic x);
55 long double asinl(long double x);
57 floating_point atan (arithmetic x);
59 long double atanl(long double x);
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);
65 floating_point ceil (arithmetic x);
67 long double ceill(long double x);
69 floating_point cos (arithmetic x);
71 long double cosl(long double x);
73 floating_point cosh (arithmetic x);
75 long double coshl(long double x);
77 floating_point exp (arithmetic x);
79 long double expl(long double x);
81 floating_point fabs (arithmetic x);
83 long double fabsl(long double x);
85 floating_point floor (arithmetic x);
86 float floorf(float x);
87 long double floorl(long double x);
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);
93 floating_point frexp (arithmetic value, int* exp);
94 float frexpf(float value, int* exp);
95 long double frexpl(long double value, int* exp);
97 floating_point ldexp (arithmetic value, int exp);
98 float ldexpf(float value, int exp);
99 long double ldexpl(long double value, int exp);
101 floating_point log (arithmetic x);
103 long double logl(long double x);
105 floating_point log10 (arithmetic x);
106 float log10f(float x);
107 long double log10l(long double x);
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);
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);
117 floating_point sin (arithmetic x);
119 long double sinl(long double x);
121 floating_point sinh (arithmetic x);
122 float sinhf(float x);
123 long double sinhl(long double x);
125 floating_point sqrt (arithmetic x);
126 float sqrtf(float x);
127 long double sqrtl(long double x);
129 floating_point tan (arithmetic x);
131 long double tanl(long double x);
133 floating_point tanh (arithmetic x);
134 float tanhf(float x);
135 long double tanhl(long double x);
139 bool signbit(arithmetic x);
141 int fpclassify(arithmetic x);
143 bool isfinite(arithmetic x);
144 bool isinf(arithmetic x);
145 bool isnan(arithmetic x);
146 bool isnormal(arithmetic x);
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);
155 floating_point acosh (arithmetic x);
156 float acoshf(float x);
157 long double acoshl(long double x);
159 floating_point asinh (arithmetic x);
160 float asinhf(float x);
161 long double asinhl(long double x);
163 floating_point atanh (arithmetic x);
164 float atanhf(float x);
165 long double atanhl(long double x);
167 floating_point cbrt (arithmetic x);
168 float cbrtf(float x);
169 long double cbrtl(long double x);
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);
175 floating_point erf (arithmetic x);
177 long double erfl(long double x);
179 floating_point erfc (arithmetic x);
180 float erfcf(float x);
181 long double erfcl(long double x);
183 floating_point exp2 (arithmetic x);
184 float exp2f(float x);
185 long double exp2l(long double x);
187 floating_point expm1 (arithmetic x);
188 float expm1f(float x);
189 long double expm1l(long double x);
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);
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);
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);
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);
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);
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
215 int ilogb (arithmetic x);
217 int ilogbl(long double x);
219 floating_point lgamma (arithmetic x);
220 float lgammaf(float x);
221 long double lgammal(long double x);
223 long long llrint (arithmetic x);
224 long long llrintf(float x);
225 long long llrintl(long double x);
227 long long llround (arithmetic x);
228 long long llroundf(float x);
229 long long llroundl(long double x);
231 floating_point log1p (arithmetic x);
232 float log1pf(float x);
233 long double log1pl(long double x);
235 floating_point log2 (arithmetic x);
236 float log2f(float x);
237 long double log2l(long double x);
239 floating_point logb (arithmetic x);
240 float logbf(float x);
241 long double logbl(long double x);
243 long lrint (arithmetic x);
244 long lrintf(float x);
245 long lrintl(long double x);
247 long lround (arithmetic x);
248 long lroundf(float x);
249 long lroundl(long double x);
251 double nan (const char* str);
252 float nanf(const char* str);
253 long double nanl(const char* str);
255 floating_point nearbyint (arithmetic x);
256 float nearbyintf(float x);
257 long double nearbyintl(long double x);
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);
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);
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);
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);
275 floating_point rint (arithmetic x);
276 float rintf(float x);
277 long double rintl(long double x);
279 floating_point round (arithmetic x);
280 float roundf(float x);
281 long double roundl(long double x);
283 floating_point scalbln (arithmetic x, long ex);
284 float scalblnf(float x, long ex);
285 long double scalblnl(long double x, long ex);
287 floating_point scalbn (arithmetic x, int ex);
288 float scalbnf(float x, int ex);
289 long double scalbnl(long double x, int ex);
291 floating_point tgamma (arithmetic x);
292 float tgammaf(float x);
293 long double tgammal(long double x);
295 floating_point trunc (arithmetic x);
296 float truncf(float x);
297 long double truncl(long double x);
306 #include <type_traits>
308 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
309 #pragma GCC system_header
313 #include <__undef_macros>
315 _LIBCPP_BEGIN_NAMESPACE_STD
324 using ::isgreaterequal;
327 using ::islessgreater;
529 #if _LIBCPP_STD_VER > 14
530 inline _LIBCPP_INLINE_VISIBILITY float hypot( float x, float y, float z ) { return sqrt(x*x + y*y + z*z); }
531 inline _LIBCPP_INLINE_VISIBILITY double hypot( double x, double y, double z ) { return sqrt(x*x + y*y + z*z); }
532 inline _LIBCPP_INLINE_VISIBILITY long double hypot( long double x, long double y, long double z ) { return sqrt(x*x + y*y + z*z); }
534 template <class _A1, class _A2, class _A3>
535 inline _LIBCPP_INLINE_VISIBILITY
538 is_arithmetic<_A1>::value &&
539 is_arithmetic<_A2>::value &&
540 is_arithmetic<_A3>::value,
541 __promote<_A1, _A2, _A3>
543 hypot(_A1 __lcpp_x, _A2 __lcpp_y, _A3 __lcpp_z) _NOEXCEPT
545 typedef typename __promote<_A1, _A2, _A3>::type __result_type;
546 static_assert((!(is_same<_A1, __result_type>::value &&
547 is_same<_A2, __result_type>::value &&
548 is_same<_A3, __result_type>::value)), "");
549 return hypot((__result_type)__lcpp_x, (__result_type)__lcpp_y, (__result_type)__lcpp_z);
554 _LIBCPP_INLINE_VISIBILITY
555 _LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
556 __libcpp_isnan_or_builtin(_A1 __lcpp_x) _NOEXCEPT
558 #if __has_builtin(__builtin_isnan)
559 return __builtin_isnan(__lcpp_x);
561 return isnan(__lcpp_x);
566 _LIBCPP_INLINE_VISIBILITY
567 _LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
568 __libcpp_isnan_or_builtin(_A1 __lcpp_x) _NOEXCEPT
570 return isnan(__lcpp_x);
574 _LIBCPP_INLINE_VISIBILITY
575 _LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
576 __libcpp_isinf_or_builtin(_A1 __lcpp_x) _NOEXCEPT
578 #if __has_builtin(__builtin_isinf)
579 return __builtin_isinf(__lcpp_x);
581 return isinf(__lcpp_x);
586 _LIBCPP_INLINE_VISIBILITY
587 _LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
588 __libcpp_isinf_or_builtin(_A1 __lcpp_x) _NOEXCEPT
590 return isinf(__lcpp_x);
594 _LIBCPP_INLINE_VISIBILITY
595 _LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
596 __libcpp_isfinite_or_builtin(_A1 __lcpp_x) _NOEXCEPT
598 #if __has_builtin(__builtin_isfinite)
599 return __builtin_isfinite(__lcpp_x);
601 return isfinite(__lcpp_x);
606 _LIBCPP_INLINE_VISIBILITY
607 _LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
608 __libcpp_isfinite_or_builtin(_A1 __lcpp_x) _NOEXCEPT
610 return isfinite(__lcpp_x);
613 #if _LIBCPP_STD_VER > 17
614 template <typename _Fp>
616 _Fp __lerp(_Fp __a, _Fp __b, _Fp __t) noexcept {
617 if ((__a <= 0 && __b >= 0) || (__a >= 0 && __b <= 0))
618 return __t * __b + (1 - __t) * __a;
620 if (__t == 1) return __b;
621 const _Fp __x = __a + __t * (__b - __a);
622 if (__t > 1 == __b > __a)
623 return __b < __x ? __x : __b;
625 return __x < __b ? __x : __b;
629 lerp(float __a, float __b, float __t) _NOEXCEPT { return __lerp(__a, __b, __t); }
632 lerp(double __a, double __b, double __t) _NOEXCEPT { return __lerp(__a, __b, __t); }
634 constexpr long double
635 lerp(long double __a, long double __b, long double __t) _NOEXCEPT { return __lerp(__a, __b, __t); }
637 #endif // _LIBCPP_STD_VER > 17
639 template <class _IntT, class _FloatT,
640 bool _FloatBigger = (numeric_limits<_FloatT>::digits > numeric_limits<_IntT>::digits),
641 int _Bits = (numeric_limits<_IntT>::digits - numeric_limits<_FloatT>::digits)>
642 _LIBCPP_INLINE_VISIBILITY
643 _LIBCPP_CONSTEXPR _IntT __max_representable_int_for_float() _NOEXCEPT {
644 static_assert(is_floating_point<_FloatT>::value, "must be a floating point type");
645 static_assert(is_integral<_IntT>::value, "must be an integral type");
646 static_assert(numeric_limits<_FloatT>::radix == 2, "FloatT has incorrect radix");
647 static_assert((_IsSame<_FloatT, float>::value || _IsSame<_FloatT, double>::value
648 || _IsSame<_FloatT,long double>::value), "unsupported floating point type");
649 return _FloatBigger ? numeric_limits<_IntT>::max() : (numeric_limits<_IntT>::max() >> _Bits << _Bits);
652 // Convert a floating point number to the specified integral type after
653 // clamping to the integral types representable range.
655 // The behavior is undefined if `__r` is NaN.
656 template <class _IntT, class _RealT>
657 _LIBCPP_INLINE_VISIBILITY
658 _IntT __clamp_to_integral(_RealT __r) _NOEXCEPT {
659 using _Lim = std::numeric_limits<_IntT>;
660 const _IntT _MaxVal = std::__max_representable_int_for_float<_IntT, _RealT>();
661 if (__r >= ::nextafter(static_cast<_RealT>(_MaxVal), INFINITY)) {
663 } else if (__r <= _Lim::lowest()) {
666 return static_cast<_IntT>(__r);
669 _LIBCPP_END_NAMESPACE_STD
673 #endif // _LIBCPP_CMATH