cos_pi.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/config/no_tr1/cmath.hpp>
  11. #include <boost/math/tools/config.hpp>
  12. #include <boost/math/special_functions/trunc.hpp>
  13. #include <boost/math/tools/promotion.hpp>
  14. #include <boost/math/constants/constants.hpp>
  15. namespace boost{ namespace math{ namespace detail{
  16. template <class T, class Policy>
  17. T cos_pi_imp(T x, const Policy& pol)
  18. {
  19. BOOST_MATH_STD_USING // ADL of std names
  20. // cos of pi*x:
  21. bool invert = false;
  22. if(fabs(x) < 0.5)
  23. return cos(constants::pi<T>() * x);
  24. if(x < 1)
  25. {
  26. x = -x;
  27. }
  28. T rem = floor(x);
  29. if(itrunc(rem, pol) & 1)
  30. invert = !invert;
  31. rem = x - rem;
  32. if(rem > 0.5f)
  33. {
  34. rem = 1 - rem;
  35. invert = !invert;
  36. }
  37. if(rem == 0.5f)
  38. return 0;
  39. rem = cos(constants::pi<T>() * rem);
  40. return invert ? T(-rem) : rem;
  41. }
  42. } // namespace detail
  43. template <class T, class Policy>
  44. inline typename tools::promote_args<T>::type cos_pi(T x, const Policy& pol)
  45. {
  46. typedef typename tools::promote_args<T>::type result_type;
  47. return boost::math::detail::cos_pi_imp<result_type>(x, pol);
  48. }
  49. template <class T>
  50. inline typename tools::promote_args<T>::type cos_pi(T x)
  51. {
  52. return boost::math::cos_pi(x, policies::policy<>());
  53. }
  54. } // namespace math
  55. } // namespace boost
  56. #endif