为什么我的双变量通过添加 c++ 显示 nan?

Why is my double variable showing nan through adding c++?

本文关键字:c++ 显示 nan 添加 我的 变量 为什么      更新时间:2023-10-16

我正在尝试将总利润打印到输出文件中,但总利润显示为nan。利润变量很好,它不会转向无穷大或除以 0。我只是将利润值添加到总利润中。不知道为什么它向我展示nan。

double profit, totalprofit; 
int intsize = 500;
for(int j = 0; j < 372500; j++)
{

infile>>data;
tradethis.push_back(data);
if(j%intsize==0)
{
for(int i=1; i<intsize; i++)
{
profit=0;
profit=eurusd.position(i-1)*((tradethis[i]-tradethis[i-1])); //position returns 1000
totalprofit = totalprofit + profit;
outdata<<totalprofit<<endl;
cout<<totalprofit<<endl;
//pnl.push_back(totalprofit);
if(profit>0)
{
c+=1.0;
t+=1.0;
}
else if(profit<0)
{
w+=1.0;
t+=1.0;
}
}
tradethis.clear();
}
}
outdata.close();
  1. 始终初始化变量

double profit = 0, totalprofit = 0;一个好的编译器会警告这一点。

  1. 需要的地方声明变量,而不是在上面。profit不需要早日宣布。