File indexing completed on 2025-09-18 08:49:09
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_MATH_COS_PI_HPP
0008 #define BOOST_MATH_COS_PI_HPP
0009
0010 #ifdef _MSC_VER
0011 #pragma once
0012 #endif
0013
0014 #include <boost/math/tools/config.hpp>
0015
0016 #ifndef BOOST_MATH_HAS_NVRTC
0017
0018 #include <cmath>
0019 #include <limits>
0020 #include <boost/math/tools/numeric_limits.hpp>
0021 #include <boost/math/special_functions/math_fwd.hpp>
0022 #include <boost/math/special_functions/trunc.hpp>
0023 #include <boost/math/tools/promotion.hpp>
0024 #include <boost/math/constants/constants.hpp>
0025
0026 namespace boost{ namespace math{ namespace detail{
0027
0028 template <class T, class Policy>
0029 BOOST_MATH_GPU_ENABLED T cos_pi_imp(T x, const Policy&)
0030 {
0031 BOOST_MATH_STD_USING
0032
0033 bool invert = false;
0034 if(fabs(x) < T(0.25))
0035 return cos(constants::pi<T>() * x);
0036
0037 if(x < 0)
0038 {
0039 x = -x;
0040 }
0041 T rem = floor(x);
0042 if(abs(floor(rem/2)*2 - rem) > boost::math::numeric_limits<T>::epsilon())
0043 {
0044 invert = !invert;
0045 }
0046 rem = x - rem;
0047 if(rem > 0.5f)
0048 {
0049 rem = 1 - rem;
0050 invert = !invert;
0051 }
0052 if(rem == 0.5f)
0053 return 0;
0054
0055 if(rem > 0.25f)
0056 {
0057 rem = 0.5f - rem;
0058 rem = sin(constants::pi<T>() * rem);
0059 }
0060 else
0061 rem = cos(constants::pi<T>() * rem);
0062 return invert ? T(-rem) : rem;
0063 }
0064
0065 }
0066
0067 template <class T, class Policy>
0068 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type cos_pi(T x, const Policy&)
0069 {
0070 typedef typename tools::promote_args<T>::type result_type;
0071 typedef typename policies::evaluation<result_type, Policy>::type value_type;
0072 typedef typename policies::normalise<
0073 Policy,
0074 policies::promote_float<false>,
0075 policies::promote_double<false>,
0076 policies::discrete_quantile<>,
0077 policies::assert_undefined<>,
0078
0079
0080 policies::overflow_error<policies::ignore_error> >::type forwarding_policy;
0081 return policies::checked_narrowing_cast<result_type, forwarding_policy>(boost::math::detail::cos_pi_imp<value_type>(x, forwarding_policy()), "cos_pi");
0082 }
0083
0084 template <class T>
0085 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type cos_pi(T x)
0086 {
0087 return boost::math::cos_pi(x, policies::policy<>());
0088 }
0089
0090 }
0091 }
0092
0093 #else
0094
0095 namespace boost {
0096 namespace math {
0097
0098 template <typename T>
0099 BOOST_MATH_GPU_ENABLED auto cos_pi(T x)
0100 {
0101 return ::cospi(x);
0102 }
0103
0104 template <>
0105 BOOST_MATH_GPU_ENABLED auto cos_pi(float x)
0106 {
0107 return ::cospif(x);
0108 }
0109
0110 template <typename T, typename Policy>
0111 BOOST_MATH_GPU_ENABLED auto cos_pi(T x, const Policy&)
0112 {
0113 return ::cospi(x);
0114 }
0115
0116 template <typename Policy>
0117 BOOST_MATH_GPU_ENABLED auto cos_pi(float x, const Policy&)
0118 {
0119 return ::cospif(x);
0120 }
0121
0122 }
0123 }
0124
0125 #endif
0126
0127 #endif
0128