Boolean Data Type in freebasic

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Post by fxm »

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
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Post by fxm »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Post by MrSwiss »

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
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Post by fxm »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Post by MrSwiss »

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
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Post by fxm »

It's working for me (Win32)!
(see my post: http://www.freebasic.net/forum/viewtopi ... 22#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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Post by MrSwiss »

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
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Post by fxm »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Post by MrSwiss »

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: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Boolean Data Type in freebasic

Post by dodicat »

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: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: Boolean Data Type in freebasic

Post by speedfixer »

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: 1755
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Boolean Data Type in freebasic

Post by SARG »

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: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Boolean Data Type in freebasic

Post by St_W »

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
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Post by fxm »

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: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Boolean Data Type in freebasic

Post by D.J.Peters »

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
Post Reply