Whats the PB equivalent for boolean

Just starting out? Need help? Post your questions and find answers here.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Whats the PB equivalent for boolean

Post by Demivec »

Lewis wrote:Sure, although this doesn't contribute much to the TRUE/FALSE thread (for what that academic conversation is worth).
@Lewis: The thread topic actually relates to what PB variable type represents a boolean. The answers which were given were that byte, word, long, quad, and integer could be used.

The academic part, which I believe was started by you, doesn't really relate to the thread. I admit I departed from the thread topic a little when I was responding to the ideas you stated as being important.
:roll: Very observant of you, blueznl! :) However, the value not-zero is impossible to use directly in calculations, unlike a TRUE value of -1, which in certain circumstances can also improve code readability. :wink: Tips: Code and run X = NOT 0, and then code and run Y = NOT -1. :idea:
I found several incompatibilities in your assumptions. The first is that IMHO Boolean types are primarily used for conditionals and not calculations. Using a Boolean type in a calculation is more a characteristic of slow computers or limited memory than sound programming strategy (though it does have its uses :) ). Second, if you try to use a Boolean type as part of a calculation it is possible to do it with TRUE being defined as either -1 or as 1 (see my previous code sample). Third, if you are coding something that uses Boolean types in calculations it is your responsibility for making sure that the dual-use of the Boolean type (as both a logic result and as a numeric type) is aligned with the way you are using it.


Fred said that he planned on making a Boolean() function that would evaluate a conditional expression and would be usable in calculations. I'm not sure what it would return for the TRUE value. As you stated, it would be important that it returns a consistent value, i.e. -1 or 1 but not both, whenever it evaluates to TRUE.
User avatar
Lewis
User
User
Posts: 47
Joined: Fri Nov 25, 2005 1:12 pm

Re: Whats the PB equivalent for boolean

Post by Lewis »

@Demivec: You seem to be missing the point! Choosing the TRUE value of 1 (or any value other than 0) and using it in calculations requires that you make this known for later reading of the code (by yourself or others). Using the true TRUE value of -1 would not require further documentation -- provided it was implemented by the programming language. That said, I've stated that my views are to be treated purely academically since PB is highly unlikely to implement them so far into its development (too many programs would need to be checked and possibly changed).

Cheers,
Lewis

PS. If you insist on getting in the last word, go ahead... :wink:
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: Whats the PB equivalent for boolean

Post by LuCiFeR[SD] »

Lewis wrote:@Demivec: You seem to be missing the point! Choosing the TRUE value of 1 (or any value other than 0) and using it in calculations requires that you make this known for later reading of the code (by yourself or others). Using the true TRUE value of -1 would not require further documentation -- provided it was implemented by the programming language. That said, I've stated that my views are to be treated purely academically since PB is highly unlikely to implement them so far into its development (too many programs would need to be checked and possibly changed).

Cheers,
Lewis

PS. If you insist on getting in the lasting word, go ahead... :wink:
Hardly takes rocket science does it?

Code: Select all

EnableExplicit
Define Switch.b,NxtLoop.i

Macro Bool(Switch)
  Switch=Switch Not Switch
EndMacro


For NxtLoop = 0 To 5 ;loop here just to show the switch works
  
  Switch = Bool(Switch) ;Execute the macro
  
  If Switch = 1 
    ;insert your code here
    Debug "true "+Str(Switch) 
    
  Else
    ;insert your code here
    Debug "false "+Str(Switch) 
    
  EndIf
  
Next
User avatar
Lewis
User
User
Posts: 47
Joined: Fri Nov 25, 2005 1:12 pm

Re: Whats the PB equivalent for boolean

Post by Lewis »

@LuCiFeR[SD]: Nice contribution, however, work-arounds are just that -- work-arounds. :)

Cheers,
Lewis
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Whats the PB equivalent for boolean

Post by Demivec »

Lewis wrote:PS. If you insist on getting in the last word, go ahead... :wink:
:wink:
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: Whats the PB equivalent for boolean

Post by LuCiFeR[SD] »

It's not a workaround though. It does exactly what is required. You could easily pull the line out of the macro and use it directly in the code.

all you need is the

Code: Select all

Switch=Switch Not Switch

If Switch = 1 
    ;insert your code here
    Debug "true "+Str(Switch) 
    
  Else
    ;insert your code here
    Debug "false "+Str(Switch) 
    
  EndIf

The only difference is the macro, which could just as easily be a procedure, improves readability. as the name just tells you what it does. use a few comments here and there to help you out and all is good with the world, no hacks, no bodges, just simple code.
Post Reply