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

source code of include/boost/math/special_functions/sin_pi.hpp