Boolean Data Type in freebasic

For other topics related to the FreeBASIC project or its community.
MrSwiss
Posts: 3079
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Postby MrSwiss » Jul 29, 2015 10:31

coderJeff wrote:However, some suggested only certain operators should be allowed to be used with booleans. OK, I am trying that out in jayrm/fbc/boolean2 branch. If some operators are not allowed with boolean (e.g. math operators), then yes, error would be generated, unless explicit conversion (e.g. CINT()) is used.

I'd also go with the suggested change (from dkl) to boolean return on shortcut operators AndAlso / OrElse.

coderJeff wrote:I get the impression that users are willing to try out a work in progress if it's available in a package, and less likely to build it from sources (even less likely to build from an alternate branch).

The reason is NOT the "willing" part, rather the "being able to" part. Not everybody feels him-/her-self capable to be able to do so.
Most of us are not exactly compiler specialists.
marcov
Posts: 2750
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: Boolean Data Type in freebasic

Postby marcov » Jul 29, 2015 14:00

St_W wrote:
marcov wrote:Doing multiple times is pretty Standard [...]

I've adapted my build jobs accordingly to build fbc two times for each new build in the future (except FreeBSD, which is a bit special). The process is a bit easier for fbc than your description for FPC, because the fbc's rtlib is written in C. The build now works like this:
1. Build rtlib, gfxlib2
2. Build Compiler using old/stable fbc Version
3. Build the Compiler again using the previous fbc build (step 2)
I hope that helps avoiding bootstrap issues in the future.


Strange. I don't see rtlib or gfxlib2 rebuilt with the newly generated compiler. Or do they only contain C parts ?

Is there no FB library code in those at all?
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: Boolean Data Type in freebasic

Postby Tourist Trap » Jul 29, 2015 14:35

fxm wrote:- for '-8*(a=b)', the boolean (a=b) is implicitly converted to integer before multiplying

That is exactly what is expected. Just keep it done like this.
However you've tested for a, b as boolean, this is not what I've meant of course since I don't see what would mean true>false. Only boolean<>boolean or boolean=boolean makes sense - for me at least.

One thing really to be avoided, would be this form : -8*CInt(a=b). I do vote for implicit conversion when boolean mixed in calculation, not error, unless a compiler option set to allow numerical/non-numerical boolean compatibility - this last for rare people who may program without mixing the types.
MrSwiss
Posts: 3079
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Postby MrSwiss » Jul 29, 2015 14:50

Tourist Trap wrote:One thing really to be avoided, would be this form : -8*CInt(a=b). I do vote for implicit conversion when boolean mixed in calculation, not error, unless a compiler option set to allow numerical/non-numerical boolean compatibility - this last for rare people who may program without mixing the types.

People who know what they're doing (and they are not so rare) would never try that anyway, it simply doesn't make any sense at all.
So, I for one, would favor the forced conversion with -8*CInt(a=b), without it: compiler ERROR!
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: Boolean Data Type in freebasic

Postby Tourist Trap » Jul 29, 2015 15:16

MrSwiss wrote:So, I for one, would favor the forced conversion with -8*CInt(a=b), without it: compiler ERROR!

From my math school cursus I can remember having done boolean calculations with 0 and 1 values for true and false. Recently I've read many codes using comparison results inside some formulas like floor/ceil etc... I prefere this way to do than using macros which lack some type checking.

Whatever implicit conversion to numeric stands somewhere when it is time for the computer to perform boolean calculation.

What I can understand however, and you are right at this, is that non-zero value shouldn't anymore return anything else than -1, or +1 if this last value is choosen for 'true'.

For example what is expected is not to get any error in things like below. Errors there would be a pitty.

Code: Select all

dim a, b as boolean
dim i as integer
var r1 = a + i
typeof(r1) = typeof(i)       'expected true
var r2 = a * i
typeof(r2) = typeof(i)       'expected true
var r3 = i*(r1>r2)
typeof(r3) = typeof(i)      'really expected to be true
(...)

