Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   AND and OR and parentheses (http://www.velocityreviews.com/forums/t439085-and-and-or-and-parentheses.html)

rick 08-15-2005 07:56 PM

AND and OR and parentheses
 
the following line appears in the K&R book in section 5.7:
leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
my compiler suggests parentheses "around && within ||"

given that the AND operator has higher precedence than OR, would the
correct grouping be this:
leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
?

thanks,
~rick


Peter Pichler 08-15-2005 08:09 PM

Re: AND and OR and parentheses
 
rick wrote:

> the following line appears in the K&R book in section 5.7:
> leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
> my compiler suggests parentheses "around && within ||"


I hate that warning. Some compilers can be very fussy, even if the
precedence is obvious.

> given that the AND operator has higher precedence than OR, would the
> correct grouping be this:
> leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
> ?


Yes.

> thanks,
> ~rick


Peter


Eric Sosman 08-15-2005 08:17 PM

Re: AND and OR and parentheses
 


rick wrote:
> the following line appears in the K&R book in section 5.7:
> leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
> my compiler suggests parentheses "around && within ||"
>
> given that the AND operator has higher precedence than OR, would the
> correct grouping be this:
> leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
> ?


Yes; your parentheses don't change the meaning of the
original. The compiler (presumably) issues the warning
because the people who wrote it felt that this is something
programmers often get wrong.

Oddly enough, in this particular case you *can't* get
it wrong! Consider the other possibility:

leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);

This expression gives the same value to `leap' as the first
(equivalent) pair, even though it arrives at the value by a
different route.

--
Eric.Sosman@sun.com


CBFalconer 08-15-2005 09:46 PM

Re: AND and OR and parentheses
 
Eric Sosman wrote:
> rick wrote:
>
>> the following line appears in the K&R book in section 5.7:
>> leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
>> my compiler suggests parentheses "around && within ||"
>>
>> given that the AND operator has higher precedence than OR, would
>> the correct grouping be this:
>> leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;

>
> Yes; your parentheses don't change the meaning of the
> original. The compiler (presumably) issues the warning
> because the people who wrote it felt that this is something
> programmers often get wrong.
>
> Oddly enough, in this particular case you *can't* get
> it wrong! Consider the other possibility:
>
> leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
>
> This expression gives the same value to `leap' as the first
> (equivalent) pair, even though it arrives at the value by a
> different route.


Actually that last is probably the most efficient, since 75% of the
time the value is resolved with one test, 24% with two tests, and
1% with three. I would use even more parens, as in:

leap = (year % 4 == 0) &&
((year % 100 != 0) || (year % 400 == 0));

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson



akarl 08-16-2005 11:19 PM

Re: AND and OR and parentheses
 
CBFalconer wrote:
> Eric Sosman wrote:
>
>>rick wrote:
>>
>>
>>>the following line appears in the K&R book in section 5.7:
>>> leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
>>>my compiler suggests parentheses "around && within ||"
>>>
>>>given that the AND operator has higher precedence than OR, would
>>>the correct grouping be this:
>>> leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;

>>
>> Yes; your parentheses don't change the meaning of the
>>original. The compiler (presumably) issues the warning
>>because the people who wrote it felt that this is something
>>programmers often get wrong.
>>
>> Oddly enough, in this particular case you *can't* get
>>it wrong! Consider the other possibility:
>>
>> leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
>>
>>This expression gives the same value to `leap' as the first
>>(equivalent) pair, even though it arrives at the value by a
>>different route.

>
>
> Actually that last is probably the most efficient, since 75% of the
> time the value is resolved with one test, 24% with two tests, and
> 1% with three. I would use even more parens, as in:
>
> leap = (year % 4 == 0) &&
> ((year % 100 != 0) || (year % 400 == 0));
>


Yes, I think parentheses around the "boolean" (atomic) expressions
improve readability considerably, it's also customary in mathematics.
(However, I think relying on the precedence of `and' over `or' is
perfectly okay.)

August

Malcolm 08-17-2005 05:47 AM

Re: AND and OR and parentheses
 

"akarl" <fusionfive@comhem.se> wrote
>
> Yes, I think parentheses around the "boolean" (atomic) expressions improve
> readability considerably, it's also customary in mathematics. (However, I
> think relying on the precedence of `and' over `or' is perfectly okay.)
>

The rule I use is that you can rely on your reader to know that * and / have
a higher precedence than + and -, and everything else needs parentheses.



Richard Bos 08-17-2005 06:38 AM

Re: AND and OR and parentheses
 
"Malcolm" <regniztar@btinternet.com> wrote:

