ROBOTC.net forums
http://www.robotc.net/forums/

StopPgm and UserMode variables values are "backwards"
http://www.robotc.net/forums/viewtopic.php?f=52&t=766
Page 2 of 2

Author:  Ford Prefect [ Thu Oct 09, 2008 3:09 pm ]
Post subject:  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.

Author:  Jeff McBride [ Thu Oct 09, 2008 3:20 pm ]
Post subject:  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.

Author:  Ford Prefect [ Thu Oct 09, 2008 3:30 pm ]
Post subject:  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.

Author:  Dick Swan [ Thu Oct 09, 2008 4:00 pm ]
Post subject:  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".

Author:  Jeff McBride [ Thu Oct 09, 2008 4:01 pm ]
Post subject:  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

Author:  ptan [ Thu Oct 09, 2008 8:16 pm ]
Post subject:  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.

Author:  Ford Prefect [ Fri Oct 10, 2008 4:41 am ]
Post subject:  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.

Page 2 of 2 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/