You can however make the compiler warn hardly at

Code: Select all

dim a as boolean = not (-123)        'non-sense
dim b as boolean = true
dim c as boolean = false
dim d as boolean = b<c                            '???
dim e as boolean = (b=c)                          'ok 
dim f as boolean = b + c                           'non-sense

However we can discuss if replacing last line as below should be valid or not :

Code: Select all

var f = b + c   '----> returns integer?

I wouldn't say too much about this below even if in any case I see no danger if it was allowed, but who knows :

Code: Select all

dim as boolean b = not(-1)  ' ----> implicit conversion from int to bool; b = false
Last edited by Tourist Trap on Jul 29, 2015 16:07, edited 1 time in total.
fxm
Posts: 8966
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Postby fxm » Jul 29, 2015 15:47

coderJeff wrote:If some operators are not allowed with boolean (e.g. math operators), then yes, error would be generated, unless explicit conversion (e.g. CINT()) is used.

You speak of explicit conversion of boolean to integer by using Cint(), but have you really added the overload function:
Declare Function CInt ( ByVal As Boolean ) As Integer
St_W
Posts: 1466
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Boolean Data Type in freebasic

Postby St_W » Jul 29, 2015 17:02

marcov wrote:
St_W wrote:[..]
1. Build rtlib, gfxlib2
2. Build Compiler using old/stable fbc Version
3. Build the Compiler again using the previous fbc build (step 2)
[..]

Strange. I don't see rtlib or gfxlib2 rebuilt with the newly generated compiler. Or do they only contain C parts ?
Is there no FB library code in those at all?

Both rtlib (FreeBasic's runtime library) and gfxlib2 (FreeBasic's graphics library) do not contain FB code and are written in C (and a few assembler codes, especially for the x86 platform). That's why (at least to my knowledge) these libs aren't dependent on the Compiler build.

MrSwiss wrote:
coderJeff wrote:I get the impression that users are willing to try out a work in progress if it's available in a package, and less likely to build it from sources (even less likely to build from an alternate branch).

The reason is NOT the "willing" part, rather the "being able to" part. Not everybody feels him-/her-self capable to be able to do so.
Most of us are not exactly compiler specialists.

Actually building fbc and the rtlib/gfxlib2 isn't that hard. I'd even see it has become quite easy thanks to dkl's build system changes some time ago. One definitely does not need to be compiler specialist - at least I can't say that about myself :-) Have a look in the wiki for step by step instructions if you are interested. But I still agree that a lot people at least do not think that they are able to build the compiler themselves.
I'd consider my nightly builds also as a convenience service for comparing different versions and eliminating the need to set up a FB build environment on every PC.
MrSwiss
Posts: 3079
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Postby MrSwiss » Jul 29, 2015 17:26

St_W wrote:... eliminating the need to set up a FB build environment on every PC

And, I might add: keep it current (C/C++ compilers and the like).
That I think, is the main concern/hindrance of most users here ...
Thanks a lot for your so called "service" it is most appreciated!
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: Boolean Data Type in freebasic

Postby D.J.Peters » Jul 29, 2015 19:40

If FB interprets BOOLEAN TRUE as -1 (the old behavior) it's wrong.

If FB follows the C++ namemangling of BOOL but gives BOOLEAN a wrong value it's useless in math expressions and linking with C++ classes.

Code: Select all

dim as BOOLEAN a
var c=3.14 *(a=FALSE)
print c
sleep
In this case c must be 3.14 not -3.14.

Joshy
dkl
Site Admin
Posts: 3206
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Boolean Data Type in freebasic

Postby dkl » Jul 29, 2015 20:05

As explained in the opening post, Boolean stores 0/1 in memory, to be compatible with gcc. Only when converting a Boolean to an integer or float you get 0/-1. I think that's the best compromise for mixing FB and C/C++ worlds.

