Author |
Topic: Creating a negative value (Read 320 times) |
|
Matt
Developer
member is offline


Gender: 
Posts: 210
|
 |
Creating a negative value
« Thread started 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
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Creating a negative value
« Reply #1 on: May 25th, 2013, 07:50am » |
|
on May 25th, 2013, 06:51am, Matt wrote: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.
|
« Last Edit: May 25th, 2013, 08:18am by admin » |
Logged
|
|
|
|
Matt
Developer
member is offline


Gender: 
Posts: 210
|
 |
Re: Creating a negative value
« Reply #2 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.
Matt
|
|
Logged
|
|
|
|
|