I have a problem with above code. my program terminates as soon as the value of d gets 1.000000. But it should terminate only if d < 1.0Code:if (d < 1.0) { overlap=1; break; }
I have a problem with above code. my program terminates as soon as the value of d gets 1.000000. But it should terminate only if d < 1.0Code:if (d < 1.0) { overlap=1; break; }
You need to show us a bit more code. Is the above code inside a loop of some sort?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Code:overlap=0; for (k=0; k<N-1; k++) { for (j=k+1; j<N; j++) { dx = fabs(x[k] - x[j]); if (dx > L/2.0){ dx -=L;} dy = fabs(y[k] - y[j]); if (dy > L/2.0){ dy -=L;} dz = fabs(z[k] - z[j]); if (dz > L/2.0){ dz -=L;} d = dx*dx + dy*dy + dz*dz; if (d < 1.0) { overlap=1; break; } } if (overlap == 1) break; } if (overlap == 1) { printf("It's hard to fit monomers inside the box\n"); exit(-1); }
Well, if the variables in question are floats, you might be running into floating-point inaccuracies. Let's say that 1.0 can be represented as a double, but 0.999... is the closest a float can get. You have this expression:
1.0 is a double constant (instead of 1.0f, which would be a float one), so d is promoted to a double. Perhaps 0.999... can also be represented as a double, so that's what you get. 0.999... is less than 1.0, and there you have it.Code:if (d < 1.0)
[edit] No, I'm probably wrong.
Assuming your system is reasonably similar to mine, of course. [/edit]Code:$ cat one.c #include <stdio.h> int main() { printf("%i\n", 1.0f < 1.0); return 0; } $ gcc one.c -o one && ./one 0 $
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
How do you know that d==1? Do you print the value inside the above code or afterwards?
yes i print it.
Where do you print the value?
What happens when you change this
to this:Code:printf("It's hard to fit monomers inside the box\n");
?Code:printf("%f %i\n", d, d < 1.0);
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I did :
float d ;
if (d< 1.0f)
it worked