我收到以下错误:
Implied Vol.exe中0x009f1559处的未处理异常:0xC0000005:访问冲突写入位置0x00110ee4。
#include "EurCall.h"
#Include <cmath>
double N(double x){
double gamma=0.2316419;
double a1=0.319381530;
double a2=-0.356563782;
double a3=1.781477937;
double a4=-1.821255978;
double a5=1.330274429;
double pi=4.0*atan(1.0);
double k =1.0/(1.0+gamma*x);
if (x>=0.0){
return 1.0-((((a5*k+a4)*k+a3)*k+a2)*k+a1)*k*exp(-x*x/2.0)/sqrt(2.0*pi);
}
else return 1.0-N(-x);
}
double EurCall::d_plus(double S0, double sigma, double r){
return (log(S0/K)+(r+0.5*pow(sigma,2.0))*T)/(sigma*sqrt(T));
}
double EurCall::d_minus(double S0, double sigma, double r){
return d_plus(S0,sigma,r)-sigma*sqrt(T);
}
double EurCall::PriceByBSFormula(double S0, double sigma, double r){
return S0*N(d_plus(S0,sigma,r))-K*exp(-r*T)*N(d_minus(S0,sigma,r));
}
double EurCall::VegaByBSFormula(double S0,double sigma, double r){
double pi=4.0*atan(1.0);
return S0*exp(-d_plus(S0,sigma,r)*d_plus(S0,sigma,r)/2)*sqrt(T)/sqrt(2.0*pi);
}
从调试器开始,错误似乎就像代码尝试在第一个大括号中输入函数N一样。
好的,这里是调用代码:
#include "Solver03.h"
#include "EurCall.h"
#include <iostream>
using namespace std;
class Intermediary: public EurCall
{
private:
double S0,r;
public:
Intermediary(double S0_, double r_, double T_, double K_):EurCall(T_,K_){S0=S0; r=r_;}
double Value(double sigma)
{
return PriceByBSFormula(S0,sigma,r);
}
double Deriv(double sigma)
{
return VegaByBSFormula(S0,sigma,r);
}
};
int main()
{
double S0=100.0;
double r=0.1;
double T=1.0;
double K=100.0;
Intermediary Call(S0,r,T,K);
double Acc=0.001;
double LEnd=0.01, REnd=1.0;
double Tgt=12.56;
cout << "Implied vol by bisect: "
<< SolveByBisect(&Call,Tgt,LEnd,REnd,Acc)
<< endl;
double Guess=0.23;
cout << "Implied vol by Newton-Raphson: "
<< SolveByNR(&Call,Tgt,Guess,Acc)
<< endl;
return 0;
}
这是堆栈中的最后一个条目
暗示Vol.exe!N(双x = -1。#IND000000000000)第5行+ 0x9字节C ++
虽然你需要多少条目?