I tried with you an expression (since I do not know the origin of c1 , c2 , c12 and N, I hard-coded their values). Thus, the hard-set values โโare as follows:
double c1 = 0.1; double c2 = 0.2; double c12 = 0.3; double N = 0.4;
And I have a probability = NaN .
As mentioned above, pay attention to the input. The first problematic expressions (you may get an overflow here due to the separation of extra or large numbers):
double p = c2 / N; double p1 = c12 / c1; double p2 = (c2 - c12) / (N - c1);
Then you calculate the logarithms. In fact, in my case (with the hard-coded values โโindicated above) I got NaN in the expression Math.log(1 - p1) (since it is trying to calculate the decimal logarithm of a negative number - p1 <1, when c1> c2 is a very likely case).
Generally speaking, you can get not only overflow (in extreme cases), but also NaN (even for "reasonable" input).
The suggestion is to split the long expression into small Java expressions. And check every value that could lead to NaN or overflow before calculating and throwing exceptions manually. This will help locate the cause of the problem when you receive invalid input.
source share