There must be some bug in your code:
Normally, NaN came from divided by zero or negative value inside sqrt root, etc.

The program should be able to provide the line number where the error occurs.
Find out the code which produce the error.