# MATLAB Programming/Boolean and Rational

## Introduction[edit]

A large number of MATLAB's functions are operations on two types of numbers: *rational* numbers and *boolean* numbers.

Rational numbers are what we usually think of when we think of what a number is. 1, 3, and -4.5 are all rational numbers. MATLAB stores rational numbers as *doubles* by default, which is a measure of the number of decimal places that are stored in each variable and thus of how accurate the values are. Note that MATLAB represents irrational numbers such as pi with rational approximations, except when using the symbolic math toolbox. See that section for details.

Boolean numbers are either "TRUE" or "FALSE", represented in MATLAB by a 1 and a 0 respectively. Boolean variables in MATLAB are actually interchangable with doubles, in that boolean operators can be performed with arrays of doubles and vice versa. Any non-zero number in this case is considered "TRUE".

Most of the rational operators also work with complex numbers. Complex numbers; however, cannot be interchanged with boolean values like the real rationals can.

## Rational Operators on Single Values[edit]

MATLAB has all the standard rational operators. It is important to note, however, that *Unless told otherwise, all rational operations are done on entire arrays, and use the matrix definitions.* Thus, even though for now we're only talking about operations on a single value, when we get into arrays, it will be important to distinguish between matrix and componentwise multiplication, for example.

Add, Subtract, multiply, divide, exponent operators:

```
%addition
a = 1 + 2
%subtraction
b = 2 - 1
%matrix multiplication
c = a * b
%matrix division (pseudoinverse)
d = a / b
%exponentiation
e = a ^ b
```

The modulo function returns the remainder when the arguments are divided together, so a modulo b means the remainder when a is divided by b.

```
%modulo
remainder = mod(a,b)
```

All of these functions except for the modulus work for complex numbers as well.

## Relational Operators[edit]

Equality '==' returns the value "TRUE" (1) if both arguments are equal. This must not be confused with the assignment operator '=' which assigns a value to a variable.

```
>> %relational
>>a=5;b=5;
>>a==b
ans = 1
%Assignment
>>a=5;b=3;
>>a=b
a = 3
```

Note that in the first case, a value of 1 (true) is returned, however for the second case a gets assigned the value of b.

Greater than, less than and greater than or equal to, less than or equal to are given by >, <, >=, <= respectively. All of them return a value of true or false. Example:

```
>>a=3;b=5;
>>a<=b
ans = 1
>>b<a
ans = 0
```

## Boolean Operators on Single Values[edit]

The boolean operators are & (boolean AND) | (boolean OR) and ~ (boolean NOT /negation). A value of zero means false, any non-zero value (usually 1) is considered true.

Here's what they do:

```
>>%boolean AND
>> y = 1 & 0
y = 0
>> y = 1 & 1
y = 1
>>%boolean OR
>> y = 1 | 0
y = 1
>> y = 1 | 1
y = 1
```

The negation operation in MATLAB is given by the symbol ~, which turns any FALSE values into TRUE and vice versa:

```
>> c = (a == b)
c = 1
>> ~c
ans = 0
```

This is necessary because conditionals (IF/SWITCH/TRY) and loops (FOR/WHILE) always look for statements that are TRUE, so if you want it to do something only when the statement is FALSE you need to use the negation to change it into a true statement.

The NOT operator has precedence over the AND and OR operators in MATLAB unless the AND or OR statements are in parenthesis:

```
>> y = ~1 & 0
y = 0
>> y = ~(1&0)
y = 1
```

## Terminology[edit]

MATLAB refers to Booleans as "logicals" and does not use the word "Boolean" in code or documentation.