int变量初始化函数失败

时间:2014-04-07 05:53:45

标签: c++ initialization

我有一个函数想要返回一个向量,在我的函数内部我设置了一个int计数器(调料),每次循环后它将增加1。 当我调试我的函数时,我看到我的计数器没有初始化,相应的前29个计算(调料<30)我得到0值。

请看我的代码,谢谢!

vector<float> 
RichardRollPrepayment::ComputingCPR(
  float maturity, 
  float settledTime, 
  float frequencyPreYear,
  float couponRate, 
  vector<float> mortgageRate
)  
{
  vector<float> cprVector(ceil((maturity-settledTime) * frequencyPreYear));
  float seasonality[] = {
    .94, .76, .73, .96, .98, .92, .99, 1.1, 1.18, 1.21, 1.23, .97
  };
  int seasoning = int(settledTime) * 12;

  for (int i = 0; i < cprVector.size(); i++) {

    if (seasoning < 30) {
      cprVector[i] = 
      (.2406 - .1389 * atan(5.952*(1.089 - couponRate/mortgageRate[i])))
        * (seasoning/30) * seasonality[seasoning%12];

      seasoning++;
    }

    else {
      cprVector[i] =
      (.2406 - .1389 * atan(5.952*(1.089 - couponRate/mortgageRate[i])))
        * 1 * seasonality[seasoning%12];

      seasoning++;
    }
  }

  return cprVector;
}

1 个答案:

答案 0 :(得分:1)

seasoning小于30时,您将与seasoning/30相乘,因为seasoning是一个整数,所以该值为0.

将其替换为seasoning / 30.0f