Q:

Unusual cause of NaN in C++?  Do limits approaching zero cause NaN? 

While working on a C++ project that involves neural networks, I was hampered by NaN results.  After a great deal of tracing (trying to find the origin of NaN), I realized that its source was my sigmoid derivative function, shown below. 
double sigDer(double n){
    return 2*exp(-n) / pow(1 + exp(-n), 2);
}

Although it has a domain of all real numbers, values such as -1008.3 caused a result of NaN. According to mathematica, the correct result should be very close to zero - 2.522*10^-438.  I've averted the issue in the following manner:
double sigDer(double n){
    double res = 2*exp(-n) / pow(1 + exp(-n), 2);
    if( isnan(res) ){
        return 0;
    } else{
        return res;
    }
} 

With this simple assumption, my code functions as expected; however, I still don't understand why sigDer(<# with large magnitude>) does not return ~0.  Could someone please inform me about causes of NaN in C++ (Xcode IDE) other than dividing by zero and taking an even root of a negative?  
Thanks in advance!  I'd also like to know why signer(-1008.3) returns NaN and how to better/more efficiently trace the source of NaN values. 

A:

Well, if both numerator and denominator end up being 0, then you're doing 0/0, which is NaN.
I haven't done the calculation, but I imagine exp(-1008) is smaller than 2E-308, i.e. the smallest representable value in double. 

Could someone please inform me about causes of NaN

Wikpedia has this covered. 

A:

Denominator in every case is trying to reach infinity (which means the whole fraction is trying to reach 0).  This, however, means that you're defining somewhere division by infinity (in conjunction with limited double range). 
An explanation in this lies in the c++ exp function which returns +-HUGE_VAL if the return value cannot be represented as a double. 
Having said that, when your result cannot be contained within a double variable it will result in dividing by infinity and thus a nan
Btw if you want to operate on big numbers you can implement a class that stores numbers eg in a string and overload operators.