Open In App

Python | math.cos() function

Last Updated : 20 Mar, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.cos() function returns the cosine of value passed as argument. The value passed in this function should be in radians.

Syntax: math.cos(x)

Parameter:
x : value to be passed to cos()

Returns: Returns the cosine of value passed as argument

Code #1:




# Python code to demonstrate the working of cos()
     
# importing "math" for mathematical operations 
import math 
    
a = math.pi / 6
     
# returning the value of cosine of pi / 6 
print ("The value of cosine of pi / 6 is : ", end ="") 
print (math.cos(a)) 


Output:

The value of cosine of pi/6 is : 0.8660254037844387

 
Code #2:




# Python program showing 
# Graphical representation of 
# cos() function 
import math
import numpy as np
import matplotlib.pyplot as plt 
  
in_array = np.linspace(-(2 * np.pi), 2 * np.pi, 20)
  
out_array = []
  
for i in range(len(in_array)):
    out_array.append(math.cos(in_array[i]))
    i += 1
  
   
print("in_array : ", in_array) 
print("\nout_array : ", out_array) 
  
# red for numpy.sin() 
plt.plot(in_array, out_array, color = 'red', marker = "o"
plt.title("math.cos()"
plt.xlabel("X"
plt.ylabel("Y"
plt.show() 


Output:

in_array : [-6.28318531 -5.62179738 -4.96040945 -4.29902153 -3.6376336 -2.97624567
-2.31485774 -1.65346982 -0.99208189 -0.33069396 0.33069396 0.99208189
1.65346982 2.31485774 2.97624567 3.6376336 4.29902153 4.96040945
5.62179738 6.28318531]

out_array : [1.0, 0.7891405093963934, 0.2454854871407988, -0.40169542465296987, -0.8794737512064891, -0.9863613034027223, -0.6772815716257412, -0.08257934547233249, 0.5469481581224268, 0.9458172417006346, 0.9458172417006346, 0.5469481581224268, -0.0825793454723316, -0.6772815716257405, -0.9863613034027223, -0.8794737512064893, -0.40169542465296987, 0.2454854871407988, 0.7891405093963934, 1.0]



Similar Reads

Python - cmath.cos() function
cMath module contains a number of functions which is used for mathematical operations for complex numbers. The cmath.cos() function returns the cosine value of a complex number. The value passed in this function can be int, float, and complex numbers. Syntax: cmath.cos(x) Parameter:This method accepts only single parameters. x :This parameter is th
1 min read
Python | PyTorch cos() method
PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes.The function torch.cos() provides support for the cosine function in PyTorch. It expects the input in radian form and the output is in the range [-1, 1]. The input type is tensor and if the input cont
2 min read
Python | sympy.cos() method
In sympy, cos() method is cosine function. Using the cos(x) method in the sympy module, we can compute the cosine of x. Syntax : sympy.cos(x) Return : Returns the cosine of x Code #1: Below is the example using cos() method to find cosine function. C/C++ Code # importing sympy library from sympy import * # calling cos() method on expression geek1 =
1 min read
Python - tensorflow.raw_ops.Cos()
TensorFlow is open-source python library designed by Google to develop Machine Learning models and deep learning neural networks. TensorFlow raw_ops provides low level access to all TensorFlow operations. Cos() is used to find element wise cosine of x. Syntax: tf.raw_ops.Cos(x, name) Parameters: x: It's the input tensor. Allowed dtype for this tens
2 min read
numpy.cos() in Python
numpy.cos(x[, out]) = ufunc 'cos') : This mathematical function helps user to calculate trigonometric cosine for all x(being the array elements). Parameters : array : [array_like]elements are in radians. 2pi Radians = 360 degrees Return : An array with trigonometric cosine of x for all x i.e. array elements Code #1 : Working Python Code # Python pr
2 min read
Python math.sqrt() function | Find Square Root in Python
sqrt() function returns square root of any number. It is an inbuilt function in Python programming language. In this article, we will learn more about the Python Program to Find the Square Root. sqrt() Function We can calculate square root in Python using the sqrt() function from the math module. In this example, we are calculating the square root
3 min read
Python math function | modf()
modf() function is an inbuilt function in Python that returns the fractional and integer parts of the number in a two-item tuple. Both parts have the same sign as the number. The integer part is returned as a float. Syntax : modf(number) Time Complexity: O(1) Auxiliary Space: O(1) Parameter : There is only one mandatory parameter which is the numbe
3 min read
Python math function | hypot()
hypot() function is an inbuilt math function in Python that return the Euclidean norm, [Tex]\sqrt{(x*x + y*y)} [/Tex]. Syntax : hypot(x, y) Parameters : x and y are numerical values Returns : Returns a float value having Euclidean norm, sqrt(x*x + y*y). Error : When more than two arguments are passed, it returns a TypeError. Note : One has to impor
2 min read
Python | math.fabs() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.fabs() function returns the absolute value of the number. Syntax: math.fabs(x) Parameter: x: This is a numeric expression. Returns: the absolute value of the number. Time Complexity: O(1) Auxiliary Space: O(1) Code #1: C/C++
1 min read
Python | math.floor() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.floor() function returns the largest integer not greater than x. If number is already integer, same number is returned. Syntax: math.floor(x) Parameter: x: This is a numeric expression. Returns: largest integer not greater th
1 min read
Practice Tags :