sin_pi.hpp 1.6 KB

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