我写了以下代码:
#include<array>
#include<type_traits>
namespace math{
namespace detail{
template<std::size_t... Is> struct seq{};
template<std::size_t N, std::size_t... Is>
struct gen_seq : gen_seq<N-1, N-1, Is...>{};
template<std::size_t... Is>
struct gen_seq<0, Is...> : seq<Is...>{};
template<class T,std::size_t N>
struct sin_coeffs{
using array_type = std::array<T,N>;
constexpr static inline T coeff(std::size_t n){
return power(-1, n-1) * inverse((T)factorial((2 * n)-1));
}
template<std::size_t...NS>
constexpr static array_type _coeffs(seq<NS...>){
return {{coeff(NS)...}};
}
constexpr static array_type coeffs=_coeffs(gen_seq<N>{});
};
}
template<class T,std::size_t N = max_factorial, class dcy = std::decay_t<T>>
constexpr std::enable_if_t<std::is_floating_point<dcy>::value,dcy> sin(T x) noexcept{
constexpr std::array<dcy,N>& coeffs = detail::sin_coeffs<dcy,N>::coeffs;
const dcy x_2 = x*x;
dcy pow = x;
dcy result = 0;
for(std::size_t i=0;i<N;++i){
result += coeffs[i] * pow;
pow*=x_2;
}
return result;
}
}
示例主要:
int main()
{
constexpr double d = math::sin(0.0);
}
该代码被设计为constexpr sin函数,它使用constexpr数组来保存系数以进行所需的计算。
未列出的所有功能都存在于单独的标题中,编译没有问题。
我正在尝试使用&#34;标记&#34;使用基于this answer to another question的constexpr函数填充数组的技巧。
我正在使用标志
编译GCC 5.3.1- std = c ++ 1z -pthread -g -O3 -MMD -MP -Wall -pedantic
编译器不会向我的代码发出错误,但在编译时会停止。
我让编译运行了几分钟,但它没有做任何事情。
我已经测试了代码中使用的所有函数,它们都可以独立于本节进行编译。
int main()
{
math::detail::sin_coeffs<double,20>::coeffs[0];
}
此代码片段还重现了这个问题,这个问题让我相信它与sin函数本身无关,而与sin_coeffs结构有关。
编辑: 以下是其他要求的功能:
#include <type_traits>
namespace math{
template<class T,class dcy = std::decay_t<T>>
constexpr inline std::enable_if_t<std::is_floating_point<T>::value,dcy> inverse(T value){
return (value == 0) ? 0.0 : 1.0 / value;
}
template <class T>
constexpr inline std::decay_t<T> sign(T value) {
return value < 0 ? -1 : 1;
}
template <typename T>
constexpr inline std::decay_t<T> abs(T value) {
return value * sign(value);
}
template<class T>
constexpr inline std::decay_t<T> power(T const& base, std::size_t const& pow){
if(pow==0){return 1;}
else if(pow == 1){return base;}
else{
T result = base;
for(std::size_t i=1;i<pow;++i){
result*=base;
}
return result;
}
}
constexpr std::intmax_t factorial(std::intmax_t const& n){
if(n==0){return 1;}
std::intmax_t result = n;
for(std::intmax_t i=n-1;i>0;--i){
result *=i;
}
return result;
}
constexpr static std::size_t max_factorial = 20;//replace with calculated version later
}
答案 0 :(得分:6)
const
限定符:constexpr const std::array<dcy,N>& coeffs = /* ... */ ;
^^^^^
gen_seq
会生成从0
到N - 1
的值,但对于您的coeff
功能,您需要1
到N
的值。您可以通过以下方式解决此问题:1
到N
的值(有关详细说明,请参阅此答案的底部):template<std::size_t N, std::size_t... Is>
struct gen_seq: gen_seq<N-1, N, Is...> {};
// ^--- N instead of N - 1
template<std::size_t...NS>
constexpr static array_type _coeffs(seq<NS...>){
return {{coeff(NS + 1)...}};
// ^^^^
}
power
来计算-1 ** n
,请使用三元条件:constexpr static inline T coeff(std::size_t n){
return (n%2 ? 1 : -1) * inverse((T)factorial((2 * n)-1));
}
有了这个,我可以计算系数:
auto arr = math::detail::sin_coeffs<double, 10>::coeffs;
for (auto x: arr) {
std::cout << x << " ";
}
输出:
1 -0.166667 0.00833333 -0.000198413 2.75573e-06 -2.50521e-08 1.6059e-10 -7.64716e-13 ...
据我所知,这些是正确的系数(1
,-1/3!
,1/5!
,...)。请注意,我必须使用N = 10
,否则我会溢出std::intmax_t
(在我的架构上) - 如果您使用std::intmax_t
溢出factorial
,Clang会在编译时发出警告(一个不错的编译器!)。
通过以上两个修改,您的代码工作正常(max_factorial
值除外,但您可以根据需要进行调整)。
请参阅rextester
上的代码:http://rextester.com/CRR35028
您的gen_seq<N>
生成了从0
到N - 1
的序列,因此您正在呼叫coeff(0)
,呼叫power(-1, static_cast<size_t>(0) - 1)
,实际上是{{1} (在我的架构上),无法编译。在power(-1, 18446744073709551615)
&#34;修复&#34;中添加一个简单的案例汇编(显示这是一个问题,但没有解决真正的问题):
power
此外,您的template<class T>
constexpr inline std::decay_t<T> power(T const& base, std::size_t const& pow) {
if (pow == static_cast<size_t>(0) - 1) { // Stupid test
return 1;
}
/* ... */
}
值也可能太大了。在更正max_factorial
之后,我无法编译power
(我可能有32位max_factorial > 11
所以你可能会超过这个,但我认为20在所有情况下都太大了)。
此外,对于未来的问题,std::intmax_t
似乎提供了更好的信息,说明它为什么不编译:
clang
正在进行(几乎)无限循环。power
溢出factorial
的返回值发出警告。intmax_t
技巧如何运作?您的gen_seq
结构基本上是seq
的结构持有者(因为您无法将其直接存储在变量中)。
std::size_t...
是&#34;递归&#34;使用gen_seq
构建gen_seq<N, ...>
的结构。它是如何工作的:
gen_seq<N - 1, ...>
正如您所看到的,由于您继承了gen_seq<3>: gen_seq<2, 2>
gen_seq<2, 2>: gen_seq<1, 1, 2>
gen_seq<1, 1, 2>: gen_seq<0, 0, 1, 2>
gen_seq<0, 0, 1, 2>: seq<0, 1, 2> // Specialized case
,因此gen_seq<N - 1, N - 1, ...>
的最后一个继承具有您不想要的seq
值。因为&#34;发送&#34;至0
是可变seq
,您希望避免使用std::size_t...
,因此您更改为0
:
gen_seq<N - 1, N, ...>
现在,当您调用gen_seq<3>: gen_seq<2, 3>
gen_seq<2, 3>: gen_seq<1, 2, 3>
gen_seq<1, 2, 3>: gen_seq<0, 1, 2, 3>
gen_seq<0, 1, 2, 3>: seq<1, 2, 3> // Specialized case
时,您允许编译器推导出_coeffs(gen_seq<N>{})
的模板参数NS
。在此基础上,您可以使用_coeffs
中的NS
来pack expansion:
_coeffs