I'm making a set of functions in C for simulate a microcanonial ensamble (a periodic-bounded box with N particles with fixed Energy and Volume). The point is that one of my main functions is to get the distance between 2 particles, and sometimes it returns 0 even if the particles are not that close.
The function is
// Determinar la mínima distancia dadas dos coordenadasy una longitud de caja periodica
float pdist(float x1, float y1, float x2, float y2, float a) {
float Dx = abs(x2-x1);
float Dy = abs(y2-y1);
if(Dx >a/2) Dx = a-Dx;
if(Dy >a/2) Dy = a-Dy;
return sqrt(pow(Dx,2)+pow(Dy,2));
}
If you want to see all the code, you can see it in https://gitlab.com/jarr.tecn/statistical_mechanics
in the file box.h and one example in ejemplo.h
The problem comes from
absfunction which will return an integer:Gives: