Thread: Debugging driving me crazy

  1. #1
    ---
    Join Date
    May 2004
    Posts
    1,379

    Debugging driving me crazy

    This section of code just doesn't seem to work for me. It skips the if block (in bold) when I know the statement is true.

    Code:
    double convert_to_julian(int *p_year, int *p_mon, int *p_day){
      int a = 0, b = 0, c = 0, d = 0;
    
      if(*p_mon == 1 || *p_mon == 2){
        *p_year -= 1;
        *p_mon += 12;
      }
       // At this point I know that year is 1994 month is 2 and day is 1
      if((*p_year >= 1582) && (*p_mon >= OCT) && (*p_day >= 15)){
        a = (*p_year / 100);
        b = (2 - a) + (a / 4);
      }
      else{
        b = 0;
      }
    
      .
      .
      .
    But it does work if I remove the (*p_day >= 15) condition.
    There must be something I'm forgetting



    [edit]
    omg I just realised my problem. sorry for wasting time.
    the answer was this
    Code:
    if((*p_year > 1582) || (*p_year == 1582) && (*p_mon >= OCT) && (*p_day >= 15)){
    Last edited by sand_man; 07-06-2005 at 06:52 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The logic of those two if statements are identical...

    gg

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Furthermore, I would rather expect that block of statements to be skipped if month is 2 (assuming OCT is defined as 10) and/or day is 1. When you shake mathematics really hard, it turns out that 2<10 and 1<15.
    Away.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by Codeplug
    The logic of those two if statements are identical...

    gg
    No they aren't.
    nevermind anyway, it's working.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> nevermind anyway
    Well, I can't "nevermind" misinformation - the logic is identical.

    Work it out for yourself: Operator Precedence and Associativity

    gg

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Codeplug
    >> nevermind anyway
    Well, I can't "nevermind" misinformation - the logic is identical.

    Work it out for yourself: Operator Precedence and Associativity

    gg
    Huh?

    Let's try them for today:

    Code:
    #include <stdio.h>
    
    int main()
    {
      int mon = 7;
      int day = 7;
      int year = 2005;
    
      printf("For mon = %d, day = %d, year = %d\n\n", mon, day, year);
      /* algorithm 1 */
      printf("Algorithm 1 says ");
      if((year >= 1582) && (mon >= 10) && (day >= 15)){
        printf("true\n");
      }
      else {
        printf("false\n");
      }
    
      /* algorithm 2 */
      printf("Algorithm 2 says ");
      if((year > 1582) || (year == 1582) && (mon >= 10) && (day >= 15)){
        printf("true\n");
      }
      else {
        printf("false\n");
      }
      return 0;
    }
    [edit]
    [edit]
    Previously edited edit remarks removed.
    [/edit]
    [/edit]

    Regards,

    Dave
    Last edited by Dave Evans; 07-07-2005 at 04:39 PM.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Overconfidence is a bastard.

    I'm the mis-informer since I didn't test for months < 10 or days < 15, and assumed right-to-left associativity on && and ||.

    gg

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Which is another good reason to use explicit parathensis so people like me don't get confused:
    Code:
    if ((year > 1582) || 
        ((year == 1582) && (mon >= 10) && (day >= 15)))
    gg

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    And shouldn't the check be something like this?
    Code:
    int bar(int year, int month, int day)
    {
       return year > 1582 || (year == 1582 && (month > 10 || (month == 10 && day >= 15)));
    }
    Otherwise it would skip the first 14 days of November and December in 1582.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Dave_Sinkula
    And shouldn't the check be something like this?
    Right

    (But note that I already knew that. I was just pointing out that the two expressions in prior posts had different values. That's my story and I'm sticking to it. --- But I had added some incorrect remarks that I have now removed.)

    Regards,

    Dave
    Last edited by Dave Evans; 07-07-2005 at 04:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. This program is driving my crazy!
    By zyphirr in forum C Programming
    Replies: 8
    Last Post: 12-05-2008, 05:36 PM
  2. This is driving me crazy! 256 colors palette
    By manugarciac in forum C++ Programming
    Replies: 0
    Last Post: 04-29-2007, 05:36 PM
  3. this is driving me crazy
    By pinko_liberal in forum C Programming
    Replies: 12
    Last Post: 06-05-2004, 08:03 AM
  4. Getline is driving me crazy
    By Vanished in forum C++ Programming
    Replies: 2
    Last Post: 01-23-2003, 12:27 AM
  5. IE 6 and Banner Ads is driving me crazy
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-09-2002, 02:18 AM