View unanswered posts | View active topics It is currently Sun Apr 20, 2014 10:48 pm






Reply to topic  [ 22 posts ]  Go to page Previous  1, 2
StopPgm and UserMode variables values are "backwards" 
Author Message
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: StopPgm and UserMode variables values are "backwards"
but it DOES work this way, because every C compiler fulfills the algebraic sentences of logic.
(not (1==2)) = true
(not (2==2)) = false

Actually true and false are 2 possible values of a binary digit.
But C doesn't store boolean values in bits, but in byte or word variables.
And thats why even if always false=0 => true = (!0) = anything but 0.

The trouble begins if you set sth like
#define true 1

if you did, it's bullshit.

_________________
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)}


Thu Oct 09, 2008 3:09 pm
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post Re: StopPgm and UserMode variables values are "backwards"
Ford Prefect wrote:

The trouble begins if you set sth like
#define true 1

if you did, it's bullshit.


Give me a single example of a C compiler that doesn't do that. In your logic they are all bullshit.

_________________
Jeff McBride
Benson Robotics Club


Thu Oct 09, 2008 3:20 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: StopPgm and UserMode variables values are "backwards"
there is no value "true" or "false" in ANSI C, so the problem doesn't exist here.

But if other compilers did it the same way as it's implemented in RobotC: yes, then it's bullshit, too,
because it breaks the algebraic rules.

There's no equality in injustice.

_________________
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)}


Thu Oct 09, 2008 3:30 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post Re: StopPgm and UserMode variables values are "backwards"
Ford Prefect wrote:
[Dick,
this is not correct !

in ANSI C
the "truth value FALSE" means equal to 0 (zero)
the "truth value TRUE" means unequal to 0, so any other value different from 0 is TRUE (e.g., 2 or 6Million or -784623 or "t").

I think if you had actually checked the ANSI C standard, you'll find that it doesn't define a "bool" type at all! You need to get to C++.

You will find that most C compiles include the file "stdbool.h" in their library distribution. And you'll usually find that somewhere in this file declarations similar to the following.
Code:
#define bool unsigned char
#define true 1
#define false 0

It is pointless to continue this discussion. ROBOTC is what it is on 'bool' types. It is one of the useful extensions to the language implemented in ROBOTC. And this type of extension is found in many other much higher cost C compilers (Microsoft, IAR, Renesas, Microchip, Atmel, ...) -- either as a C extension or via "stdbool.h".


Thu Oct 09, 2008 4:00 pm
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post Re: StopPgm and UserMode variables values are "backwards"
That is exactly my point. Although ANSI C doesn't define "true" and "false" at the language level, most compilers include #define, const or enum declarations called "true" and "false" for the conviencence of programmers. They are useful for making code more readable such as the difference between:
Code:
bool stopProgram;

stopProgram = 1;


and

Code:
bool stopProgram;

stopProgram = true;


If you look at my earliest post on this topic, my recommendation was to avoid using "true" and "false" as operands with relational operators "== true", "== false", etc. Since the other operand is already a boolean expression the relational operator is unnecessary and leads to exactly this kind of confusion. The "true" and "false" constants should only be used as r-values in assignment statements "x = true" and as parameters to functions that take boolean arguments "x(false)".

If ANSI C did define "true" and "false" as language keywords then the behaivor you are asking for would make sense. Since the language (and therefore the compiler) does not believe that there is anything special about the words "true" and "false", it can't contextually change the meaning of "==" as you suggest.

Note: There are non-C languages that intrinsicly support "true" and "false" as keywords and do not have these kinds of issues. Unfortunately none of them will generate code for the NXT.

Jeff

_________________
Jeff McBride
Benson Robotics Club


Thu Oct 09, 2008 4:01 pm
Profile
Rookie

Joined: Thu Oct 09, 2008 8:11 pm
Posts: 4
Post Re: StopPgm and UserMode variables values are "backwards"
I think you guys should just stop arguing about who is right, and look at this as a lesson on what to watch out for in "C" programming in the real world.

I have been programming in "C" for 30 years (yes, since 1978), and I never make any assumptions on what TRUE and FALSE are defined as. I always test for FALSE and NOT FALSE instead of testing for TRUE since in my experience, different compilers do indeed define TRUE as different values.

Just test for FALSE or do your own #define to redefine what you think TRUE and FALSE should be.

Paul Tan.


Thu Oct 09, 2008 8:16 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: StopPgm and UserMode variables values are "backwards"
Jeff McBride wrote:
That is exactly my point. Although ANSI C doesn't define "true" and "false" at the language level, most compilers include #define, const or enum declarations called "true" and "false" for the conviencence of programmers.
Note: There are non-C languages that intrinsicly support "true" and "false" as keywords and do not have these kinds of issues. Unfortunately none of them will generate code for the NXT.


I'm completely with you, that's all the same that I ever said.
Ford wrote:
I never said ANSI C specified intrinsic "true" and "false" constants, I spoke of truth values, an these are values of Boole's Algebra.
(...)
C compilers identify a statement (expression) as false, if the numeric value is equal to 0 (zero),
and a statement (expression) is identified as true, if the the numeric value is unequal to 0 (zero), e.g. 2 or even -3.


Regarding this, you can't go and define a "faked constant" also called "true", which always is only equal to 1.

That's a contradiction.

But maybe you can do it this way:
Code:
#define false 0
#define true (!false)


With this work-around, the "if-terms"
if (expression)
and
if (expression==true)
after all will become equivalent to each other

And so let's close this discussion.

_________________
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 Oct 10, 2008 4:41 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 22 posts ]  Go to page Previous  1, 2

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.