# MYSQL Arithmetic

## Trigonometry (SIN, COS)

Angles are in Radians, not Degrees. All computations are done in IEEE 754 64-bit floating point. All floating point computations are subject to small errors, known as machine ε (epsilon) errors, so avoid trying to compare them for equality. There is no way to avoid these errors when using floating point; they are built in to the technology.

If you use DECIMAL values in trigonometric computations, they are implicitly converted to floating point, and then back to decimal.

#### Sine

Returns the sine of a number X expressed in radians

`SELECT SIN(PI()); -> 1.2246063538224e-16`

#### Cosine

Returns the cosine of X when X is given in radians

`SELECT COS(PI()); -> -1`

#### Tangent

Returns the tangent of a number X expressed in radians. Notice the result is very close to zero, but not exactly zero. This is an example of machine ε.

`SELECT TAN(PI()); -> -1.2246063538224e-16`

#### Arc Cosine (inverse cosine)

Returns the arc cosine of X if X is in the range -1 to 1

```
SELECT ACOS(1); -> 0
SELECT ACOS(1.01); -> NULL
```

#### Arc Sine (inverse sine)

Returns the arc sine of X if X is in the range -1 to 1

`SELECT ASIN(0.2); -> 0.20135792079033`

#### Arc Tangent (inverse tangent)

ATAN(x) returns the arc tangent of a single number.

`SELECT ATAN(2); -> 1.1071487177941`

ATAN2(X, Y) returns the arc tangent of the two variables X and Y. It is similar to calculating the arc tangent of Y / X. But it is numerically more robust: t functions correctly when X is near zero, and the signs of both arguments are used to determine the quadrant of the result.

Best practice suggests writing formulas to use ATAN2() rather than ATAN() wherever possible.

```
ATAN2(1,1); -> 0.7853981633974483 (45 degrees)
ATAN2(1,-1); -> 2.356194490192345 (135 degrees)
ATAN2(0, -1); -> PI (180 degrees) don't try ATAN(-1 / 0)... it won't work
```

#### Cotangent

Returns the cotangent of X

`SELECT COT(12); -> -1.5726734063977`

#### Conversion

```
SELECT RADIANS(90) -> 1.5707963267948966
SELECT SIN(RADIANS(90)) -> 1
SELECT DEGREES(1), DEGREES(PI()) -> 57.29577951308232, 180
```