Previous Page Next Page Contents

bool -- Boolean evaluation

Introduction

bool(b) evaluates the Boolean expression b.

Call(s)

bool(b)

Parameters

b - a Boolean expression

Returns

TRUE, FALSE, or UNKNOWN.

Overloadable:

b

Related Functions

_lazy_and, _lazy_or, FALSE, if, is, repeat, TRUE, UNKNOWN, while

Details

Example 1

MuPAD realizes that 1 is less than 2:

>> 1 < 2 = bool(1 < 2)
                              (1 < 2) = TRUE

Note that bool can only compare real numbers of syntactical type Type::Real:

>> bool(PI < 2 + sqrt(2))
      Error: Can't evaluate to boolean [_less]

One can compare floating point approximations. Alternatively, one can use is:

>> bool(float(PI) < float(2 + sqrt(2))), is(PI < 2 + sqrt(2))
                                TRUE, TRUE

Example 2

The Boolean operators and, or, not do not evaluate equations and inequalities logically, and return a symbolic Boolean expression. Boolean evaluation and simplification is enforced by bool:

>> a = a and 3 < 4
                              a = a and 3 < 4
>> bool(a = a and 3 < 4)
                                   TRUE

Example 3

bool handles the special Boolean constant UNKNOWN:

>> bool(UNKNOWN and 1 < 2), bool(UNKNOWN or 1 < 2),
   bool(UNKNOWN and 1 > 2), bool(UNKNOWN or 1 > 2)
                       UNKNOWN, TRUE, FALSE, UNKNOWN

Example 4

bool must be able to reduce all parts of a composite Boolean expression to one of the Boolean constants. No symbolic Boolean subexpressions may be involved:

>> b := b1 and b2 or b3: bool(b)
      Error: Can't evaluate to boolean [bool]
>> b1 := 1 < 2: b2 := x = x: b3 := FALSE: bool(b)
                                   TRUE
>> delete b, b1, b2, b3:

Example 5

There is no need to use bool explicitly in the conditional parts of if, repeat, and while statements. Note, however, that these structures internally use ``lazy evaluation'' via _lazy_and and _lazy_or rather than ``complete Boolean evaluation'' via bool:

>> x := 0: if x <> 0 and sin(1/x) = 0 then 1 else 2 end
                                     2

In contrast to ``lazy evaluation'', bool evaluates all conditions. Consequently, a division by zero occurs in the evaluation of sin(1/x) = 0:

>> bool(x <> 0 and sin(1/x) = 0)
      Error: Division by zero
>> delete x:

Example 6

Expressions involving symbolic Boolean subexpressions cannot be processed by bool. However, simplify with the option logic can be used for simplification:

>> (b1 and b2) or (b1 and (not b2)) and (1 < 2)
                   b1 and b2 or b1 and not b2 and 1 < 2
>> simplify(%, logic)
                                    b1

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000