Dark Mode On/Off

LAST UPDATED: APRIL 12, 2023

The **math library** in python has a plethora of **trigonometric functions** which are enough for performing various trigonometric calculations in just minimal lines of code. These functions can be used after importing the math module or by referencing the math library with the **dot** operator as follows:

```
math.function_name(parameter)
# or import the math module
import math
```

There are 2 helper methods that help convert values from radians to degrees and vice-versa so that you can check the value in any format you want.

: This method converts the value passed to it from radians to degrees.**degrees(value)**

: This method converts the value passed to it from degrees to radians.**radians(value)**

__ Quick Fact:__ All the trigonometric functions in Python assume that the input angle is in terms of

`radians`

.Also, as we study mathematics, `pi/2`

**90 degrees** and `pi/3`

is **60 degrees**, the **math** module in python provides a `pi`

constant that represents `pi`

which can be used with the trigonometric function.

Trigonometric functions |
Description |

math.cos() | It returns the cosine of the number (in radians). |

math.sin() | It returns the sine of the number (in radians). |

math.tan() | It returns the tangent of the number in radians. |

math.acos() | It returns the arc cosine of the number in radians. |

math.asin() | It returns the arc sine of the number in radians. |

math.atan() | It returns the arc tangent of the number in radians. |

In the code example below, we have used the `degrees()`

and `radians()`

methods,

```
import math
print(math.degrees((math.pi/2)))
print(math.radians(60))
```

__Output__:

90.0 1.0471975511965976

Now let's see the various trigonometric functions in action, one by one.

`sin(x)`

FunctionThis function returns the **sine** of the value which is passed (x here). The input `x`

should be an **angle** mentioned in terms of **radians** (pi/2, pi/3/ pi/6, etc).

`cos(x)`

FunctionThis function returns the **cosine** of the value passed (x here). The input `x`

is an angle represented in **radians**.

`tan(x)`

FunctionThis function returns the **tangent** of the value passed to it, i.e **sine/cosine** of an angle. The input here is an angle in terms of **radians**.

`sin`

, `cos`

, and `tan`

:Below we have a simple code example for the 3 trigonometric functions defined above,

```
import math
print(math.sin(math.pi/3)) #pi/3 radians is converted to 60 degrees
print(math.tan(math.pi/3))
print(math.cos(math.pi/6))
```

**Output:**

0.8660254037844386 1.7320508075688767 0.8660254037844387

`asin(x)`

FunctionThis function returns the **inverse of the sine**, which is also known as the **arc sine of a complex number**. The input is in terms of radians and should be within the range **-1 to 1**. It returns a **floating-point number as output**.

`acos(x)`

FunctionThis function returns the **cosine inverse** of the parameter `x`

in radians. This is also known as the **arc cosine of a complex number**. The value of `x`

should be within the range of **-1 and 1**. It returns a floating-point number as output.

`atan(x)`

FunctionThis function returns the **inverse of the tangent**, in radians. It is known as the **arc tangent of the complex number**. The parameter's value lies within **-1 and 1**. It can also be represented as `(math.sin(math.pi / 4)) / (math.cos(math.pi / 4))`

. It returns a floating-point number as output.

`asin`

, `acos`

, and `atan`

with angles in radians:```
import math
print(math.asin(1))
print(math.acos(0))
print(math.atan(1))
```

**Output:**

1.5707963267948966 1.5707963267948966 0.7853981633974483

The below example depicts how the `asin`

, `acos `

and `atan `

functions can be used with complex numbers, which have the format `x+iy`

.

```
import cmath
x = 1.0
y = 1.0
z = complex(x,y)
print ("The arc sine is: ",cmath.asin(z))
print ("The arc cosine is: ",cmath.acos(z))
print ("The arc tangent is: ",cmath.atan(z))
```

**Output:**

The arc sine is : (0.6662394324925153+1.0612750619050357j) The arc cosine is : (0.9045568943023814-1.0612750619050357j) The arc tangent is : (1.0172219678978514+0.40235947810852507j)

`sinh(x)`

FunctionThis method returns the **hyperbolic sine of the angle** of the complex number that is passed.

`cosh(x)`

FunctionThis method returns the **hyperbolic cosine of the angle** of the complex number that is passed.

`tanh(x)`

FunctionThis method returns the **hyperbolic tangent of the angle** of the complex number that is passed.

`sinh`

, `cosh`

, and `tanh `

with complex numbers:```
import cmath
x = 1.0
y = 1.0
z = complex(x,y)
print ("The hyperbolic sine is : ",cmath.sinh(z))
print ("The hyperbolic cosine is : ",cmath.cosh(z))
print ("The hyperbolic tangent is : ",cmath.tanh(z))
```

**Output:**

The hyperbolic sine is : (0.6349639147847361+1.2984575814159773j) The hyperbolic cosine is : (0.8337300251311491+0.9888977057628651j) The hyperbolic tangent is : (1.0839233273386946+0.2717525853195118j)

`asinh(x)`

FunctionIt returns the **inverse of the hyperbolic sine** of an angle/complex number.

`acosh(x)`

FunctionIt returns the **inverse of the hyperbolic cosine** of an angle/complex number.

`atanh(x)`

FunctionIt returns the **inverse of the hyperbolic tangent** of an angle/complex number.

`asinh`

, `acosh`

, and `atanh `

methods with complex numbers:```
import cmath
x = 1.0
y = 1.0
z = complex(x,y)
print ("The inverse hyperbolic sine is : ",cmath.asinh(z))
print ("The inverse hyperbolic cosine is : ",cmath.acosh(z))
print ("The inverse hyperbolic tangent is : ",cmath.atanh(z))
```

**Output:**

The inverse hyperbolic sine is : (1.0612750619050357+0.6662394324925153j) The inverse hyperbolic cosine is : (1.0612750619050357+0.9045568943023813j) The inverse hyperbolic tangent is : (0.40235947810852507+1.0172219678978514j)

`atan2(y, x)`

FunctionThis translates to `atan2(y/x)`

wherein both y and x are numeric values. It returns a value that lies between `–pi`

and `pi`

. This represents the angle between the positive x-axis and the coordinates (x,y).

`print(math.atan2(1.2,1.3))`

**Output: **

0.7454194762741583

`hypot(x,y)`

FunctionThis function returns the **hypotenuse**, i.e the **Euclidean norm**. This means it returns the distance between the origin and the points (x,y). This indicates the length of the vector in the 2-D space. The Euclidean norm is also known as the ‘magnitude’ of the vector. This is the numerical value of the vector (since vectors have magnitude and direction).

If more than 2 arguments are passed, it gracefully returns a `TypeError`

.

`print(math.hypot(2,2))`

**Output: **

2.8284271247461903

** NOTE: **When these values are compared to the numeric values of the angles, there might be a few decimal place differences, which can be safely ignored. For example,

`tan(pi/3)`

converts to `sqrt(3)`

. If the `tan`

function is used, the output is `sqrt(3)`

gives

Well, you are absolutely right. There are two kinds of exceptions that could potentially occur due to the wrong usage of these methods:

**TypeError**: This kind of error occurs when a value that is not a number is passed as a parameter to one of the trigonometric methods.

**ValueError**: This error occurs when an invalid value/parameter is passed to the method.

```
import math
# It is being passed as a string rather than a number,
# hence the error.
print(math.sin('2'))
# Inverse of the sine angle can be found for parameters
# which are in radians only and inverse of sine of an integer
# is mathematically undefined, leading to the error
print(math.asin(5))
```

**Output:**

TypeError: must be real number, not str ValueError: math domain error

We learned about how various trigonometric methods could be used by importing the **math** module in python. Remember to always pass the parameter in `radians`

or convert the `degrees`

into `radians`

and then pass it as a parameter.

Python has a wide range of Trigonometric functions like math.cos(), math.sine() that can be used to calculate the Trigonometry terms like these. Read the complete article to know how you can use Trigonometric functions in Python.

math.cos() function in Python can give the cosine of a number.

tan() function is used to to calculate the tangent of a number.

Advertisement