Oxygen Basic

Programming => Problems & Solutions => Topic started by: Arnold on November 27, 2015, 01:58:47 AM

Title: Toggle = not Toggle that is the question
Post by: Arnold on November 27, 2015, 01:58:47 AM
Hi Charles,

I would like to ask a question about boolean values. I tried out this little test code:

Code: OxygenBasic
  1. 'Toggle.o2bas
  2.  
  3. include "$/inc/minwin.inc"
  4. include "$/inc/console.inc"
  5.  
  6. print "True = " true & cr
  7. print "False = " false & cr & cr
  8.  
  9. bool toggle = not false  'true
  10.  
  11. while 1
  12.   toggle=not toggle
  13.   print "toggle = " toggle & cr
  14.   if not toggle then print "this is: not toggle  (" toggle ")" & cr
  15.   print "Enter ..." & cr & cr
  16.   waitkey()
  17. wend
  18.  

If I run this code the case of 'not toggle' will be displayed correctly. If I apply at the beginning: bool toggle = true then this will not work. (not toggle -> -2).

I suppose there are reasons why you use -1/0 for true/false in boolean types? But why is 1/-2 used for true/false values if I apply the true value of minwin.inc?

Roland

.
Title: Re: Toggle = not Toggle that is the question
Post by: Patrice Terrier on November 27, 2015, 06:51:32 AM
This is a classical behavior.

Better to define is as long

long toggle = 0

toggle = not toggle // then toggle = -1

if toggle is define as toogle = 1, then you will always get a bad result with the NOT operator.

Title: Re: Toggle = not Toggle that is the question
Post by: Arnold on November 27, 2015, 07:21:35 AM
Hi Patrice,

bool toggle
bool toggle = false
bool toggle = -1

these combinations will also work with OxygenBasic besides using long. So there would be enough workarounds. I only wondered if there is a reason for 1/-2 values when using 1 as a value for True.

Roland
Title: Re: Toggle = not Toggle that is the question
Post by: Patrice Terrier on November 27, 2015, 08:59:14 AM
This is because NOT 1 = -2
Title: Re: Toggle = not Toggle that is the question
Post by: Arnold on November 27, 2015, 12:33:39 PM
Thank you for the help. I did not expect this result, but my windows calculator gives the same answer. Although I did not yet find the explanation in Internet for this rule I will keep it in mind for the future.

Roland
Title: Re: Toggle = not Toggle that is the question
Post by: Charles Pegge on November 27, 2015, 01:38:37 PM
Hi Roland & Patrice,

I would add that Oxygen does not use real boolean types. It uses 32 bit integers, in common with C/Windows API.

All Oxygen logic is bitwise, so when performing Boolean logic, use % true -1

some negative binaries and their end bit patterns:

-1  ffff  1111
-2  fffe  1110
-3  fffd  1101
-4  fffc  1100

Title: Re: Toggle = not Toggle that is the question
Post by: Mike Lobanovsky on November 27, 2015, 02:06:10 PM
In order to follow Boolean logic rules, True doesn't have to be defined at all. With False = 0 as in all the languages I'm aware of, the assumption that True is anything that's <> False | 0 is sufficient to express every possible logical combination. The numeric representation of True as -1 for retro-BASICs and other languages with a long historical record is purely traditional and is strictly engine implementation specific; any representation would do as long as it is consistent and taken account of by the engine.

Note that C defines False as long 0, and True is in fact anything that's not False, with the internal numeric representation of True being 1. C++, C#, Visual Basic, and VB.NET follow the same pattern. FBSL follows it too.

Also note that, despite a particular numeric representation of True and False in a given language, many languages would not allow you direct arithmetics (hexa, decimal, octal, or binary) with Booleans or comparison of Boolean values for equality/inequality with numeric values where the sign of their numeric representation might have otherwise affected the result.

For the sake of compatibility with major modern BASICs and C/C++/C# and their derivatives, I'd recommend sticking with the C interpretation of Booleans when designing your own Boolean based branching in your apps.
Title: Re: Toggle = not Toggle that is the question
Post by: JRS on November 27, 2015, 09:25:41 PM
Mike is correct.

Code: [Select]
TRUE = NOT FALSE
Title: Re: Toggle = not Toggle that is the question
Post by: Mike Lobanovsky on November 28, 2015, 02:23:56 AM
Yes, John, and that's the only thing you can safely rely on. Everything else comes from the devil (or the dev). :D
Title: Re: Toggle = not Toggle that is the question
Post by: Charles Pegge on November 28, 2015, 02:03:14 PM

