Jump to content

Photo

Logical operators in XB, vs mathematical operators in conditionals

TI Extended BASIC XB IF-THEN

27 replies to this topic

#1 OLD CS1 OFFLINE  

OLD CS1

    >OLD CS1█

  • 6,095 posts
  • Technology Samurai
  • Location:Tallahassee, FL

Posted Wed Aug 17, 2016 9:14 AM

I have been looking at a COMPUTE! Magazine program from 1983 1984 called "Circus."  Not sure why, but I felt compelled to OCR the PDF and then correct the text produced so it will actually run.  I noticed a few inefficiencies, but what particularly caught my eye were lines like these:

380 IF ((H=104)+(H=112)+(H=120)+(H=128))*(FL=1)THEN GOSUB 50:: GOTO 290
390 IF (H=104)+(H=112)+(H=120)+(H=128)THEN GOSUB 50 :: DY=1 :: GOTO 290

Instead of using Extended BASIC's logical OR and AND operators, the programmer used the + and * mathematics as if programming in regular TI BASIC.

 

Are there any performance benefits to using one over the other in Extended BASIC?

 

The full listing follows.

Spoiler


#2 adamantyr OFFLINE  

adamantyr

    Stargunner

  • 1,483 posts

Posted Wed Aug 17, 2016 9:50 AM

Nice, I typed in Circus way back when! BTW It was February 1984 issue of Compute! magazine.

 

My experience with writing Aperture in pure BASIC definitely exposed that the more complex your logic statement, the longer it takes for the interpreter to process it. So you're better off in most cases to check your most common scenario (which would be in this case, a space) so you can immediately act on it.

 

As to what is better, using AND/OR or the mathematical statements, I'm sure some of our XB guys who have mined the cartridge and spun their own versions can tell us for sure. :) I would surmise though that those lines would be equally over-complex even using AND and OR, as every condition has to be considered.

 

On a whole, the game could have been written to be much more efficient. It has the look that the original conversion was started in BASIC and then switched to Extended BASIC for performance improvements. But they still are using character graphics for movement, when using a sprite would be MUCH more efficient. (The pigeons are just a complete waste.) In fact, you could probably implement the Atari/Commodore 64 game design of two clowns on a see-saw in Extended BASIC. Hmm... maybe I'll do that. :)


Edited by adamantyr, Wed Aug 17, 2016 10:00 AM.


#3 Sinphaltimus OFFLINE  

Sinphaltimus

    River Patroller

  • 2,521 posts
  • Distracted at the Keyboard
  • Location:Poconos, PA

Posted Wed Aug 17, 2016 3:53 PM

Instead of using Extended BASIC's logical OR and AND operators,........

 

 

What AND OR operators are there in XB? 



#4 adamantyr OFFLINE  

adamantyr

    Stargunner

  • 1,483 posts

Posted Wed Aug 17, 2016 4:14 PM

 

 

What AND OR operators are there in XB? 

In TI BASIC, in order to do AND and OR operations, you have to utilize math.

 

Some examples:

 

IF A AND B THEN = IF (A<>0)*(B<>0) THEN

IF A OR B THEN = IF (A<>0)+(B<>0) THEN

 

The principal is that any value check inside parenthesis will equate to either 0 (if false) or -1 (if true). Don't ask my why it's not 1, some weirdness in the interpreter...

 

In TI Extended BASIC, you have AND and OR keywords available so you can use those instead. Doing a variable alone is equivalent to "Is this variable not zero?"

 

You can also do bitwise operations in Extended BASIC, which is awesome. If you do a statement like "IF A AND 32 THEN" it will actually do a bitwise check of A using the provided value as a bitmask.

 

So if A was equal to 36: 00100100

And you did A AND 32: 00100000

 

Then the result is 32, not zero, and the IF statement passes!


Edited by adamantyr, Wed Aug 17, 2016 4:23 PM.


#5 Lee Stewart OFFLINE  

Lee Stewart

    River Patroller

  • 3,966 posts
  • Location:Silver Run, Maryland

