BBC BASIC for Windows
Programming >> BBC BASIC language >> Creating a negative value http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1369464708 Creating a negative value
Post by Matt on May 25th, 2013, 06:51am
Hi,
When reading a check box, for instance, the returned value is either 0 or 1. In order to use this a boolean value it must be turned in to 0 and -1 respectively. Do it the other way, the ABS keywork can be used, but there doesn't seem to be a version for negating. So far, I have been using A% = -B% to get the result required (and it works fine), but is there a more correct way?
Matt
Re: Creating a negative value
Post by admin on May 25th, 2013, 07:50am
When reading a check box, for instance, the returned value is either 0 or 1. In order to use this a boolean value it must be turned in to 0 and -1 respectively.
Not as a rule: 0 and 1 are perfectly good Booleans in BBC BASIC. Only rarely would you need to convert +1 to -1, for example if you were wanting to reverse the result of a test using NOT, which is always avoidable. I don't think I've ever bothered to negate the +1, I've simply treated it as a Boolean, but of course as you say if you do want to you can just use the unary minus.
Remember that BBC BASIC does not have a true Boolean data type (the same is true of C), so as a programmer you have some flexibility as to what value to use for 'true'. All that matters is that your program gives the results you need!
Edit: You can create a Boolean 'not' function, equivalent to C's ! operator, as follows:
Code:
DEF FNnot(b%) = (b% == FALSE)
Richard.
Re: Creating a negative value
Post by Matt on May 25th, 2013, 1:23pm
Thanks Richard.
The main reason I change 1 to -1 is simply to follow standard boolean TRUE / FALSE figures created by BB4W.
I do use NOT quite frequently in conditional statements, which is perhaps wrong.
Also, on the off chance that I later need to to flip it rather than set it, it will follow through correctly without me having to re-examine the code. As you've stated before on previous threads, NOT 1 does not equal 0. So if A% is 1, and therefore TRUE, NOT A% is -2, and therefore also TRUE.
Just a matter of consistency, really.
But all-in-all, as you say, I can probably get away with out doing so.