Thread: Problem with ">", "<" in a for loop

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    26

    Problem with ">", "<" in a for loop

    This is embarrassing, but i have no idea of what is happening.

    I'm using numerical integration to solve a ODE, and I asked my program where the final point is. I devided the phase space in little cells... and because of the nature of my system it always finishes in (0,1) or in (0,-1).

    Here is my "question code":

    Code:
    if((i-DIM/2)/25.0<salidax[n][k]<(i+1-DIM/2)/25.0)
    	    if((j-DIM/2)/25.0<saliday[n][k]<(j+1-DIM/2)/25.0) {
    	      printf("&#37;f %f %f %f %f %f \t", (i-DIM/2)/25.0,salidax[n][k],(i+1-DIM/2)/25.0,(j-DIM/2)/25.0,saliday[n][k],(j+1-DIM/2)/25.0);
    }
    It shoud work. I also tried the "&&" operator insted of calling "if" twice, but the same result:

    Code:
     0.280000 -0.000000 0.320000 0.040000 -1.000000 0.080000
     0.280000 -0.000000 0.320000 0.080000 -1.000000 0.120000 	
     0.280000 -0.000000 0.320000 0.120000 -1.000000 0.160000 
     0.280000 -0.000000 0.320000 0.160000 -1.000000 0.200000 	
     0.280000 -0.000000 0.320000 0.200000 -1.000000 0.240000 
     0.280000 -0.000000 0.320000 0.240000 -1.000000 0.280000
     0.280000 -0.000000 0.320000 0.280000 -1.000000 0.320000
    &#191;Is something wrong in my "question" code?

    Thanks a lot!
    Last edited by Isolda_; 09-11-2007 at 12:27 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It looks very complex. How about pre-calculating some of those values into a set of local variables.

    --
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The expression of form "a < x < b" does not do what you think it does. It doesn't check that x is between a and b. First, it checks if a < x. If true, this part of the expression evaluates to 1. Then, it checks 1 < b. Or, if a >= x, then a < x == 0 and it compares 0 < b.

    You want to use: a < x && x < b.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    26
    &#191;Do you say, instead of use the limits
    Code:
    (j-DIM/2)/25.0
    , etc., asing each of this limits to a local variable and then ask?

    Code:
     
    inf_x=(i-DIMENSION/2)/25.0;
    sup_x=(i+1-DIMENSION/2)/25.0;
    inf_y=(j-DIMENSION/2)/25.0;
    sup_y=(j+1-DIMENSION/2)/25.0;
    
      if(inf_x<salidax[n][k]<sup_x)
        if(inf_y<saliday[n][k]<sup_y) {
          printf(fp,"&#37;f %f %f %f %f %f \t", inf_x,salidax[n][k],sup_x,inf_y,saliday[n][k],sup_y);}
    It doesn't work



    Code:
    0.360000 -0.000000 0.400000 0.040000 -1.000000 0.080000
    EDITION:

    O_O

    brewbuck, I did'nt see your answer. I never would imagine "a < x < b" doesn`t do what I think. Thanks a lot!

    The result is this:

    Code:
    0.000000 0.000000 0.040000 0.960000 1.000000 1.000000 	
    0.000000 0.000000 0.040000 0.960000 1.000000 1.000000 	
    0.000000 0.000000 0.040000 0.960000 1.000000 1.000000 	
    0.000000 0.000000 0.040000 0.960000 1.000000 1.000000 	
    0.000000 0.000000 0.040000 0.960000 1.000000 1.000000 	
    0.000000 0.000000 0.040000 0.960000 1.000000 1.000000 	
    0.000000 0.000000 0.040000 0.960000 1.000000 1.000000 	
    0.000000 0.000000 0.040000 0.960000 1.000000 1.000000 	
    0.000000 0.000000 0.040000 0.960000 1.000000 1.000000 	
    0.000000 0.000000 0.040000 0.960000 1.000000 1.000000...
    The problem now is i'm getting a lot of copies of the same thing; like the "i" and "j" don't evolve!, but when I put a
    Code:
    printf
    just after the declaration of local variables, it does evolve. I'm confuse.
    Last edited by Isolda_; 09-11-2007 at 12:57 PM.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    if(inf_x<salidax[n][k]<sup_x)
    What he means is that you cant do that. This statment dosn't provide you with the right result. Instead use this

    Code:
    if( inf_x < salidax[n][k] && salidax[n][k] < sup_x )
    And you need to give some white spaces in your code, to make it read better. It is very difficult to read your code.

    ssharish

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    It doesn't work
    Of course it doesn't. And for the very same reason the brewbuck mentionned before.

    Expressions of the type a < x < b don't do what you think they do, as they basically come down to (a < x) < b. Now, the expression (a < x), if true, returns 1, if not, it returns 0. So you're comparing the result of a<x to b. Now since its quite unlikely that x will always be 1 or 0, you're not gonna get the results you expect.

    What you want to do is :

    Code:
    (x > a && x < b)
    Then you'll be going somewhere.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    26
    Yes, I corrected it in my edition of the post. Thanks!

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, 11: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