cos_pi.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2007 John Maddock
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_COS_PI_HPP
  6. #define BOOST_MATH_COS_PI_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/special_functions/math_fwd.hpp>
  11. #include <boost/config/no_tr1/cmath.hpp>
  12. #include <boost/math/tools/config.hpp>
  13. #include <boost/math/special_functions/trunc.hpp>
  14. #include <boost/math/tools/promotion.hpp>
  15. #include <boost/math/constants/constants.hpp>
  16. namespace boost{ namespace math{ namespace detail{
  17. template <class T, class Policy>
  18. T cos_pi_imp(T x, const Policy& pol)
  19. {
  20. BOOST_MATH_STD_USING // ADL of std names
  21. // cos of pi*x:
  22. bool invert = false;
  23. if(fabs(x) < 0.25)
  24. return cos(constants::pi<T>() * x);
  25. if(x < 0)
  26. {
  27. x = -x;
  28. }
  29. T rem = floor(x);
  30. if(itrunc(rem, pol) & 1)
  31. invert = !invert;
  32. rem = x - rem;
  33. if(rem > 0.5f)
  34. {
  35. rem = 1 - rem;
  36. invert = !invert;
  37. }
  38. if(rem == 0.5f)
  39. return 0;
  40. if(rem > 0.25f)
  41. {
  42. rem = 0.5f - rem;
  43. rem = sin(constants::pi<T>() * rem);
  44. }
  45. else
  46. rem = cos(constants::pi<T>() * rem);
  47. return invert ? T(-rem) : rem;
  48. }
  49. } // namespace detail
  50. template <class T, class Policy>
  51. inline typename tools::promote_args<T>::type cos_pi(T x, const Policy&)
  52. {
  53. typedef typename tools::promote_args<T>::type result_type;
  54. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  55. typedef typename policies::normalise<
  56. Policy,
  57. policies::promote_float<false>,
  58. policies::promote_double<false>,
  59. policies::discrete_quantile<>,
  60. policies::assert_undefined<> >::type forwarding_policy;
  61. return policies::checked_narrowing_cast<result_type, forwarding_policy>(boost::math::detail::cos_pi_imp<value_type>(x, forwarding_policy()), "cos_pi");
  62. }
  63. template <class T>
  64. inline typename tools::promote_args<T>::type cos_pi(T x)
  65. {
  66. return boost::math::cos_pi(x, policies::policy<>());
  67. }
  68. } // namespace math
  69. } // namespace boost
  70. #endif