Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-索引超出范围 我尝试将C++类转换成C语言,在这个过程中学习C++。我以前从未遇到过向量,我的理解是这就像C#中的列表函数。在类的转换过程中,我使用List futures_price=newlist(Convert.ToInt32(no_steps)+1)重新编写了代码;。我一运行代码,就得到一个“索引超出范围”错误_C#_C++_List_Vector_Compiler Errors - Fatal编程技术网

C#-索引超出范围 我尝试将C++类转换成C语言,在这个过程中学习C++。我以前从未遇到过向量,我的理解是这就像C#中的列表函数。在类的转换过程中,我使用List futures_price=newlist(Convert.ToInt32(no_steps)+1)重新编写了代码;。我一运行代码,就得到一个“索引超出范围”错误

C#-索引超出范围 我尝试将C++类转换成C语言,在这个过程中学习C++。我以前从未遇到过向量,我的理解是这就像C#中的列表函数。在类的转换过程中,我使用List futures_price=newlist(Convert.ToInt32(no_steps)+1)重新编写了代码;。我一运行代码,就得到一个“索引超出范围”错误,c#,c++,list,vector,compiler-errors,C#,C++,List,Vector,Compiler Errors,环顾一下SOF,我认为问题在于参数超出了与此相关的索引范围,但我看不到使用下面的代码解决此问题的简单解决方案 特别是,这是触发错误的一行:期货价格[0]=现货价格*Math.Pow(d,无步骤) 以下是完整代码: public double futures_option_price_call_american_binomial(double spot_price, double option_strike, double r, double sigma, double time, double

环顾一下SOF,我认为问题在于参数超出了与此相关的索引范围,但我看不到使用下面的代码解决此问题的简单解决方案

特别是,这是触发错误的一行:期货价格[0]=现货价格*Math.Pow(d,无步骤)

以下是完整代码:

public double futures_option_price_call_american_binomial(double spot_price, double option_strike, double r, double sigma, double time, double no_steps)
        {

           //double spot_price, // price futures contract
           //double option_strike, // exercise price
           //double r, // interest rate
           //double sigma, // volatility
           //double time, // time to maturity
           //int no_steps

            List<double> futures_prices = new List<double>(Convert.ToInt32(no_steps) + 1);
               //(no_steps+1);
           //double call_values = (no_steps+1);
            List<double> call_values = new List<double>(Convert.ToInt32(no_steps) + 1);

           double t_delta = time/no_steps;
           double Rinv = Math.Exp(-r*(t_delta));
           double u = Math.Exp(sigma * Math.Sqrt(t_delta));
           double d = 1.0/u;
           double uu= u*u;
           double pUp   = (1-d)/(u-d);   // note how probability is calculated
           double pDown = 1.0 - pUp;

           futures_prices[0] = spot_price * Math.Pow(d, no_steps);

            int i;

            for (i=1; i<=no_steps; ++i) futures_prices[i] = uu*futures_prices[i-1]; // terminal tree nodes
            for (i=0; i<=no_steps; ++i) call_values[i] = Math.Max(0.0, (futures_prices[i]-option_strike));
            for (int step = Convert.ToInt32(no_steps) - 1; step >= 0; --step)
            {
                for (i = 0; i <= step; ++i)
                {
                    futures_prices[i] = d * futures_prices[i + 1];
                    call_values[i] = (pDown * call_values[i] + pUp * call_values[i + 1]) * Rinv;
                    call_values[i] = Math.Max(call_values[i], futures_prices[i] - option_strike); // check for exercise
                };
            };

            return call_values[0];
        }
