I have this equation
double x = ((newCount/allCount)/.8)*5.0;
newCount is a double with value 0
allCount is a double with value 0
the result of x is -nan(0x8000000000000)
why this happens and how to check this value in objective c to assign default value for it
The problem is that the denominator (
allCount) is 0; dividing by zero is not allowed and the answer is not a number. The simplest thing you could do is to test for that before doing the division:There are more complicated ways using C’s floating point environment and testing for the
FE_DIVBYZEROexception, but while that’s standard it’s rarely used and therefore potentially more difficult for a later reader of the code to comprehend.