Python math.cos() Method



The Python math.cos() method is used to calculate the cosine value of an angle in radians. Mathematically, the cosine function is defined as the ratio of the adjacent side to hypotenuse in a right angled triangle; and its domain can be all real numbers. This method raises a TypeError whenever we pass anything but a floating-point number as an argument to it.

Note − This function is not accessible directly, so we need to import math module and then we need to call this function using math static object.

Syntax

Following is the syntax for the Python math.cos() method −

math.cos(x)

Parameters

  • x − This must be a numeric value.

Return Value

This method returns a numeric value between -1 and 1, which represents the cosine of the angle.

Example

The following example shows the usage of the Python math.cos() method. Here, we are trying to pass the standard cosine angles and find their trigonometric cosine ratios using this method.

import math

# If the cosine angle is pi
x = 3.14
cosine = math.cos(x)
print("The cosine value of x is:", cosine)

# If the cosine angle is pi/2
x = 3.14/2
cosine = math.cos(x)
print("The cosine value of x is:", cosine)

# If the cosine angle is 0
x = 0
cosine = math.cos(x)
print("The cosine value of x is:", cosine)

When we run above program, it produces following result −

The cosine value of x is: -0.9999987317275395
The cosine value of x is: 0.0007963267107332633
The cosine value of x is: 1.0

Example

Not just the standard angles, this method can also be used to find the cosine ratios for non-standard angles.

In this example, we are creating multiple number objects that hold non-standard angles in radians. These values are passed as arguments to this method in order to find their resultant cosine ratios.

import math

# If the cosine angle is pi
x = 5.48
cosine = math.cos(x)
print("The cosine value of x is:", cosine)

# If the cosine angle is pi/2
x = 1.34
cosine = math.cos(x)
print("The cosine value of x is:", cosine)

# If the cosine angle is 0
x = 0.78
cosine = math.cos(x)
print("The cosine value of x is:", cosine)

If we compile and run the given program, the output is displayed as follows −

The cosine value of x is: 0.6944181792510162
The cosine value of x is: 0.22875280780845939
The cosine value of x is: 0.7109135380122773

Example

Even though complex numbers are still considered as numbers, this method only accepts real numbers as arguments.

Let us look at scenario where we pass complex numbers as arguments to the cos() method. The method raises a TypeError.

import math

# If the cosine angle is a complex number
x = 12-11j
cosine = math.cos(x)
print("The cosine value of x is:", cosine)

On executing the program above, the output is displayed as follows −

Traceback (most recent call last):
  File "main.py", line 5, in 
    cosine = math.cos(x)
TypeError: can't convert complex to float

Example

We can use the math.radians() method to convert an angle in degrees and pass it as an argument to the cos() method.

In the following example, we are creating a number object that holds a cosine angle in degrees. Since, the cos() method takes arguments in radians, we can call the radians() method on this object to convert it into the corresponding radian value. We will then pass this radian value as an argument to this method and find its cosine ratio.

import math

# Take the cosine angle in degrees
x = 60

# Convert it into radians using math.radians() function
rad = math.radians(x)

# Find the cosine value using cos() method
cosine = math.cos(rad)

# Display the cosine ratio
print("The cosine value of x is:", cosine)

The output for the program above is as follows −

The cosine value of x is: 0.5000000000000001
python_maths.htm
Advertisements