# Math Operators

Plus (+), Minus(-), Multiplication (*), and Division (/) are defined in all programming languages. They do what you would expect.

# Math Operators

Math in a computer language is very similar to math on a calculator, or even the math you do in algebra.

## Basic Operators

The basic operations are: +, -, /, *. (plus, minus, division, multiplication). These are the same in all langauges.

Language specific Math operations are:

## Matlab

^ (the caret) is the power operator. 5 ^ 2 == 25;

mod (the modulo function). mod(5, 2) == 1 (5 divide by 2 leaves a remainder of 1)

## C, Java, ActionSript

^ (the caret) is BIT WISE AND. DO NOT USE IT (for this course);

pow (the power function). pow(5, 2) == 25 ( 5 raised to the 2nd power is 25)

% (the modulo operator). 5 % 2 == 1 (5 divide by 2 leaves a remainder of 1)

## Precedence

Precedence is the order in which a combination of mathematical operations take place. For example, if you have 5+6-7*8/9, do you do the + first, or the /, or perhaps the * or -?

Most programming languages have a list of dozens of precedence rules, but they can be summed as:

1. (Multiplication and Division) before (Addition and Subtraction)
2. Left to Right for equal precedences
3. Evaluate expressions in Parentheses before anything else!

Examples:

5 + 6 * 7 is equivalent to 5 + ( 6 * 7 ) or 47

5 + 6 - 7 is equivalent to (5 + 6) - 7 or 4

5 * (6 - 7) is equivalent to 5 * (6 - 7) or -5

## Math Function Libraries:

Almost all modern programming languages provide a "Library" of Math functions for use in Mathematical programming. These include trig functions (sin, cos, etc), max, min functions, modulo functions, etc.

## Matlab

```            ```
sin( pi );  % compute the sin of pi
cos( pi );  % compute the cos of pi
max( [1 5 3 2] ); % produces 5
min( [1 5 3 2] ); % produces 1
```
```

## ActionSript

```            ```
// Must preface Math functions with Math. (Math dot)

Math.sin( pi );  // compute the sin of pi
Math.cos( pi );  // compute the cos of pi
Math.max( [1 5 3 2] ); // produces 5
Math.min( [1 5 3 2] ); // produces 1
```
```

## The C Language

```            ```
// Must include <math.h> at the top of the file

#include <math.h>

sin( pi );  // compute the sin of pi
cos( pi );  // compute the cos of pi
max( [1 5 3 2] ); // produces 5
min( [1 5 3 2] ); // produces 1
```
```

## Integer Math vs Floating Point math

Integers are "Whole Numbers" and an operation done in the realm of whole numbers remains in the realm of whole numbers, thus 5/2 is 2.

Dividing two real numbers, e.g., 5.0 / 3.0, gives a real number

For more information on Integer and Floating Point Math, see the chapter on this topic.