3
double SampleInterval = (PopulationValue - valueOfSignItems) / (SampleSize - noOfSignItems);

如果我的除数 = 0,则 sampleInterval 将 bcom 无穷大,如果除数和除数都 = 0,它将是 = NaN

当 SampleInterval = infinity 和 SampleInterval = NaN 时,我需要执行我的代码。怎么可能..?? 谁能告诉我如何将十进制值与无穷大或 NaN 进行比较?

4

2 回答 2

6

您必须使用 Double.IsInfinity() 和 Double.IsNaN() 方法。

if (Double.IsInfinity(SampleInterval))
{
  //TODO
}
if (Double.IsNaN(SampleInterval))
{
  //TODO
}

不要直接与 Double.NaN 比较,它总是返回 false。

于 2010-10-28T10:13:49.297 回答
0

Upping a old topic to add another solution (not really good looking but still worth the try if not working for -oo)

You can generate an -Infinity double by yourself, and use it as a comparison

double minusInfinity = -1.0/0.0;
if (yourDouble==minusInfinity ) {
    // yourDouble is equal to -oo
}
else {
    // yourDouble is not equal to -oo
}

You can do the same for +oo or NaN by using comparison :

double nan = 0.0/0.0;
double infinity = 1.0/0.0;
于 2015-04-23T15:53:43.870 回答