避免 C++ 中的负值
我有以下函数,我尝试通过包含 if 语句来避免负值,但这没有帮助。 ...关于如何解决这个问题的建议...
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您获得的值不是负值。
2.17691e-06 是 2.17691 x 1/1000000 = 0.00000217691 的指数表示。
看看求幂。
如果您不想显示幂符号,请考虑在显示/使用 g 之前设置数字的精度。
设置精度的方法之一是此处。
The value you are getting is not negative.
2.17691e-06 is the exponential representation for 2.17691 x 1/1000000 = 0.00000217691.
Have a look at exponentiation.
if you don't want to show the exponentiation sign, consider setting the precision of the digit before showing/using g.
One of the ways to set precision is here.
你的想法是对的,但语法有点不对劲。试试这个:
You have the right idea, but the syntax is a little bit off. Try this:
调用此函数时是否设置非双精度类型变量的值?
您收到任何警告吗?这应该会给你一个线索。
Are you setting the value of a non-double type variable while calling this function?
Are you getting any warnings? That should give you a clue.