Posted Wed Aug 17, 2016 4:15 PM

 

 

What AND OR operators are there in XB? 

 

There are four logical operators.  In order of precedence, they are: NOT, XOR, AND and OR.

 

...lee



#6 Sinphaltimus OFFLINE  

Sinphaltimus

    River Patroller

  • 2,521 posts
  • Distracted at the Keyboard
  • Location:Poconos, PA

Posted Wed Aug 17, 2016 4:22 PM

In TI BASIC, in order to do AND and OR operations, you have to utilize math.

 

Some examples:

 

If A AND B THEN

Why isn't this in the manual? I mean, I've searched the manual (linked) for XOR and and and OR and that stuff but I didn't find them. I did find an example of an "AND" operation but it consisted of an if then else syntax that really confused me. Or am I losing it real fast?

Page 96
 

IF A = 1 THEN IF B = 2 THEN C = 3 ELSE
D = 4 ELSE E = 5 operates as follows: If
A is not equal to 1, then E is set equal to
5 and control passes to the next line, If A
is equal to 1 and B is not equal to 2, then
D is set equal to 4 and control passes to
the next line. If A is equal to 1 and B is
equal to 2, then C is set equal to 3 and
control passes to the next line.
>100 IF A=1 THEN IF B=2 THEN C=3
 
 
Now I went ahead and tried it with this..

10 a=1::b=1
20 if a=1 and b=1 then c=1
30 print c
 
my result was 0 but I also didn't get a syntax error. EDIT _ OOOOPS - I typed
20 if a=1 and b=1 then c=1
20  print c

Why isn't this in the manual? Or where exactly in the manual is AND OR mentioned as operators? 
 
 
EDIT: OK Logically that weird if-then-else statement makes sense to me after reading it a few times. I get that now. But AND OR as typed operations would be great.


This changes everything.

Edited by Sinphaltimus, Wed Aug 17, 2016 4:27 PM.


#7 adamantyr OFFLINE  

adamantyr

    Stargunner

  • 1,483 posts

Posted Wed Aug 17, 2016 4:27 PM

Page 42-43 of the TI Extended BASIC manual has the particulars.



#8 Sinphaltimus OFFLINE  

Sinphaltimus

    River Patroller

  • 2,521 posts
  • Distracted at the Keyboard
  • Location:Poconos, PA

Posted Wed Aug 17, 2016 4:30 PM

I can't believe this was right here the whole time.

OMFG - I'm loosing it fast. Lee again thanks and also adamantyr.

I probably shouldn't work this programming except on well rested days off.



#9 adamantyr OFFLINE  

adamantyr

    Stargunner

  • 1,483 posts

Posted Wed Aug 17, 2016 4:31 PM

I can't believe this was right here the whole time.

OMFG - I'm loosing it fast. Lee again thanks and also adamantyr.

I probably shouldn't work this programming except on well rested days off.

 

We say that all the time about the Editor Assembler manual as well. :D



#10 Sinphaltimus OFFLINE  

Sinphaltimus

    River Patroller

  • 2,521 posts
  • Distracted at the Keyboard
  • Location:Poconos, PA

Posted Wed Aug 17, 2016 4:34 PM

 

We say that all the time about the Editor Assembler manual as well. :D

I was going to apologize for being a dumbass until I saw your response. LOL. Yeah. I dunno. How disappointed I was without the use of AND OR and now NOT, I was stuck with <,>,<>,= this whole time. Because things I tried like | & didn't work. *scratches head* thanks for the chuckle.


Edited by Sinphaltimus, Wed Aug 17, 2016 4:34 PM.


#11 apersson850 OFFLINE  

apersson850

    Dragonstomper

  • 594 posts

Posted Wed Aug 17, 2016 4:35 PM

The principal is that any value check inside parenthesis will equate to either 0 (if false) or -1 (if true). Don't ask my why it's not 1, some weirdness in the interpreter...

