BBC BASIC for Windows
« Creating a negative value »

Welcome Guest. Please Login or Register.
Apr 6th, 2018, 12:20am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Creating a negative value  (Read 320 times)
Matt
Developer

member is offline

Avatar




PM

Gender: Male
Posts: 210
xx 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
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx 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 » User IP Logged

Matt
Developer

member is offline

Avatar




PM

Gender: Male
Posts: 210
xx 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
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls