Go to the first, previous, next, last section, table of contents.

## Arithmetic Operators

The `awk` language uses the common arithmetic operators when evaluating expressions. All of these arithmetic operators follow normal precedence rules, and work as you would expect them to.

Here is a file `grades' containing a list of student names and three test scores per student (it's a small class):

```Pat   100 97 58
Sandy  84 72 93
Chris  72 92 89
```

This programs takes the file `grades', and prints the average of the scores.

```\$ awk '{ sum = \$2 + \$3 + \$4 ; avg = sum / 3
>        print \$1, avg }' grades
-| Pat 85
-| Sandy 83
-| Chris 84.3333
```

This table lists the arithmetic operators in `awk`, in order from highest precedence to lowest:

`- x`
Negation.
`+ x`
Unary plus. The expression is converted to a number.
`x ^ y`
`x ** y`
Exponentiation: x raised to the y power. `2 ^ 3' has the value eight. The character sequence `**' is equivalent to `^'. (The POSIX standard only specifies the use of `^' for exponentiation.)
`x * y`
Multiplication.
`x / y`
Division. Since all numbers in `awk` are real numbers, the result is not rounded to an integer: `3 / 4' has the value 0.75.
`x % y`
Remainder. The quotient is rounded toward zero to an integer, multiplied by y and this result is subtracted from x. This operation is sometimes known as "trunc-mod." The following relation always holds:
```b * int(a / b) + (a % b) == a
```
One possibly undesirable effect of this definition of remainder is that `x % y` is negative if x is negative. Thus,
```-17 % 8 = -1
```
In other `awk` implementations, the signedness of the remainder may be machine dependent.
`x + y`
Addition.
`x - y`
Subtraction.

For maximum portability, do not use the `**' operator.

Unary plus and minus have the same precedence, the multiplication operators all have the same precedence, and addition and subtraction have the same precedence.

Go to the first, previous, next, last section, table of contents.