double Scos [61][61][61] = {0};
double kdotr;
int ik;
int howmany [34] = {0};
auto Fs_ = initializer_list<int>({0});
copy(Fs_.begin(), Fs_.end(), Fs);
for ( size_t z=0; z<5; ++z )
{
for ( size_t y=0; y<5; ++y )
{
for ( size_t x=0; x<10; ++x )
{
for ( int k1=0; k1<=60; ++k1 )
{
for ( int k2=0; k2<=60; ++k2 )
{
for ( int k3=0; k3<=60; ++k3 )
{
int i = x+y*10+z*50;
kdotr = (double)dQ*( (k1-30)*(x_[i][0]-x) + (k2-30)*(x_[i][1]-y) + (k3-30)*(x_[i][2]-z) );
if ( isnan(kdotr) )
cout << "kdotr " << k1 << " " << k2 << " " << k3 << endl;
Scos[k1][k2][k3] += (double)cos(kdotr);
if ( isnan(Scos[k1][k2][k3]) )
cout << "Scos " << k1 << " " << k2 << " " << k3 << endl;
}
}
}
}
}
}
for ( int k1=0; k1<=60; ++k1 )
{
for ( int k2=0; k2<=60; ++k2 )
{
for ( int k3=0; k3<=60; ++k3 )
{
double k = (double)dQ*sqrt( pow((k1-30),2) + pow((k2-30),2) + pow((k3-30),2) );
ik = round(k/0.1);
Fs[ik] += Scos[k1][k2][k3];
if ( isnan(Fs[ik]) )
cout << "Fs[ik] " << k1 << " " << k2 << " " << k3 << endl;
++howmany[ik];
}
}
}
一开始只有一些声明和初始化(数组Fs
已经在其他地方声明了,还有dQ
and x_
)。
我打电话是isnan
因为代码奇怪地返回了一些 NaN。起初,我认为问题在于kdotr
走向无穷大,这将是cos
; 但是,代码永远不会在 失败Scos
,但在某些Fs[ik]
. 这对我来说没有意义,因为Fs
来自一个简单的总和(并且它被初始化为 0)。
你有没有碰巧NaN
在 C++ 中的有限项之间求和?