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