Reputation: 3
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
Upvotes: 0
Views: 1878
Reputation:
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:
if (allCount != 0) {
x = ((newCount/allCount)/.8)*5.0
} else {
x = defaultValue;
}
There are more complicated ways using C's floating point environment and testing for the FE_DIVBYZERO
exception, but while that's standard it's rarely used and therefore potentially more difficult for a later reader of the code to comprehend.
Upvotes: 0
Reputation: 9392
allCount is a 0, thus you just divided by 0 (which is impossible if you didn't know..) So before you assign x, just make sure that allCount is not 0 first.
if (allCount != 0)
double x = ((newCount/allCount)/.8)*5.0;
Upvotes: 0