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