Q:

C++ Unexpected NaN

I'm coding a project work right now, and my latest problem is that the output gives NaN after a while (then persistently, of course), but with my limited knowledge I can't figure out why. I read a bit about where NaN usually comes from ( 0/0, inf*0, etc.), but that doesn't seem to be the case here. While the whole code is too long and complex to post here, I already determined the part of code it seems to come from:
long double force_dipol(particle &part1, particle &part2, long double distance) {
    if ((abstand(part1.dummyd)!=0) && (abstand(part2.dummyd)!=0) { //Division checks
        long double ret;
        ret = 3.*( part1.dummyd[0]*part2.dummyd[0] + part1.dummyd[1]*part2.dummyd[1] + part1.dummyd[2]*part2.dummyd[2] ) / mypow(distance, 3);
        ret -= 9.*abstand(part1.dummyd)*abstand(part2.dummyd) / mypow(distance, 3) * (part1.dummyd[2]/abstand(part1.dummyd) * part2.dummyd[2]/abstand(part2.dummyd));
        return ret;
    }
}

For any who care, its the force term of a dipole-dipole interaction (yay, physics!). 

particle is my structure that just keeps the variables together.
dummyd is a [3]-Array that is the dipole moment, its components range from roughly -5 to 5, at max
abstand() is the vector norm, mathematically speaking, and abstand(dummyd) is between 0.6 and 2.5
distance ranges from 2.4 to 5.6
mypow() is just custom pow() for performance
the 3. and 9. are numbers XP
all variables used are long double

Additionally, if I comment out the first term of the function (the line below long double ret) it seems to stop NaN'ing, while the second term (the line after that, the longer one) seems to work.
The function above is part of a whole algorithm that gets iterated a few million times in total; somewhere along the line, the output of the position and momentum arrays (which use this function) starts to get only NaN and keeps it from there. To actually filter the output is quite a bit of work, but so far it seems it gets there from normal values (that is, in the expected range, nowhere near big numbers).
Now, I want to understand why I get NaN in this case, and how to fix it. Anyone who can explain to me gets +10 awesomeness.

A:

You don't return anything when the if condition fails. This will lead to undefined behavior if you use the "returned" value.