If we have a .h file with macros or inline functions using relational operators or booleans in math expressions (instead of logic ones), it still must be translated by keeping the 0/1 vs. 0/-1 issue in mind, as always.

Code: Select all

#define FOO(x) ((x != 0) + 1) // C, same as x ? 1 : 2
#define FOO(x) ((-(x <> 0)) + 1) '' FB, same as iif(x, 1, 2)
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: Boolean Data Type in freebasic

Postby Tourist Trap » Jul 29, 2015 20:10

D.J.Peters wrote:If FB interprets BOOLEAN TRUE as -1 (the old behavior) it's wrong.

If FB follows the C++ namemangling of BOOL but gives BOOLEAN a wrong value it's useless in math expressions and linking with C++ classes.

Code: Select all

dim as BOOLEAN a
var c=3.14 *(a=FALSE)
print c
sleep
In this case c must be 3.14 not -3.14

Most of programming languages set the value for TRUE as -1. This left aside, if it was set to +1, it wouldn't be so bad.

Leave this as detail, the important thing for me is to still be able to insert directly (a>b) -that now will return a boolean- in arithmetic expression mixing numeric values with booleans, rather than CInt(a>b). This wouldn't break any rule if it is done as an implicit conversion for the programmer comfort. The same as when one can write Print 5 rather than Print Str(5) , which would be the rigor and however neglicting that end users are human.
fxm
Posts: 8966
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Postby fxm » Jul 29, 2015 20:33

dkl wrote:As explained in the opening post, Boolean stores 0/1 in memory, to be compatible with gcc. Only when converting a Boolean to an integer or float you get 0/-1.

Yes, we can check it:

Code: Select all

Dim As Boolean b = false

Print b, Cast(Byte, b), *Cast(Byte Ptr, @b)
b = true
Print b, Cast(Byte, b), *Cast(Byte Ptr, @b)

Sleep

Code: Select all

false          0             0
true          -1             1
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: Boolean Data Type in freebasic

Postby Tourist Trap » Jul 29, 2015 20:38

Other question here about ENUM. How would be possible to get booleans integrated as possible value into Enums? would it be possible at all, would it be possible only for bivalued Enums (booleans synomyms), or would it be possible as 2 special values inside a set of value (excluding then 0, -1 for the non boolean fields) making so a boolean extension from a semantic point of view?
Example :

Code: Select all

Enum _DECISION
     _true = TRUE
     _false = FALSE
     _maybe = 2
     _tomorrow = 3
     _never = -1000
End Enum '_DECISION

Unless booleans would be excluded from enums which until recently were used for similar purposes.
fxm
Posts: 8966
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Postby fxm » Jul 30, 2015 5:40

When an integer value is assigned to a boolean, the corresponding byte in memory is assigned to '0' if the integer is nul and '+1' otherwise.
=> OK for this conversion integer=>boolean.

But when the byte corresponding to a boolean is set to a value different of '0' and '+1', the boolean state is true (OK), but the conversion to an integer does not return '-1' but -byte_value.
=> NOK (IMHO) for this conversion boolean=>integer.

Code: Select all

Dim As Boolean b = 7

Print b, Cast(Byte, b), *Cast(Byte Ptr, @b)

*Cast(Byte Ptr, @b) = 7
Print b, Cast(Byte, b), *Cast(Byte Ptr, @b)

Sleep

Code: Select all

true          -1             1
true          -7             7
St_W
Posts: 1466
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Boolean Data Type in freebasic

Postby St_W » Jul 30, 2015 8:30

I think that a programmer shouldn't rely on the internal implementation at all anyway. I would define FALSE=0 and TRUE as anything <> 0.
IMHO I'd also throw errors for any operations on boolean that just don't make sense in general. For specific implementation the operator could be overload-able so that the programmer can define what should happen (instead of an error).
When the programmer wants to convert boolean to integer rather often and use it like an integer for arithmetic operations etc. he/she should probably just use Integers instead of booleans.

Return to “Community Discussion”

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests