How can we compare whether the result of the arithmetic operation NaN or infinity.?

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

if my divisor = 0, sampleInterval wil bcom is infinite, and it will be = NaN if both dividends and divisor are equal = 0

I need to do my code when SampleInterval = infinity and in a different context when SampleInterval = NaN. How is this possible .. ?? can someone call me, how can I compare the desmin value with infinity or with NaN ??

+3
source share
2 answers

You must use the Double.IsInfinity () and Double.IsNaN () methods.

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

Do not compare directly with Double.NaN, it will always return false.

+6
source

, ( , , -oo)

-Infinity double

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

+ oo NaN, :

double nan = 0.0/0.0;
double infinity = 1.0/0.0;
0

Source: https://habr.com/ru/post/1771900/


All Articles