anon_38293
anon_38293

Reputation: 9

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

I'm trying to print the totalprofit into the output file, but the totalprofit comes out to be nan. The profit variable is fine, and its not turning toward infinity or dividing by 0. I'm simply adding the profit values into a totalprofit. Not sure why its showing me 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();

Upvotes: 0

Views: 131

Answers (1)

Michael Chourdakis
Michael Chourdakis

Reputation: 11178

  1. Always initialize your variables

double profit = 0, totalprofit = 0; A good compiler would warn on that.

  1. Declare the variables where you need it, not on top. profit does not need an early declaration.

Upvotes: 2

Related Questions