Calling all Programmers and Builders of Strategies –
Do you know what’s “true”? Are you sure?
When testing to see if something is “true” vs. “false” (in Opto 22 products, and elsewhere when programming in general), keep in mind that an integer representation of “false” is pretty consistently 0, but “true” might be 1, -1 or more generally “non-zero.”
What’s the best way to make your code as robust as possible?
(Hint: you don’t need to test for both 1 and -1.)
In a Condition block, use the “Variable True?” command, rather than “Equal?” to -1 or 1.
In a Script block, there’s a few options. You might have code like this:
if (bFlag == -1) then
// do something here
endif
That can be simplified to just the following. There’s no reason to do a comparison. The integer automatically gets resolved down to either a zero or non-zero value.
if (bFlag) then
// do something here
endif
If that’s not clear enough, another option is the following.
if (IsVariableTrue(bFlag)) then
// do something here
endif
FYI, PAC Display now lets you pick what true means to you: -1 (the default) or +1.
Also, when comparing float values to 0.0, remember that you could get very very close to zero but not exactly. So testing to see if a float is equal to zero is not good.
Instead, evaluate the absolute value of the difference between the floats you’re comparing. For more info on comparing floats and other issues related to storing floating-point values in a binary-based computer, see form 1755: Using Floats Technical Note.
Happy coding!
-OptoMary