Boolean Data Type in freebasic

For other topics related to the FreeBASIC project or its community.
fxm
Posts: 9066
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Postby fxm » Sep 14, 2015 10:58

St_W wrote:The compiler warning disappeared with the latest build (#371 win32) - thank you, dkl - , but the problem mentioned by fxm seems to be not fixed yet

Yes:
- AndAlso & OrElse return a boolean only if the two operands are boolean literals.
- If one operand at least is a boolean variable, the return is always an integer (whatever the second operand).

Compiled with 'fbc_win32_mingw_0371_2015-09-14.zip':

Code: Select all

Dim As Boolean A = false, B = true
Print false Orelse true
Print false Orelse B  '' NOK
Print A Orelse true   '' NOK
Print A Orelse B      '' NOK

Sleep

Code: Select all

true
-1
-1
-1
(yesterday, I tested this with only boolean/integer literals!)
Last edited by fxm on Sep 14, 2015 14:05, edited 1 time in total.
fxm
Posts: 9066
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Postby fxm » Sep 14, 2015 13:50

The only cases where AndAlso & OrElse should return a Boolean is when the two operands are both Boolean (literal or variable).

Compiled with 'fbc_win32_mingw_0371_2015-09-14.zip':

Code: Select all

Dim As Integer I1 = 0, I2 = -1
Dim As Boolean B1 = false, B2 = true

Print false Or true
Print false Or B2
Print B1 Or true
Print B1 Or B2
Print false Or I2
Print I1 Or true
Print I1 Or I2
Print false Or -1
Print 0 Or true
Print 0 Or -1
Print B1 Or I2
Print I1 Or B2
Print
Print false Orelse true
Print false Orelse B2    '' NOK
Print B1 Orelse true     '' NOK
Print B1 Orelse B2       '' NOK
Print false Orelse I2
Print I1 Orelse true
Print I1 Orelse I2
Print false Orelse -1
Print 0 Orelse true
Print 0 Orelse -1
Print B1 Orelse I2
Print I1 Orelse B2

Sleep

Code: Select all

true
true
true
true
-1
-1
-1
-1
-1
-1
-1
-1

true
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
Last edited by fxm on Sep 14, 2015 14:05, edited 1 time in total.
MrSwiss
Posts: 3182
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Postby MrSwiss » Sep 14, 2015 13:52

dkl wrote:cbool() on the other hand is working fine I think - it will trigger a "constant conversion overflow" warning for any values besides 0, -1, 1. That's not too bad, we have similar behavior for other types, e.g. cbyte(&hFFFF) gives this warning too.

Well, on cbyte(&hFFFF) it makes sense, but on cbool() I'd expect it to take "anything" numeric and evaluate only:
If (0 Or 0.0~00) Then false Else true ... without throwing any warnings!
Strings could simply be converted to Double before being evaluated (most versatile numeric type IMHO).

Or is there any valid reason to throw a warning? I know that a Boolean = 1 x Byte, but isn't that exactly, what casting is used for?
Converting between otherwise incompatible types?
fxm
Posts: 9066
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Postby fxm » Sep 15, 2015 7:20

fxm wrote:The only cases where AndAlso & OrElse should return a Boolean is when the two operands are both Boolean (literal or variable).

OK now with this last fix:
boolean: Fix AndAlso/OrElse to return Boolean even if not const-folded
MrSwiss
Posts: 3182
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Postby MrSwiss » Sep 15, 2015 8:28

fxm wrote:OK now with this last fix

Yes, AndAlso / OrElse is OK, but:

Code: Select all

Print B1 Or B2    '' NOK: returns integer (both are Boolean Var's.)

This indicates, that some of the AndAlso / OrElse fixes, are also needed on the "other" Boolean operators:
And, Or, Xor etc.
fxm
Posts: 9066
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Postby fxm » Sep 15, 2015 8:42

It's working for me (Win32)!
(see my post: viewtopic.php?p=211422#p211422
warning: on a text window of 25 lines without line memory, the first output line disappears)
Last edited by fxm on Sep 15, 2015 8:53, edited 1 time in total.
MrSwiss
Posts: 3182
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Postby MrSwiss » Sep 15, 2015 8:48

fxm wrote:It's working for me (Win32)!

I've tested with both 32/64 WIN, same result ... (as above).
Todays St_W builds used for testing: 2015-09-15
fxm
Posts: 9066
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Postby fxm » Sep 15, 2015 8:56

Compiled with fbc_win32_mingw_0377_2015-09-15.zip:

Code: Select all

Screen 0
Width 80, 30

Dim As Integer I1 = 0, I2 = -1
Dim As Boolean B1 = false, B2 = true

Print false Or true
Print false Or B2
Print B1 Or true
Print B1 Or B2
Print false Or I2
Print I1 Or true
Print I1 Or I2
Print false Or -1
Print 0 Or true
Print 0 Or -1
Print B1 Or I2
Print I1 Or B2
Print
Print false Orelse true
Print false Orelse B2    '' NOK
Print B1 Orelse true     '' NOK
Print B1 Orelse B2       '' NOK
Print false Orelse I2
Print I1 Orelse true
Print I1 Orelse I2
Print false Orelse -1
Print 0 Orelse true
Print 0 Orelse -1
Print B1 Orelse I2
Print I1 Orelse B2

Sleep

Code: Select all

true
true
true
true
-1
-1
-1
-1
-1
-1
-1
-1

true
true
true
true
-1
-1
-1
-1
-1
-1
-1
-1
MrSwiss
Posts: 3182
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Postby MrSwiss » Sep 15, 2015 9:08

fxm wrote:warning: on a text window of 25 lines without line memory, the first output line disappears

Yes, you're right that's been the problem ...
Now tested on larger (buffered) Console-Window, everything works as expected ... (tiny little buggers ...).
dodicat
Posts: 5886
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Boolean Data Type in freebasic

Postby dodicat » Sep 15, 2015 12:53

It depends on your Width settings.

Code: Select all

 
Screen 0
Width 80, 901

for n as integer=1 to 900
    print n,cast(boolean,n and 1),,cast(boolean,n andalso 1)
next n
sleep
 

On Win XP 32 bit screen 0 you can see all the results.
Don't know about the later Windows.
speedfixer
Posts: 366
Joined: Nov 28, 2012 1:27
Location: California

Re: Boolean Data Type in freebasic

Postby speedfixer » Sep 17, 2015 21:32

with:

FreeBASIC Compiler - Version 1.04.0 (07-28-2015), built for linux-x86 (32bit)
Copyright (C) 2004-2015 The FreeBASIC development team.

I was able to:

Code: Select all

dim as boolean ggg
ggg += 1
print ggg


without fail, warning or message.

Also, a mention in the docs that the default is FALSE might be appropriate.

David
SARG
Posts: 903
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Boolean Data Type in freebasic

Postby SARG » Sep 17, 2015 22:31

With a Iater version I got an error.....

    FreeBASIC Compiler - Version 1.04.0 (08-20-2015), built for win32 (32bit)
    Copyright (C) 2004-2015 The FreeBASIC development team.

    compiling: Dbg_test_bool.bas -o Dbg_test_bool.c (main module)
    Dbg_test_bool.bas(2) error 20: Type mismatch in 'ggg += 1'
St_W
Posts: 1468
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Boolean Data Type in freebasic

Postby St_W » Sep 18, 2015 0:08

I can confirm that the latest build (2015-09-16) also gives that error, as expected. So that issue seems to be non-existent anymore. As the git builds are usually WIP it is usually advisable to try with the latest build in general. dkl did commit quite a lot of changes in the last days and weeks.
fxm
Posts: 9066
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Postby fxm » Sep 19, 2015 12:12

speedfixer wrote:Also, a mention in the docs that the default is FALSE might be appropriate.

KeyPgDim ⇒ FxMwikki [Booleans are initialized to False by default when they are created]
(paragraph "Initializers")
D.J.Peters
Posts: 7773
Joined: May 28, 2005 3:28

Re: Boolean Data Type in freebasic

Postby D.J.Peters » Oct 13, 2015 6:43

Declare Operator Next( ByRef cond As T, ByRef stp As T ) As Integer
Declare Operator Next( ByRef cond As T, ByRef stp As T ) As Boolean
I would more prefer Boolean. :-)

Joshy

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 9 guests