sin_pi.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. inline 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_imp(T(-x), pol);
  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(iconvert(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<>,
  57. // We want to igore overflows since the result is in [-1,1] and the
  58. // check slows the code down considerably.
  59. policies::overflow_error<policies::ignore_error> >::type forwarding_policy;
  60. return policies::checked_narrowing_cast<result_type, forwarding_policy>(boost::math::detail::sin_pi_imp<value_type>(x, forwarding_policy()), "sin_pi");
  61. }
  62. template <class T>
  63. inline typename tools::promote_args<T>::type sin_pi(T x)
  64. {
  65. return boost::math::sin_pi(x, policies::policy<>());
  66. }
  67. } // namespace math
  68. } // namespace boost
  69. #endif