View unanswered posts | View active topics It is currently Fri Feb 21, 2020 5:51 am






Reply to topic  [ 12 posts ] 
if-condition check does not seem to work correct (a && b) 
Author Message
Rookie

Joined: Mon Mar 02, 2009 7:03 pm
Posts: 6
Post if-condition check does not seem to work correct (a && b)
Hi,
I'm currently working with robotc at my university and noticed that an if-condition like (false && false && true) does not abort checking after the first &&. In fact even the statement resulting in "true" is checked. Normally this should be the case with single & signs but not if they appear doubled. Am I correct or am I wrong?

Example:
if (a >= 0 && b >= 0 && map[a][b] == c) {
foo();
}
else {
bar();
}

let's assume values:
a = -1
b = -1
c = 0

Noticed behaviour on NXT: The code throws an 'index out of bounds' exception (7) because map[a][b] == c is checked as map[-1][-1] == c.
Expected behaviour: The condition check should abort and bar() should be called.

Working solution:
if (a >= 0 && b >= 0) {
if (map[a][b] == c) {
foo();
}
else {
bar();
}
}

Is there any place to submit this bug report?

Andreas


Mon Mar 02, 2009 7:11 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: if-condition check does not seem to work correct (a && b)
what happens if you put the terms in brackets:

Code:
if ((a >= 0) && (b >= 0) ) {

/..

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Last edited by Ford Prefect on Fri Mar 06, 2009 3:07 pm, edited 3 times in total.



Tue Mar 03, 2009 2:37 pm
Profile
Rookie

Joined: Thu Mar 05, 2009 11:14 pm
Posts: 2
Post Re: if-condition check does not seem to work correct (a && b)
If you are trying to see that the statement is completed when all the arguments are true, then you may want to set it up like so since you have contradicting arguments.
Code:
if (a >= 0 && b >= 0 ) {
    if (map[a][b] == c) {
        foo();
    }
    else {
    bar();
    }
}


now for this, if both of the arguments in the first if statement are true, it will go to the contents. Inside the contents there is another if statement, and if it is true it will go to 'foo()' and if it isn't it will go to 'bar()'. if the first statement isn't true, then it will just skip this section completely.


Thu Mar 05, 2009 11:22 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: if-condition check does not seem to work correct (a && b)
EDIT:
maybe RobotC starts checking all arguments from the right to the left,
so the first term which is checked might be the map[x][y] term.
This may be the reason why the program aborts before the middle and the left term
(a >= 0) && (b >= 0 )
could have been checked and might have returned a "false".

Try to cascade the if condition (like in your working solution) or try a test:
put the map-term to the left side and see what happens:
if ((map[a][b] == c) && (a >= 0) && (b >= 0 ))

If RobotC is sort of "smart" it will break the if condition and returns "false" as soon as the 1st false argument has been found (starting from the right).
(I actually don't suppose it to be sort of this, but you'll never know... ;) )

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Fri Mar 06, 2009 3:12 pm
Profile
Rookie

Joined: Mon Mar 02, 2009 7:03 pm
Posts: 6
Post Re: if-condition check does not seem to work correct (a && b)
@mr mckee: It seems like you haven't read my post completly ;) Anyway thanks.

@all others: putting the conditions into parenthesis ( ) does not help. Also if they are switched from right to left it does not make a change.

Andreas


Fri Mar 06, 2009 3:48 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: if-condition check does not seem to work correct (a && b)
so RobotC always seems to make a full boolean check of all conditional terms.
Your workaraound of 2 cascaded if conditions might be the only way to solve this problem.

I'm curious if ANSI compilers (Borland, Microsoft, gpp) behave different. But consider that RobotC is NOT ANSI anyway .

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Last edited by Ford Prefect on Sat Mar 07, 2009 5:38 pm, edited 1 time in total.



Fri Mar 06, 2009 5:52 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: if-condition check does not seem to work correct (a && b)
According to my copy of K+R "The C Programming Language" the Logical And Operator should work as follows:

Quote:
The && operator groups left to right. It returns 1 if both its operands compare unequal to zero, 0 or otherwise. Unlike &, && guarantees left to right evaluation; the first operand is evaluated, including all side effects; if it is equal to 0, the value of the expression is 0. Otherwise the right operand is evaluated, and if it is equal to 0, the expression's value is 0, otherwise 1.


