View unanswered posts | View active topics It is currently Tue Feb 25, 2020 6:03 am

 Page 2 of 2 [ 22 posts ] Go to page Previous  1, 2
 Print view Previous topic | Next topic
StopPgm and UserMode variables values are "backwards"
Author Message
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
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)

Thu Oct 09, 2008 3:09 pm
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Re: StopPgm and UserMode variables values are "backwards"
 Ford Prefect wrote:The trouble begins if you set sth like#define true 1if 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
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
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)

Thu Oct 09, 2008 3:30 pm
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 616
Re: StopPgm and UserMode variables values are "backwards"
 Ford Prefect wrote:[Dick,this is not correct !in ANSI Cthe "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
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
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
Rookie

Joined: Thu Oct 09, 2008 8:11 pm
Posts: 4
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
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
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.

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)

Fri Oct 10, 2008 4:41 am
Display posts from previous:  Sort by
 Page 2 of 2 [ 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 forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning

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