公开双期货、期权、价格、看涨、美国二项式(双现货、双期权、双履约、双r、双西格玛、双时间、双无步骤)
{
//双现货价格,//价格期货合约
//双重选择权的行使价格
//双r,//利率
//双西格玛,//波动性
//双倍时间,//到期时间
//int no_步骤
列表期货价格=新列表(转换为32(无步骤)+1);
//(无步骤+1);
//双调用_值=(无_步骤+1);
列表调用值=新列表(转换为32(无步骤)+1);
双t_δ=时间/无步数;
double Rinv=Math.Exp(-r*(t_delta));
double u=Math.Exp(sigma*Math.Sqrt(t_delta));
双d=1.0/u;
双uu=u*u;
double pUp=(1-d)/(u-d);//注意概率是如何计算的
双pDown=1.0-幼犬;
期货价格[0]=现货价格*Math.Pow(d,无步骤);
int i;
对于(i=1;iA
List
开始时为空,直到添加项为止。(传递构造函数参数只是设置容量,防止代价高昂的大小调整)

在添加()之前,您无法访问
[0]

要按原样使用它,请改用数组。

A
列表开始时是空的,直到添加项为止。(传递构造函数参数只是设置容量,防止代价高昂的大小调整)

在添加()之前,您无法访问
[0]


要按您的方式使用它,请改用数组。

正如SLaks所说,在这种情况下最好使用数组。C#列表中填充了Add方法,并且通过Remove方法删除了值……这将更加复杂,而且内存/性能也会非常昂贵,因为您也在替换值

public Double FuturesOptionPriceCallAmericanBinomial(Double spotPrice, Double optionStrike, Double r, Double sigma, Double time, Double steps)
{
    // Avoid calling Convert multiple times as it can be quite performance expensive.
    Int32 stepsInteger = Convert.ToInt32(steps);

    Double[] futurePrices = new Double[(stepsInteger + 1)];
    Double[] callValues = new Double[(stepsInteger + 1)];

    Double tDelta = time / steps;
    Double rInv = Math.Exp(-r * (tDelta));
    Double u = Math.Exp(sigma * Math.Sqrt(tDelta));
    Double d = 1.0 / u;
    Double uu = u * u;
    Double pUp = (1 - d) / (u - d);
    Double pDown = 1.0 - pUp;

    futurePrices[0] = spotPrice * Math.Pow(d, steps);

    for (Int32 i = 1; i <= steps; ++i)
        futurePrices[i] = uu * futurePrices[(i - 1)];

    for (Int32 i = 0; i <= steps; ++i)
        callValues[i] = Math.Max(0.0, (futurePrices[i] - optionStrike));

    for (Int32 step = stepsInteger - 1; step >= 0; --step)
    {
        for (Int32 i = 0; i <= step; ++i)
        {
            futurePrices[i] = d * futurePrices[(i + 1)];
            callValues[i] = ((pDown * callValues[i]) + (pUp * callValues[i + 1])) * rInv;
            callValues[i] = Math.Max(callValues[i], (futurePrices[i] - option_strike));
        }
    }

    return callValues[0];
}
public Double futuresoptionprice callAmerican二项式(双点价格、双选项突击、双r、双西格玛、双时间、双步骤)
{
//避免多次调用Convert,因为它可能会导致相当高的性能开销。
Int32 stepsInteger=转换为Int32(步骤);
Double[]未来价格=新的Double[(stepsInteger+1)];
Double[]调用值=新的Double[(stepsInteger+1)];
双tDelta=时间/步长;
Double rInv=Math.Exp(-r*(tDelta));
Double u=数学表达式(sigma*Math.Sqrt(tDelta));
双d=1.0/u;
双uu=u*u;
双幼崽=(1-d)/(u-d);
双pDown=1.0-幼犬;
未来价格[0]=spotPrice*Math.Pow(d,steps);

对于(Int32 i=1;i,正如SLaks所说,在这种情况下最好使用数组。C#列表中填充Add方法,并通过Remove方法删除值……这将更加复杂,而且内存/性能成本也会很高,因为您也在替换值

public Double FuturesOptionPriceCallAmericanBinomial(Double spotPrice, Double optionStrike, Double r, Double sigma, Double time, Double steps)
{
    // Avoid calling Convert multiple times as it can be quite performance expensive.
    Int32 stepsInteger = Convert.ToInt32(steps);

    Double[] futurePrices = new Double[(stepsInteger + 1)];
    Double[] callValues = new Double[(stepsInteger + 1)];

    Double tDelta = time / steps;
    Double rInv = Math.Exp(-r * (tDelta));
    Double u = Math.Exp(sigma * Math.Sqrt(tDelta));
    Double d = 1.0 / u;
    Double uu = u * u;
    Double pUp = (1 - d) / (u - d);
    Double pDown = 1.0 - pUp;

    futurePrices[0] = spotPrice * Math.Pow(d, steps);

    for (Int32 i = 1; i <= steps; ++i)
        futurePrices[i] = uu * futurePrices[(i - 1)];

    for (Int32 i = 0; i <= steps; ++i)
        callValues[i] = Math.Max(0.0, (futurePrices[i] - optionStrike));

    for (Int32 step = stepsInteger - 1; step >= 0; --step)
    {
        for (Int32 i = 0; i <= step; ++i)
        {
            futurePrices[i] = d * futurePrices[(i + 1)];
            callValues[i] = ((pDown * callValues[i]) + (pUp * callValues[i + 1])) * rInv;
            callValues[i] = Math.Max(callValues[i], (futurePrices[i] - option_strike));
        }
    }

    return callValues[0];
}
public Double futuresoptionprice callAmerican二项式(双点价格、双选项突击、双r、双西格玛、双时间、双步骤)
{
//避免多次调用Convert,因为它可能会导致相当高的性能开销。
Int32 stepsInteger=转换为Int32(步骤);
Double[]未来价格=新的Double[(stepsInteger+1)];
Double[]调用值=新的Double[(stepsInteger+1)];
双tDelta=时间/步长;
Double rInv=Math.Exp(-r*(tDelta));
Double u=数学表达式(sigma*Math.Sqrt(tDelta));
双d=1.0/u;
双uu=u*u;
双幼崽=(1-d)/(u-d);
双pDown=1.0-幼犬;
未来价格[0]=spotPrice*Math.Pow(d,steps);

对于(It32 i=1;i为什么是代码>NoYeXixs/Cuth.2……什么行产生错误?)Salkes的答案是正确的,这只是一个注释,建议您编写C代码,如C++,而不是C++。通过查看其他源代码,你可以得到一个好主意。为什么<代码> NOSY步骤一个双……什么行产生错误?@ Salkes的答案是正确的,这只是一个注释,建议你写C代码,比如C++,而不是C++。,CamelCase方法名称和属性等。通过查看其他源代码,您可以了解到一个好的想法。啊,所以我真的应该重新编写此函数以正确运行。我发现我缺少一些关键部分。啊,所以我真的应该重新编写此函数以正确运行。我发现我缺少一些关键部分。哇!谢谢。这对查看非常有帮助它是如何建造的。我在上面遗漏了很多。哇!谢谢。这对了解它是如何建造的非常有帮助。我在上面遗漏了很多。