Laws of Form  by G Spencer-Brown, provides A very interesting view on the roots of logic.

https://en.wikipedia.org/wiki/Laws_of_Form

In this perspective, conflating operators and operands, True is the null no-action state, and False is the distinctive/transformative state.

Title: Re: Toggle = not Toggle that is the question
Post by: JRS on November 28, 2015, 05:54:41 PM
The belief that anything other than zero is TRUE seems correct in my mind. In Script BASIC, FALSE = 0 and TRUE = -1. TRUE/FALSE are SB keywords with predefined values.

Code: Script BASIC
  1. T = 10
  2. IF T = NOT FALSE THEN
  3.   PRINT T,"\n"
  4. END IF
  5.  


jrs@laptop:~/sb/sb22/test$ scriba notfalse.sb
jrs@laptop:~/sb/sb22/test$


OT

As to the discussion on BP.org about language popularity, PHP and JavaScript are the most widely used languages in todays computing. The desktop is no longer king.

Title: Re: Toggle = not Toggle that is the question
Post by: Arnold on November 29, 2015, 04:15:17 AM
Thank you again for all the info. I now understand that:

not   0=-1, not  1=-2, not  5=-6, not 17=-18 etc.
not -18=17, not -6=5,  not -2=1,  not -1=0

This looks simple, but it was not for me. After some experimenting I found that False and True defined in minwin.inc should work, sometimes with a small workaround. Therefore for some special cases I will use:

% bTrue=-1 'bitwise True

I think this should be sufficient to catch most of the possible False / True cases.

Roland
Title: Re: Toggle = not Toggle that is the question
Post by: Peter on November 29, 2015, 04:36:36 AM
As to the discussion on BP.org about language popularity, PHP and JavaScript are the most widely used languages in todays computing. The desktop is no longer king.

Now I am slave.  >:(
Title: Re: Toggle = not Toggle that is the question
Post by: JRS on November 29, 2015, 04:50:48 AM
Quote
Now I am slave.

Could be worse. You could be someone's bitch.  :-*

Computed GOTO

In the day ...
Code: [Select]
ON x GOTO 100, 200, 300, 400, ...


Title: Re: Toggle = not Toggle that is the question
Post by: Peter on November 29, 2015, 06:07:32 AM
Quote
Computed GOTO

will do when I'm dead.  ;D
Title: Re: Toggle = not Toggle that is the question
Post by: Mike Lobanovsky on November 29, 2015, 07:15:36 AM
In Script BASIC, FALSE = 0 and TRUE = -1. TRUE/FALSE are SB keywords with predefined values.

Code: Script BASIC
  1. T = 10
  2. IF T = NOT FALSE THEN
  3.   PRINT T,"\n"
  4. END IF
  5.  

The idea to check anything against literal TRUE or FALSE is surrealistic, numeric representation dependent, and bug prone. I would even call it "bad programming practice".

Instead,

Code: OxygenBasic
  1. N = 10
  2. IF N THEN ' evaluates to TRUE for any N except N = 0 in any language
  3. ....

and

Code: OxygenBasic
  1. N = 0
  2. IF NOT N THEN ' evaluates to TRUE only for N = 0 in any language
  3. ....

is independent of actual numeric representation of TRUE in a language, be it -1 or +1 or -1M or +1M, whatever.

The only practical case of seeing what TRUE evaluates to numerically in a given language I ever needed was

Code: OxygenBasic
  1. A = 10: B = 10
  2. PRINT A = B ' would print -1 in O2 and SB but 1 in FBSL, C/C++/C#, VB6, VB.NET, etc.
  3. ....

OT

Quote
As to the discussion on BP.org about language popularity, PHP and JavaScript are the most widely used languages in todays computing. The desktop is no longer king.

I think your sentence definitely misses a tongue smiley. ;) As far as I am concerned, flooding the world with web pages generated from templates in WYSIWYG IDEs for handheld gimmiks and wifi aware dishwashing machines has nothing to do with computing.

The entire world order could have worked in the exact same way if there were IUP instead of HTML, C instead of PHP, and SB instead of JS. You have overslept your glory moments, John.
Title: Re: Toggle = not Toggle that is the question
Post by: JRS on November 29, 2015, 10:50:11 AM
Quote
The entire world order could have worked in the exact same way if there were IUP instead of HTML, C instead of PHP, and SB instead of JS. You have overslept your glory moments, John.

Actually you're not too far off with my real life project converting Sage 100 accounting to Linux.