Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:40:10

0001 //  Copyright (c) 2007 John Maddock
0002 //  Use, modification and distribution are subject to the
0003 //  Boost Software License, Version 1.0. (See accompanying file
0004 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_MATH_COS_PI_HPP
0007 #define BOOST_MATH_COS_PI_HPP
0008 
0009 #ifdef _MSC_VER
0010 #pragma once
0011 #endif
0012 
0013 #include <cmath>
0014 #include <limits>
0015 #include <boost/math/special_functions/math_fwd.hpp>
0016 #include <boost/math/tools/config.hpp>
0017 #include <boost/math/special_functions/trunc.hpp>
0018 #include <boost/math/tools/promotion.hpp>
0019 #include <boost/math/constants/constants.hpp>
0020 
0021 namespace boost{ namespace math{ namespace detail{
0022 
0023 template <class T, class Policy>
0024 T cos_pi_imp(T x, const Policy&)
0025 {
0026    BOOST_MATH_STD_USING // ADL of std names
0027    // cos of pi*x:
0028    bool invert = false;
0029    if(fabs(x) < T(0.25))
0030       return cos(constants::pi<T>() * x);
0031 
0032    if(x < 0)
0033    {
0034       x = -x;
0035    }
0036    T rem = floor(x);
0037    if(abs(floor(rem/2)*2 - rem) > std::numeric_limits<T>::epsilon())
0038    {
0039       invert = !invert;
0040    }
0041    rem = x - rem;
0042    if(rem > 0.5f)
0043    {
0044       rem = 1 - rem;
0045       invert = !invert;
0046    }
0047    if(rem == 0.5f)
0048       return 0;
0049    
0050    if(rem > 0.25f)
0051    {
0052       rem = 0.5f - rem;
0053       rem = sin(constants::pi<T>() * rem);
0054    }
0055    else
0056       rem = cos(constants::pi<T>() * rem);
0057    return invert ? T(-rem) : rem;
0058 }
0059 
0060 } // namespace detail
0061 
0062 template <class T, class Policy>
0063 inline typename tools::promote_args<T>::type cos_pi(T x, const Policy&)
0064 {
0065    typedef typename tools::promote_args<T>::type result_type;
0066    typedef typename policies::evaluation<result_type, Policy>::type value_type;
0067    typedef typename policies::normalise<
0068       Policy,
0069       policies::promote_float<false>,
0070       policies::promote_double<false>,
0071       policies::discrete_quantile<>,
0072       policies::assert_undefined<>,
0073       // We want to ignore overflows since the result is in [-1,1] and the 
0074       // check slows the code down considerably.
0075       policies::overflow_error<policies::ignore_error> >::type forwarding_policy;
0076    return policies::checked_narrowing_cast<result_type, forwarding_policy>(boost::math::detail::cos_pi_imp<value_type>(x, forwarding_policy()), "cos_pi");
0077 }
0078 
0079 template <class T>
0080 inline typename tools::promote_args<T>::type cos_pi(T x)
0081 {
0082    return boost::math::cos_pi(x, policies::policy<>());
0083 }
0084 
0085 } // namespace math
0086 } // namespace boost
0087 #endif
0088