sin_pi.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_SIN_PI_HPP
  6. #define BOOST_MATH_SIN_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/math_fwd.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 sin_pi_imp(T x, const Policy& pol)
  19. {
  20. BOOST_MATH_STD_USING // ADL of std names
  21. if(x < 0)
  22. return -sin_pi(-x);
  23. // sin of pi*x:
  24. bool invert;
  25. if(x < 0.5)
  26. return sin(constants::pi<T>() * x);
  27. if(x < 1)
  28. {
  29. invert = true;
  30. x = -x;
  31. }
  32. else
  33. invert = false;
  34. T rem = floor(x);
  35. if(itrunc(rem, pol) & 1)
  36. invert = !invert;
  37. rem = x - rem;
  38. if(rem > 0.5f)
  39. rem = 1 - rem;
  40. if(rem == 0.5f)
  41. return static_cast<T>(invert ? -1 : 1);
  42. rem = sin(constants::pi<T>() * rem);
  43. return invert ? T(-rem) : rem;
  44. }
  45. } // namespace detail
  46. template <class T, class Policy>
  47. inline typename tools::promote_args<T>::type sin_pi(T x, const Policy&)
  48. {
  49. typedef typename tools::promote_args<T>::type result_type;
  50. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  51. typedef typename policies::normalise<
  52. Policy,
  53. policies::promote_float<false>,
  54. policies::promote_double<false>,
  55. policies::discrete_quantile<>,
  56. policies::assert_undefined<> >::type forwarding_policy;
  57. return policies::checked_narrowing_cast<result_type, forwarding_policy>(boost::math::detail::sin_pi_imp<value_type>(x, forwarding_policy()), "cos_pi");
  58. }
  59. template <class T>
  60. inline typename tools::promote_args<T>::type sin_pi(T x)
  61. {
  62. return boost::math::sin_pi(x, policies::policy<>());
  63. }
  64. } // namespace math
  65. } // namespace boost
  66. #endif