That means that the 2nd operand should NOT be evaluated if the first one did not pass.

Regards,
Xander

PS: I love my 2nd edition of "The C Programming Language". I've had it for about 14 years now. The small text at the beginning of the book reads:
Quote:
This book was typeset (pic | tbl | eqn | troff -ms) in Times Romand and Courier by the authors, using an Autologic APS-5 phototypesetter and a DEC VAX 8850 running the 9th edition of the UNIX operating system.

How old-school is that? :)

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Mar 06, 2009 6:00 pm
Profile WWW
Rookie

Joined: Thu Mar 05, 2009 11:14 pm
Posts: 2
Post Re: if-condition check does not seem to work correct (a && b)
so you are just assuming it's a bug? try debugging it with try and catch exceptions. it will help you determin if you are actually seeing a bug, or that it is just throwing excetions, which it seems to be, but nothing to catch them.


Sat Mar 07, 2009 5:06 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: if-condition check does not seem to work correct (a && b)
McKee,

Can you provide us with an example of how to use this try and catch method you refer to? The only examples of the "catch" call I could find is in the example directory and pertains to user generated events, not exceptions.

Regards,
Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Mar 07, 2009 5:24 pm
Profile WWW
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: if-condition check does not seem to work correct (a && b)
Using the following program, you can see that only the first function gets called, not the second.

Code:
bool funA () {
  nxtDisplayTextLine(1, "funA");
  return false;
}

bool funB() {
  nxtDisplayTextLine(2, "funB");
  return true;
}

task main {
  if (funA() && funB())
    nxtDisplayTextLine(3, "all true");
  else
    nxtDisplayTextLine(3, "some/all false");
  while(true);
}


In this example, both get called because funA now returns true:
Code:
bool funA () {
  nxtDisplayTextLine(1, "funA");
  return true;
}

bool funB() {
  nxtDisplayTextLine(2, "funB");
  return false;
}

task main {
  if (funA() && funB())
    nxtDisplayTextLine(3, "all true");
  else
    nxtDisplayTextLine(3, "some/all false");
  while(true);
}

That means that it is evaluating as per the standard C "bible".

Regards,
Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Mar 07, 2009 6:07 pm
Profile WWW
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: if-condition check does not seem to work correct (a && b)
hi Xander,
if it should be as you assume, the exception reported by the TO must not have happened:

andreas.balzer wrote:

Example:
if (a >= 0 && b >= 0 && map[a][b] == c) {
foo();
}
else {
bar();
}

let's assume values:
a = -1
b = -1
c = 0

Noticed behaviour on NXT: The code throws an 'index out of bounds' exception (7) because map[a][b] == c is checked as map[-1][-1] == c.
Expected behaviour: The condition check should abort and bar() should be called.

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Sat Mar 07, 2009 6:26 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: if-condition check does not seem to work correct (a && b)
I ran some tests with this and I can reproduce it but I can also very easily prevent it without too much hassle.

Using this program:
Code:
int a = -1;
int b = -1;
int c = 0;
byte map[10][10];


void funA () {
  nxtDisplayTextLine(1, "funA");
}

void funB() {
  nxtDisplayTextLine(2, "funB");
}

task main {
  memset(map, 0, sizeof(map));
  if (a >= 0 && b >= 0 && map[a][b] == c) {
    funA();
  } else {
    funB();
  }
  while(true);
}

I get the error

However, with this program, I don't:
Code:
<snip> <snip>
task main {
  memset(map, 0, sizeof(map));
  if ((a >= 0) && (b >= 0) && (map[a][b] == c)){
    funA();
  } else {
    funB();
  }
  while(true);
}


So grouping is important. I did test the program (with some modifications, of course) with gcc under Linux and it core dumped (exit code 5). That is pretty much the equivalent of what happened on the NXT. Of course, grouping not only helps the compiler determine what in what order you'd like your expressions evaluated, if also helps the readers :)

Regards,
Xander

PS: The Linux test machine is a Debian 4.0 virtual machine using gcc 4.1.2

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Mar 08, 2009 4:06 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 12 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.