a problem with if

This is a discussion on a problem with if within the C Programming forums, part of the General Programming Boards category; Code: if (d < 1.0) { overlap=1; break; } I have a problem with above code. my program terminates as ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    55

    a problem with if

    Code:
    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.0

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    55
    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);
    	}

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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:
    Code:
    if (d < 1.0)
    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.

    [edit] No, I'm probably wrong.
    Code:
    $ cat one.c
    #include <stdio.h>
    
    int main() {
        printf("&#37;i\n", 1.0f < 1.0);
        return 0;
    }
    $ gcc one.c -o one && ./one
    0
    $
    Assuming your system is reasonably similar to mine, of course. [/edit]
    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.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    How do you know that d==1? Do you print the value inside the above code or afterwards?

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    55
    yes i print it.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Where do you print the value?

    What happens when you change this
    Code:
    printf("It's hard to fit monomers inside the box\n");
    to this:
    Code:
    printf("&#37;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.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    55
    I did :
    float d ;
    if (d< 1.0f)
    it worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21