A True False conundrum [Solved]

General FreeBASIC programming questions.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: A True False conundrum [Solved]

Post by MrSwiss »

caseih wrote: Maybe we need an IsNot operator.
Maybe the IsFalse() single line Macro, does the trick? (instead of: IsNot() ...)

Code: Select all

#Define IsTrue(e)   ( CBool(e) )
#Define IsFalse(e)  ( Not CBool(e) )

Dim As Double   d1 = 1.257
Dim As Single   s1 = 3.333
Dim As Long     l1 = 1023, l2 = 321

' check expression evaluation is working (before conv.)
Print  : Print Str(d1 * l1 / (s1 + l2)), Str(l1 \ l2 + (d1 - s1))
Print IsTrue(d1 * l1 / (s1 + l2)), IsFalse(d1 * l1 / (s1 + l2)), _
        IsTrue(l1 \ l2 + (d1 - s1)), IsFalse(l1 \ l2 + (d1 - s1))
d1 = 2.5 : s1 = 1.25
' is there a difference between -1 / +1
Print  : Print Str((d1 / s1) * -.5), Str((d1 / s1) * -.5), _
        Str((d1 / s1) * .5), Str((d1 / s1) * .5)
Print IsTrue((d1 / s1) * -.5), IsFalse((d1 / s1) * -.5), _
        IsTrue((d1 / s1) * .5), IsFalse((d1 / s1) * .5)
' test the 3 possible boolean values (+1 is in mem. representation)
Print : Print Str(0), Str(0), Str(-1), Str(-1), Str(1),Str(1)
Print IsTrue(0), IsFalse(0), IsTrue(-1), IsFalse(-1), IsTrue(1), IsFalse(1)

Sleep
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: A True False conundrum [Solved]

Post by caseih »

Yes that is what PB did to get around this problem.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: A True False conundrum [Solved]

Post by MrSwiss »

caseih wrote:Yes that is what PB did to get around this problem.
Well, my days with PB (PB-DOS, ver. 2.1), are too far in the past,
to remember, how things where done, in PowerBasic.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: A True False conundrum [Solved]

Post by Munair »

I know this has been fixed, but just a remark. It looks like the compiler 1.05 uses double standards. If I compile the code from the first post printing the initial value, it clearly outputs "TRUE" but the first test still fails when explicitly comparing to TRUE. Apparently it doesn't fail when comparing to non-zero. Likewise, PRINT seems to output TRUE if non-zero, or 00000001 versus 11111111 versus 10000000.

Code: Select all

Sub Test()
	Static FirstInstance as boolean = true
	print FirstInstance ' output: TRUE
	'If FirstInstance = true Then ' not TRUE
	If FirstInstance Then ' TRUE (non-zero)
		Print "First Instance"
		FirstInstance = false
	Else
		Print "Second Instance"
	End If
End Sub
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: A True False conundrum [Solved]

Post by fxm »

This bug is now fixed in the rev 1.06:
- boolean initializer values for global or static variables were not emitted properly

Highlighting:

Code: Select all

Dim As Boolean bt = True
Dim As Boolean bf = False
Static As Boolean sbt = True
Static As Boolean sbf = False

Print *Cast(Byte Ptr, @bt), *Cast(Byte Ptr, @bf)
Print *Cast(Byte Ptr, @sbt), *Cast(Byte Ptr, @sbf)

Sleep
- Compiled with rev 1.05:

Code: Select all

 1             0
-1             0
- Compiles wuth rev 1.06:

Code: Select all

 1             0
 1             0
This explains the observed behavior:
Munair wrote:I know this has been fixed, but just a remark. It looks like the compiler 1.05 uses double standards. If I compile the code from the first post printing the initial value, it clearly outputs "TRUE" but the first test still fails when explicitly comparing to TRUE. Apparently it doesn't fail when comparing to non-zero. Likewise, PRINT seems to output TRUE if non-zero, or 00000001 versus 11111111 versus 10000000.
Remark:
' *Cast(Byte Ptr, @boolean) ' allows to access the actual value of the associated BYTE under the hood.
' Cast(Byte, boolean) ' would be just a translation in an equivalent BYTE of the state of the boolean.
Post Reply