I have the following function, and I tried to avoid negative values by including the if statement, but it didn’t help.
…suggestions on how I might fix this…
double G(double S, double X, double r, double div, double k, double T)
{
double g=0;
g=Phi(d1(S,X,r,div,k,T))/(exp(-div*T)*S*k*sqrt(T));
if((isnan)(g) || (isinf)(g) || (g<0)) g=0;
return g;
}
You have the right idea, but the syntax is a little bit off. Try this: