Menu

Boolean

Hugh Greene

In computer science, a boolean data type has two
values (usually denoted true and false), to indicate whether
whatever it is evaluating is true or not. For instance, the expression
(1 > 2) evaluates to the boolean value false.

Boolean values are especially useful in their use with
if statements, which allow you to conditionally execute
a piece of code only if the expression being evaluated is true, and
(optionally) conditionally execute another piece of code if the
expression is false.

Data Type

In EDL and C++, boolean values may be efficiently stored in the bool
data type.

Alternatively, due to their numerical representation, they may also be
stored in any other numeric data type, such as int, or GM's
traditional variant data type, var (implied when no data type is
specified). These storage methods are generally less efficient for a
boolean value.

Attempting to store a boolean in a string will result in an error.
Instead, you should use the string function to
convert it first.

bool a = (2 > 1); //true
int b = (2 > 1); //true, or 1
var c = (2 > 1); //1.0
d = (2 > 1); //same as above (var)
string e = string(2 > 1); //"1"

Value

In GM and ENIGMA, the constants true and
false are numerically defined for you:

  • true = 1
  • false = 0

Meaning that you can use true and 1 interchangeably, and likewise with
false and 0.

Additional values

In GM and ENIGMA, other values may evaluate as true or false, especially
when used in an if statement.

  • In GM, any value > 0.5 evaluates to true. This means -1 and 0.1
    both evaluate to false.
  • In ENIGMA, any non-zero value evaluates to true. This means -1 and
    0.1 both evaluate to true.
  • In GM, non-numeric values, such as strings, can not directly
    evaluate to boolean. Attempting to do so will result in an error.
  • In ENIGMA, any non-null variant string or char* evaluates to
    true, and any null variant string or char* evaluates to
    false. Other non-numeric datatypes, like std::string, which is
    commonly returned from some functions, will error unless they are
    converted to a variant or other numeric datatype first.

Generally, you should avoid evaluating strings and non-numeric values
(or even non-boolean values, for that matter) as boolean. Instead, you
should use the appropriate operators (== and !=)
and string-handling functions (and
comparison operators, such as \< and >=, for numeric values). For
example, the following script could be used to convert "", "0", and
"false" into false, and any other string value into true:

if (isreal(argument0)) return argument0;
return (argument0 != "" and argument0 != "0" and argument0 != "false");

Related

Wiki: Constant
Wiki: False
Wiki: True

MongoDB Logo MongoDB