Not at all. Zero is zero, regardless of the system. If we look at 16 bits only, then not(0000H) = FFFFH. But in two complement notation, FFFFH = -1. Thus inverting all bits in zero returns minus one. This is true regardless of the number of bits, as long as we look at integers. It would not be true in the radix 100 floating point format used in the 99/4A, though.



#12 Sinphaltimus OFFLINE  

Sinphaltimus

    River Patroller

  • 2,521 posts
  • Distracted at the Keyboard
  • Location:Poconos, PA

Posted Wed Aug 17, 2016 4:36 PM

And now my search for XOR is finding it all over the pdf. SMH
 



#13 Lee Stewart OFFLINE  

Lee Stewart

    River Patroller

  • 3,966 posts
  • Location:Silver Run, Maryland

Posted Wed Aug 17, 2016 5:08 PM

You have to be careful how you use the logical operators because they are bitwise operators operating on 16-bit values (-32768 – 32767).  This is why TRUE = -1 (all bits set) and FALSE = 0 (all bits cleared). Running the following code may surprise you with the third result:

 

X = 1

PRINT X=0; NOT(X=0); NOT X

 0 -1 -2

 

The first expression X=0 = 0 (FALSE) because X = 1.  The second expression NOT(X=0) = NOT 0 = -1 (TRUE).  You may have been surprised with the third result: NOT X = NOT 1 (binary: 0000000000000001) = -2 (binary: 1111111111111110).  This could ruin your day if you expected FALSE in an IF statement because any value except 0 is treated as being true.

 

Here is another example:

 

X = 20

Y = 10

PRINT X AND Y

 0

 

The binary representation shows why this is correct:

 

0000000000010100 AND

0000000000001010

----------------

0000000000000000

 

...lee



#14 Lee Stewart OFFLINE  

Lee Stewart

    River Patroller

  • 3,966 posts
  • Location:Silver Run, Maryland

Posted Wed Aug 17, 2016 5:09 PM

We say that all the time about the Editor Assembler manual as well. :D

 

and how!!  :-o

 

...lee



#15 Lee Stewart OFFLINE  

Lee Stewart

    River Patroller

  • 3,966 posts
  • Location:Silver Run, Maryland

Posted Wed Aug 17, 2016 5:39 PM

Not at all. Zero is zero, regardless of the system. If we look at 16 bits only, then not(0000H) = FFFFH. But in two complement notation, FFFFH = -1. Thus inverting all bits in zero returns minus one. This is true regardless of the number of bits, as long as we look at integers. It would not be true in the radix 100 floating point format used in the 99/4A, though.

 

I think your last sentence may be misleading, Anders.  Though the representation of numbers in XB is, in fact, radix 100, before using the logical operators, XB rounds the number to an integer.  If any of the numbers are greater than 32767 or less than -32768, XB will issue an error.  Consider the following:

 

X = 122.55678

Y = 123

PRINT 0 OR X; X=Y; X AND Y

 123 0 123

Z = 123E10

PRINT Z

 1.23E+12

PRINT Z OR 0

* BAD VALUE

 

...lee


Edited by Lee Stewart, Wed Aug 17, 2016 5:47 PM.


#16 Sinphaltimus OFFLINE  

Sinphaltimus

    River Patroller

  • 2,521 posts
  • Distracted at the Keyboard
  • Location:Poconos, PA

Posted Wed Aug 17, 2016 6:37 PM

The more I read about assembly the more I know I'm going to have to think in binary. Not really memorize or be a binary translation whiz or anything like that, but to understand all this about the TI and math and doing simple coding.

#17 InsaneMultitasker OFFLINE  

InsaneMultitasker

    River Patroller

  • 2,427 posts

Posted Wed Aug 17, 2016 7:11 PM

The relationship operators are pretty handy, particularly for single expressions that require a true/false answer that may aid in computing another value.

 

For example, adding a preceding zero to a printed list of numbers:

FOR K=1 to 15

PRINT RPT$("0",-(K<10))&STR$(K)

NEXT K

 

Perhaps you want to toggle a value between two numbers:

1 Z=40

2 Z=-(80*(Z=40)+40*(Z=80))

3 PRINT Z::GOTO 2

 

As Lee points out, the logical (AND, OR, etc) operators can be tricky because depending on the expression, they can be used to generate either a true/false or a bitwise result.



#18 OLD CS1 OFFLINE  

OLD CS1

    >OLD CS1█

  • Topic Starter
  • 6,095 posts
  • Technology Samurai
  • Location:Tallahassee, FL

Posted Wed Aug 17, 2016 7:18 PM

Sooooooo........ any performance benefits in using mathematical operators versus logical operators in Extended BASIC?



#19 InsaneMultitasker OFFLINE  

InsaneMultitasker

    River Patroller

  • 2,427 posts

Posted Wed Aug 17, 2016 9:18 PM

Substituting AND/OR for */+ in line 380 and looping that comparison one thousand times yielded less than 1 second variation between operator usage.  (35 seconds versus 36).

 

It isn't clear to me how the interpreter handles these internally, i.e., does it convert one to the other and use the same comparison code?  Perhaps RXB or some other folks know those details.  I have always used the mathematical relationship operators because long ago I was told they were faster and I like to avoid the bitwise gotchas. I never tried to prove that first claim...



#20 adamantyr OFFLINE  

adamantyr

    Stargunner

  • 1,483 posts

Posted Wed Aug 17, 2016 9:54 PM

Yeah, I think internally it all uses the same system.

That said, you could improve performance. My favorite? Use POS. Put your potential characters into a string and then use POS to check what you find, and an ON GOTO after to route to specific routines.

#21 OLD CS1 OFFLINE  

OLD CS1

    >OLD CS1█

  • Topic Starter
  • 6,095 posts
  • Technology Samurai
  • Location:Tallahassee, FL

Posted Wed Aug 17, 2016 10:27 PM

Yeah, I think internally it all uses the same system.

That said, you could improve performance. My favorite? Use POS. Put your potential characters into a string and then use POS to check what you find, and an ON GOTO after to route to specific routines.

 

I had forgotten about that, but I used it a LOT in my own programs.  Between converting the numbers to FP to make comparisons and looking up the string to make comparisons, I bet the difference there is minimal, as well.



#22 sometimes99er OFFLINE  

sometimes99er

    River Patroller

  • 4,225 posts
  • Location:Denmark

Posted Wed Aug 17, 2016 11:55 PM

Perhaps you want to toggle a value between two numbers:
1 Z=40
2 Z=-(80*(Z=40)+40*(Z=80))
3 PRINT Z::GOTO 2


or

1 Z=40
2 Z=120-Z
3 PRINT Z::GOTO 2

Your strategy can be used for "toggling" between any number of numbers though. Maybe an array would quickly end up being nicer.

;)

Edited by sometimes99er, Thu Aug 18, 2016 12:02 AM.


#23 OLD CS1 OFFLINE  

OLD CS1

    >OLD CS1█

  • Topic Starter
  • 6,095 posts
  • Technology Samurai
  • Location:Tallahassee, FL

Posted Thu Aug 18, 2016 12:49 AM

Right, a lot of my games used something like

PLAYER=3-PLAYER

This would switch between player 1 and player 2.



#24 Opry99er OFFLINE  

Opry99er

    Quadrunner

  • 10,731 posts
  • Location:Hustisford, WI

Posted Thu Aug 18, 2016 10:25 AM

^^HA!!^^

Melikey

#25 apersson850 OFFLINE  

apersson850

    Dragonstomper

  • 594 posts

Posted Thu Aug 18, 2016 2:43 PM

Yes, I kept it a bit short, so I didn't go into the implict conversion between real and integer when the logical operators are used. Floating point multiply is for sure more complex than AND, but using AND suffers from the conversion real -> integer before and integer -> real after the operation. This is more efficient in for example Pascal, where you can use integers as variables. Hence the conversions aren't needed.







Also tagged with one or more of these keywords: TI Extended BASIC, XB, IF-THEN

0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users