# Thread: Debugging driving me crazy

1. ## 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

omg I just realised my problem. sorry for wasting time.
Code:
`if((*p_year > 1582) || (*p_year == 1582) && (*p_mon >= OCT) && (*p_day >= 15)){`

2. The logic of those two if statements are identical...

gg

3. 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.

4. Originally Posted by Codeplug
The logic of those two if statements are identical...

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

5. >> nevermind anyway
Well, I can't "nevermind" misinformation - the logic is identical.

Work it out for yourself: Operator Precedence and Associativity

gg

6. 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;
}```

Previously edited edit remarks removed.
[/edit]
[/edit]

Regards,

Dave

7. 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. 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. 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.

10. 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

Popular pages Recent additions