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 | |
6 | #ifndef BOOST_MATH_SIN_PI_HPP |
7 | #define BOOST_MATH_SIN_PI_HPP |
8 | |
9 | #ifdef _MSC_VER |
10 | #pragma once |
11 | #endif |
12 | |
13 | #include <boost/config/no_tr1/cmath.hpp> |
14 | #include <boost/math/tools/config.hpp> |
15 | #include <boost/math/special_functions/math_fwd.hpp> |
16 | #include <boost/math/special_functions/trunc.hpp> |
17 | #include <boost/math/tools/promotion.hpp> |
18 | #include <boost/math/constants/constants.hpp> |
19 | |
20 | namespace boost{ namespace math{ namespace detail{ |
21 | |
22 | template <class T, class Policy> |
23 | inline T sin_pi_imp(T x, const Policy& pol) |
24 | { |
25 | BOOST_MATH_STD_USING // ADL of std names |
26 | if(x < 0) |
27 | return -sin_pi_imp(T(-x), pol); |
28 | // sin of pi*x: |
29 | bool invert; |
30 | if(x < 0.5) |
31 | return sin(constants::pi<T>() * x); |
32 | if(x < 1) |
33 | { |
34 | invert = true; |
35 | x = -x; |
36 | } |
37 | else |
38 | invert = false; |
39 | |
40 | T rem = floor(x); |
41 | if(iconvert(rem, pol) & 1) |
42 | invert = !invert; |
43 | rem = x - rem; |
44 | if(rem > 0.5f) |
45 | rem = 1 - rem; |
46 | if(rem == 0.5f) |
47 | return static_cast<T>(invert ? -1 : 1); |
48 | |
49 | rem = sin(constants::pi<T>() * rem); |
50 | return invert ? T(-rem) : rem; |
51 | } |
52 | |
53 | } // namespace detail |
54 | |
55 | template <class T, class Policy> |
56 | inline typename tools::promote_args<T>::type sin_pi(T x, const Policy&) |
57 | { |
58 | typedef typename tools::promote_args<T>::type result_type; |
59 | typedef typename policies::evaluation<result_type, Policy>::type value_type; |
60 | typedef typename policies::normalise< |
61 | Policy, |
62 | policies::promote_float<false>, |
63 | policies::promote_double<false>, |
64 | policies::discrete_quantile<>, |
65 | policies::assert_undefined<>, |
66 | // We want to ignore overflows since the result is in [-1,1] and the |
67 | // check slows the code down considerably. |
68 | policies::overflow_error<policies::ignore_error> >::type forwarding_policy; |
69 | return policies::checked_narrowing_cast<result_type, forwarding_policy>(boost::math::detail::sin_pi_imp<value_type>(x, forwarding_policy()), "sin_pi" ); |
70 | } |
71 | |
72 | template <class T> |
73 | inline typename tools::promote_args<T>::type sin_pi(T x) |
74 | { |
75 | return boost::math::sin_pi(x, policies::policy<>()); |
76 | } |
77 | |
78 | } // namespace math |
79 | } // namespace boost |
80 | #endif |
81 | |
82 | |