Author Topic: Basic Logic  (Read 183 times)

B+

  • Hero Member
  • *****
  • Posts: 814
    • View Profile
Basic Logic
« on: 17. May 2019, 15:07:02 »
Found at SdlBasic forum:
Quote
'Hey, folks. While porting some code from Python, I accidentally discovered that sdlBasic accepts this syntax for comparison expressions:


'Code:
'dim x = 0
'print 1 <= x <= 10


'That shouldn't be valid Basic, and indeed it returns true when it's not supposed to, but sdlBasic compiles and runs the code. Beware!


Code: [Select]
x = 0
PRINT "     1 <= x <= 10  is "; 1 <= x <= 10, " not supposed to return True???"
PRINT
PRINT "It is supposed to be True, Proof:"
PRINT "            1 <= x is "; 1 <= x '           > 0
PRINT "          0 <= 10  is "; 0 <= 10 '          > -1
PRINT "  ((1 < x) <= 10)  is "; ((1 < x) <= 10) '  > -1
PRINT
PRINT " See Basic knows the difference!"
PRINT "                 x is"; x '                 > 0
PRINT "1 <= x AND x <= 10 is"; 1 <= x AND x <= 10 '> 0
« Last Edit: 17. May 2019, 15:30:33 by B+ »

ZXDunny

  • Sr. Member
  • ****
  • Posts: 258
    • View Profile
Re: Basic Logic
« Reply #1 on: 17. May 2019, 15:46:38 »
Depends on the BASIC, I suppose.

Sinclair BASIC uses 0 = False, 1+ = True.

So the line 1<=x<=10 will always return true regardless of what x actually is - if 1<=x then we get 1 (true). If x=0 then we get false (0). 1 and 0 are less than 10, so it will always evaluate to true.

B+

  • Hero Member
  • *****
  • Posts: 814
    • View Profile
Re: Basic Logic
« Reply #2 on: 17. May 2019, 16:21:01 »
Oh yeah, SmallBASIC uses 1 for True too and it also returns True for
Code: [Select]
PRINT "     1 <= x <= 10  is "; 1 <= x <= 10, " not supposed to return True???"

Wether 1 <= x returns -1, 0, or 1 all those are less than 10. 

Is the Spell Checker working for you guys? it seems to have died for me on a couple of forums.

weather whether whatever what whaat whaaat :P

« Last Edit: 17. May 2019, 19:16:21 by B+ »

Aurel

  • Regular Member
  • Hero Member
  • *
  • Posts: 562
    • View Profile
Re: Basic Logic
« Reply #3 on: 17. May 2019, 20:56:13 »
My main point about this is :

That shouldn't be valid Basic

and that is TRUE

n00b

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: Basic Logic
« Reply #4 on: 18. May 2019, 00:49:58 »
It actually is valid in pretty much every programming language.  Comparisons are still numerical operators and boolean is just a value that denotes 0 and NOT 0.

Aurel

  • Regular Member
  • Hero Member
  • *
  • Posts: 562
    • View Profile
Re: Basic Logic
« Reply #5 on: 18. May 2019, 07:50:54 »
Boolean is RESULT of comparison expression..
if we can call such a operation expression.

ZXDunny

  • Sr. Member
  • ****
  • Posts: 258
    • View Profile
Re: Basic Logic
« Reply #6 on: 18. May 2019, 12:22:09 »
So you're saying that "=", "<=", "<>" etc should not return a numerical value, yes? So what should they return, and how should it be represented in memory? As something other than a value? If so, I have some very bad news for you.

Aurel

  • Regular Member
  • Hero Member
  • *
  • Posts: 562
    • View Profile
Re: Basic Logic
« Reply #7 on: 18. May 2019, 21:13:24 »
If so, I have some very bad news for you.

What kind of bad news?

Paul
I just confirm what man above stated.
and that shouldn't be valid BASIC statement
print 1 > 2 -> show  -1 (true)

what a heck is that in BASIC world...
Of course that i understand what that mean but is stupid..

should be ;( with more sense ..with boolean type )

print BOOL(1>2) ->  -1

ZXDunny

  • Sr. Member
  • ****
  • Posts: 258
    • View Profile
Re: Basic Logic
« Reply #8 on: 19. May 2019, 00:26:24 »
If so, I have some very bad news for you.

What kind of bad news?

In computing, everything is a number. Including booleans.

Quote
Paul
I just confirm what man above stated.
and that shouldn't be valid BASIC statement
print 1 > 2 -> show  -1 (true)

what a heck is that in BASIC world...
Of course that i understand what that mean but is stupid..

should be ;( with more sense ..with boolean type )

print BOOL(1>2) ->  -1

