updated nan testing framework to check gradients of nan functions up to third order