> "akarl" <fusionfive@comhem.se> wrote
> >
> > Yes, I think parentheses around the "boolean" (atomic) expressions improve
> > readability considerably, it's also customary in mathematics. (However, I
> > think relying on the precedence of `and' over `or' is perfectly okay.)
> >

> The rule I use is that you can rely on your reader to know that * and / have
> a higher precedence than + and -, and everything else needs parentheses.


Are you sure?

#include <stdio.h>

int main(void)
{
int i, j;

for ((i=1); (i<10); (i++)) {
(j=(i*10+3));
(printf(("Value nr. %d is %d\n"), (i), (j)));
}
}

Richard

Netocrat 08-17-2005 08:28 AM

Re: AND and OR and parentheses
 
On Wed, 17 Aug 2005 06:38:50 +0000, Richard Bos wrote:

> "Malcolm" <regniztar@btinternet.com> wrote:
>
>> "akarl" <fusionfive@comhem.se> wrote
>> >
>> > Yes, I think parentheses around the "boolean" (atomic) expressions improve
>> > readability considerably, it's also customary in mathematics. (However, I
>> > think relying on the precedence of `and' over `or' is perfectly okay.)
>> >

>> The rule I use is that you can rely on your reader to know that * and / have
>> a higher precedence than + and -, and everything else needs parentheses.

>
> Are you sure?
>
> #include <stdio.h>
>
> int main(void)
> {
> int i, j;
>
> for ((i=1); (i<10); (i++)) {
> (j=(i*10+3));
> (printf(("Value nr. %d is %d\n"), (i), (j)));
> }
> }


Well if we're going to take Malcolm's statement literally without applying
common sense, this would be even better (we could go indefinitely further):

#include <stdio.h>

int main(void)
{
int i, j;

for ((((i=1))); (((i<10))); (((i++)))) {
(((j=(((i*10+3))))));
(((printf(((("Value nr. %d is %d\n"))), (((i))), (((j)))))));
}
}

I think that both of these fail to capture his intent.

--
http://members.dodo.com.au/~netocrat


Netocrat 08-17-2005 09:38 AM

Re: AND and OR and parentheses
 
On Wed, 17 Aug 2005 02:44:53 -0700, Suman wrote:
> Netocrat wrote:
>> On Wed, 17 Aug 2005 06:38:50 +0000, Richard Bos wrote:
>> > "Malcolm" <regniztar@btinternet.com> wrote:
>> >> "akarl" <fusionfive@comhem.se> wrote
>> >> >
>> >> > Yes, I think parentheses around the "boolean" (atomic) expressions
>> >> > improve readability considerably, it's also customary in
>> >> > mathematics. (However, I think relying on the precedence of `and'
>> >> > over `or' is perfectly okay.)
>> >> >
>> >> The rule I use is that you can rely on your reader to know that *
>> >> and / have a higher precedence than + and -, and everything else
>> >> needs parentheses.

>
> He m[i/e]ssed up with the last part of the sentence. Here it is for your
> viewing pleasure:
>
> With the usual exceptions, of course.


Yes, obviously that's what Malcolm meant and he shouldn't be required to
write it (which he didn't according to my newsfeed).

> [snipped a lot of code from fertile minds]
>
> C'mon guys -- be a sport.


I thought I was being - the point of my post was that the literal and
strict interpretation of what was clearly a helpful guideline rather than
a formal rule was lacking in common sense and obviously wasn't what was
intended. Hence the first line of my post: "if we're going to [not apply]
common sense" and the final line:

> I think that both of these fail to capture his intent.


--
http://members.dodo.com.au/~netocrat


Suman 08-17-2005 09:44 AM

Re: AND and OR and parentheses
 

Netocrat wrote:
> On Wed, 17 Aug 2005 06:38:50 +0000, Richard Bos wrote:
>
> > "Malcolm" <regniztar@btinternet.com> wrote:
> >
> >> "akarl" <fusionfive@comhem.se> wrote
> >> >
> >> > Yes, I think parentheses around the "boolean" (atomic) expressions improve
> >> > readability considerably, it's also customary in mathematics. (However, I
> >> > think relying on the precedence of `and' over `or' is perfectly okay.)
> >> >
> >> The rule I use is that you can rely on your reader to know that * and / have
> >> a higher precedence than + and -, and everything else needs parentheses.


He m[i/e]ssed up with the last part of the sentence. Here it is for
your
viewing pleasure:

With the usual exceptions, of course.

[snipped a lot of code from fertile minds]

C'mon guys -- be a sport.



All times are GMT. The time now is 05:45 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.