Boolean? Or integer?

General FreeBASIC programming questions.
Post Reply
ManfredMornhinweg
Posts: 45
Joined: Feb 07, 2016 18:13

Boolean? Or integer?

Post by ManfredMornhinweg »

Hello,

I have the following line in my program:

loop until (in<>"") and (unique=true)

and this is causing a warning 38: "Mixing operand data types may have undefined results". Despite the warning, the program is working as expected, but I would prefer to make a clean program that gives no warnings.

My variable "in" is a string, while "unique" is a boolean, but I thought that the expressions on both sides of the "and" would evaluate to booleans!
Can anybody tell me where my mistake is?

Manfred
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean? Or integer?

Post by fxm »

You do not make a mistake there.
This is a bug not still corrected:
dkl wrote:Also, the warnings shouldn't appear anymore now if mixing booleans with 0/-1 (e.g. true and (-1)), or integers with true/false (e.g. 123 and true). If I remember correctly, that's how it was intended, it was just not working. This gives better backwards compatibility with code which used the old-school integer TRUE/FALSE definitions.
See the bug report:
#797 relational ops can cause "mixing bool/non-bool operands" warnings

A workaround to suppress the warning:
loop until cbool(in<>"") and (unique=true)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean? Or integer?

Post by MrSwiss »

Yep, CBool() fixes the issue, testing code:

Code: Select all

Dim As Double	start = Timer
Dim As String	in = ""
Dim As Boolean	unique = FALSE 

Do
	If (Timer - start) > 3.0 Then unique = TRUE    ' run for 3 Seconds
	in += "."	' add a Dot every run
	Print in	' show current string

	Sleep 250, 1	' give CPU a Break
Loop Until CBool(in <> "") AndAlso (unique=true)
' alternative: Loop Until unique AndAlso CBool(in <> "") ' a shorter way to check Boolean = TRUE

Print : Print "any key ends ... "
Sleep : End
Compiled with Error checking as follows:
fbc -s console -exx -w pedantic "Program_Name.bas" == NO Errors / Warnings.
ManfredMornhinweg
Posts: 45
Joined: Feb 07, 2016 18:13

Re: Boolean? Or integer?

Post by ManfredMornhinweg »

Ah, thanks!

Yes, with that change the warning doesn't show up.
When bug 797 gets fixed, I will try to remember to remove those cbool functions! Just to save some bytes and microseconds...

Manfred
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean? Or integer?

Post by fxm »

Why not use conditional compilation with an identifier corresponding to each bug not still solved?
For example using:
- #Define bugxyz
at the program top for each bug xyz.
- Once that a bug is solved in the compiler version, it is sufficient to suppress (put in comment) only the line defining the bug at the top of the program.

Example with MrSwiss's code:

Code: Select all

#Define bug797

Dim As Double   start = Timer
Dim As String   in = ""
Dim As Boolean   unique = FALSE

Do
   If (Timer - start) > 3.0 Then unique = TRUE    ' run for 3 Seconds
   in += "."   ' add a Dot every run
   Print in   ' show current string

   Sleep 250, 1   ' give CPU a Break
#Ifdef bug797
Loop Until CBool(in <> "") AndAlso (unique=true)
#Else
Loop Until (in <> "") AndAlso (unique=true)
#Endif
' alternative: Loop Until unique AndAlso CBool(in <> "") ' a shorter way to check Boolean = TRUE

Print : Print "any key ends ... "
Sleep : End
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Boolean? Or integer?

Post by counting_pine »

I have to be honest: when booleans were introduced, I expected them to be used wherever it made sense, i.e. for operations returning true/false values (comparisons, AndAlso/OrElse, and functions like EOF).
If that ever becomes the case, it wouldn't make any difference in the emitted code whether a CBool were there or not.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean? Or integer?

Post by fxm »

Now that the bug report #797 relational ops can cause "mixing bool/non-bool operands" warnings is solved by the commit Hide "mixed operands" warning if the nonbool is a relational BOP, the topic title could be tagged as "solved by fbc 1.06.0".
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Boolean? Or integer?

Post by dodicat »

I think that if milliseconds are to be saved then the order should be swapped then andalso used instead of and.
The string compare in= "" is kinda sluggish.
This is Win 32 anyhows.


Code: Select all

 
dim as boolean unique
dim as string in
dim as double t=timer
dim as integer counter
do
    counter+=1
if counter>100000000 then in=" ":unique=true
loop until  cbool(unique=true) andalso (in<>"")
print timer-t
sleep 
noop
Posts: 130
Joined: Sep 18, 2006 10:29

Re: Boolean? Or integer?

Post by noop »

To all reading this:

Do you prefer to write

Code: Select all

loop until cbool(unique = true) andalso (in <> "")
since it implies that unique is a boolean variable?

Or rather the following which is shorter (and requires no cbool()):

Code: Select all

loop until unique andAlso (in <> "")
I would probably rename unique to isUnique:

Code: Select all

loop until isUnique andAlso (in <> "")
Then the "is" part gives away that it is a boolean variable.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean? Or integer?

Post by fxm »

Code: Select all

loop until isUnique andalso len(in)>0
for optimizing speed.
Post Reply