But a boolean is just a number - usually 1 or 0 for true or false (or in some dialects it's all bits set, so $FF if 8bit etc which yields -1 when viewed as a signed value) so the above statement is perfectly valid. But people who don't understand how a CPU works under the hood will often be surprised that 1<=x<=10 will always result in a true (non-zero) value.

I was puzzled that you said it shouldn't be valid BASIC. Why not?

Aurel

  • Regular Member
  • Hero Member
  • *
  • Posts: 562
    • View Profile
Re: Basic Logic
« Reply #9 on: 19. May 2019, 08:23:26 »
Because is stupid and unlogical and as i show should be typed
As Boolean .

In computing, everything is a number. Including booleans.

Of course that i know that..  ::)

ZXDunny

  • Sr. Member
  • ****
  • Posts: 258
    • View Profile
Re: Basic Logic
« Reply #10 on: 19. May 2019, 16:53:25 »
Because is stupid and unlogical and as i show should be typed
As Boolean .

In computing, everything is a number. Including booleans.

Of course that i know that..  ::)

I was wondering if you did, given what you're posted here tbh. So you want a new type to be created, "Boolean" which all BASICs should be using instead of numerical equivalents? How would we represent these at the assembly language level? How would we handle "LET a=(b>c)"? Would that particular case be disallowed, as a numeric variable cannot hold a boolean value, or would the variable (a) be converted to hold a boolean value? Would we allow users to cast booleans as integers? And how would they be converted?

B+

  • Hero Member
  • *****
  • Posts: 814
    • View Profile
Re: Basic Logic
« Reply #11 on: 19. May 2019, 17:50:10 »
If so, I have some very bad news for you.

What kind of bad news?

Paul
I just confirm what man above stated.
and that shouldn't be valid BASIC statement
print 1 > 2 -> show  -1 (true)

what a heck is that in BASIC world...
Of course that i understand what that mean but is stupid..

should be ;( with more sense ..with boolean type )

print BOOL(1>2) ->  -1

What a heck!?!?
1>2 is False = 0
Is there a Basic that returns True?
« Last Edit: 19. May 2019, 17:56:31 by B+ »

Aurel

  • Regular Member
  • Hero Member
  • *
  • Posts: 562
    • View Profile
Re: Basic Logic
« Reply #12 on: 19. May 2019, 19:36:46 »
Yo Mark yes i made mistake because of stupid talk about things we know.
Both me and Paul and i don't know why he insist on this .. :o

This BOOL or BOOLEAN already exists in Oxygen Basic compiler
and i am not sure in some others too (maybe VB)
so....what a heck is this time with you ?
So i must repeat again ...I just agree with the statement above..
and your insinuation that i don't know or don't understand is really silly.. ;D
and you UNDERSTAND

Mark yeah

print BOOL(1<2)  -> TRUE or -1( on Windows) or if author of progrmming language decide
can be 1.
Also you can easy change booleans to integers because are integers ... 0,1
I have used many times so called boolean logic to confirm TRUE or FALLSE.

simple example in o2
both functions are valid without any problem ,only difference is what is TRUE ( -1 or 1)
depend how you define them:

FUNCTION isdigit(S AS STRING) AS INT
     If (asc(S)>47 And asc(S)<58) Or asc(S)=46
     Return 1
     End if
     Return 0
END FUNCTION

FUNCTION isdigit(S AS STRING) AS BOOL
     If (asc(S)>47 And asc(S)<58) Or asc(S)=46
     Return TRUE
     End if
     Return FALSE
END FUNCTION 

..so ZXdunny do you maybe need compiled example ;D
pih..

Aurel

  • Regular Member
  • Hero Member
  • *
  • Posts: 562
    • View Profile
Re: Basic Logic
« Reply #13 on: 19. May 2019, 19:58:40 »
By the way when we "talk" Dunny
long time i don't visit your site/forum ...
where are all that people  :o
last post 2018.....

hmmm same thing everywhere...
it looks to me that all gone
maybe are on qb64 forum  ;D

ZXDunny

  • Sr. Member
  • ****
  • Posts: 258
    • View Profile
Re: Basic Logic
« Reply #14 on: 19. May 2019, 21:06:03 »
Trust me, Aurel, I know how booleans work - I really wouldn't be spending most of my days writing complex audio processing software if I didn't :D

As for the forum - we've moved to other places now. There's SpecBAS on Discord, Facebook, Twitter, IRC as well as web sub-forums integrated into various retro places. I don't work on SpecBAS as much as I used to because work generally takes precedent, but I do commit new changes to the repo from time to time. I'm also getting started on my older projects such as ZXSpin and BASin which badly need to be updated to work in Windows 10 these days.