×

Python math.cos and math.acos function

Math.cos() function

In Python, the Math module is used for performing the mathematical operations. It includes the math.cos() function that is used for obtaining the cosine value of an angle in radians.

Syntax:

math.cos(x)

Parameter:

x : Numeric value

Returns: Returns the cosine value of an angle.

Example:

import math 

a = math.pi / 6

# returns the cosine value

print ("cosine value of pi / 6 is: ", end ="")

print (math.cos(a))

Output:

cosine value of pi/6 is: 0.8660254037844387

Example 2:

import math

print ("cos(-1.23) : ", math.cos(-1.23))

print ("cos(3) : ", math.cos(3))

print ("cos(10) : ", math.cos(10))

print ("cos(pi/3) : ", math.cos(math.pi/3))

print ("cos(math.pi) : ", math.cos(math.pi))

print ("cos(2*math.pi) : ", math.cos(2*math.pi))

Output:

cos(-1.23) : 0.3342377271245026

cos(3) : -0.9899924966004454

cos(10) : -0.8390715290764524

cos(pi/3) : 0.5000000000000001

cos(math.pi) : -1.0

cos(2*math.pi) : 1.0

We can also obtain the graphical representation of the cos function by using the matplotlib and Numpy libraries.

Example:

import math

import numpy as np

import matplotlib.pyplot as plt

input_arr = np.linspace(-(2 * np.pi), 2 * np.pi, 20)

out_arr = []

for i in range(len(input_arr)):

    out_arr.append(math.cos(input_arr[i]))

    i += 1

print("input_arr : ", input_arr)

print("\nout_arr : ", out_arr)

plt.plot(input_arr, out_arr, color='red', marker="o")

plt.title("math.cos()")

plt.xlabel("X")

plt.ylabel("Y")

plt.show()

Output:

Python Math Cos And Math Acos Function

Python math.acos()

If we want to find the inverse of cosine then the math.acos() function is used for finding the inverse of cos.

It returns the arc cosine for the particularized expression. The value between -1 to 1 should be passed in this function.

Syntax

math.acos(x)

Parameter: Only a single parameter is accepted.

x : Numeric valuee to be passed to math.acos()

Returns: It returns arc cosine value.

Example:

import math  

a = math.pi / 4

# returns the value

print ("The value of arc cosine of pi / 4 is: ", end ="") 

print (math.acos(a))

Output:

The value of arc cosine of pi / 4 is: 0.6674572160283838

Example 2:

import math

print ("acos(0.64) : ", math.acos(0.64))

print ("acos(0) : ", math.acos(0))

print ("acos(-1) : ", math.acos(-1))

print ("acos(1) : ", math.acos(1))

print ("acos(0.75) : ", math.acos(0.75))

print ("acos(0.99) : ", math.acos(0.99))

print ("acos(pi/4) : ", math.acos(math.pi/4))

Output:

acos(0.64) : 0.8762980611683406

acos(0) : 1.5707963267948966

acos(-1) : 3.141592653589793

acos(1) : 0.0

acos(0.75) : 0.7227342478134157

acos(0.99) : 0.1415394733244273

acos(pi/4) : 0.6674572160283838

Example 3:

The arc cosine values of different data types can be found with the math.acos() function.

Now, we can understand by taking an example.

import math

Tup = (0.21, 0.12, 0.39, -0.89 , 0.42) # Declaration of tuple

Lis = [-0.1, 0.92, 0.35, -0.46 , 0.85] # Declaration of list

print('Arc Cosine value of Positive Number = %.2f' %math.acos(1))

print('Arc Cosine value of Negative Number = %.2f' %math.acos(-1))

print('Arc Cosine value of Tuple Item = %.2f' %math.acos(Tup[3]))

print('Arc Cosine value of List Item = %.2f' %math.acos(Lis[2]))

print('Arc Cosine value of Multiple Number = %.2f' %math.acos(0.10 + 0.20 - 0.40))

Output:

Arc Cosine value of Positive number = 0.00

Arc Cosine value of Negative Number x = 9.14

Arc Cosine value of Tuple Item =2*6

Arc Cosine value of List Item = 1.21

Arc Cosine value of Multiple number = 1.67

Example:

We can also obtain the graphical representation of the acos function by using the matplotlib and Numpy libraries.

import math 

import numpy as np

import matplotlib.pyplot as plt 

in_array = np.linspace(-(1 / 3.5 * np.pi), 1 / 3.5 * np.pi, 20)

out_array = []

for i in range(len(in_array)):

    out_array.append(math.acos(in_array[i]))

    i += 1

print("Input_Array : \n", in_array) 

print("\nOutput_Array : \n", out_array) 

plt.plot(in_array, out_array, "go-") 

plt.title("math.acos()") 

plt.xlabel("X") 

plt.ylabel("Y") 

plt.show()

Output:

Python Math Cos And Math Acos Function

Conclusion

In the above article, We studied Python math.cos() function and also math.acos() function and we learned how to use these two  functions in Python.


Related Topics

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

Python Permutations and Combinations

In mathematics, we all studied what is meant by permutations and combinations. “Permutations” define the way of arranging the elements in sequential order. “Combinations” mean the way of selecting the...

7 minutes read.

Python Identifiers

Identifiers in Python User-defined names are identifiers in Python that are used to name variables, functions, classes, modules, and other things. You can create Python identifiers using these rules: As an identifier...

4 minutes read.

Expressions in Python

What is Expression in Python? The expression contains more than one operator as well as the operands with it. Expression helps us to produce some other values. In the Python programming...

12 minutes read.

Math Module in Python

In this article, you are going to learn everything in detail about the “ math “ module in Python. We can normally work with general operations using python without importing or...

16 minutes read.

Python frozenset()

Python frozenset() class The frozenset() class in Python returns a new frozenset object, optionally with elements taken from iterable. Syntax class frozenset([iterable]) Parameter iterable: This parameter represents an iterable object, like list, set, tuple etc. Return This class returns an unchangeable...

1 minute read.

What does the if __name__ == "__main__" do in Python

In this article, you will learn about the If__name__==__main__ is a statement in python to define modules and the names of the modules. This statement plays a vital role in...

3 minutes read.

Spotify API in Python

The application programming interface is referred to as API. In essence, an API serves as a layer of communication or, as the name suggests, an interface that enables systems to...

3 minutes read.

Python bin() function

Python bin() function The bin() function in Python returns the binary version for the specified integer. Syntax bin(x) Parameter n: This parameter represents an integer or int value. Return This function returns a binary string for the specified integer...

1 minute read.

Python hash() function

Python hash() function The hash() function in Python returns the hash value of the object (if it has one).  Syntax hash(object) Parameter obj : This parameter represents the object which we need to convert into hash. Return This...

1 minute read.

Python String index() method

Python String index() method The string.index() method in Python finds the lower index of the first occurrence of the specified value or raise a ValueError if the substring is not found. Syntax index(sub[, start[, end]]) Parameter sub:...

2 minutes read.

How to check version of Python

How to check version of python The versions of Python come with different kinds of features and functionalities. It is not a herculean task to keep track of the updates these...

3 minutes read.

Python Simple Interest

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

3 minutes read.

Covariance in Python

Covariance is defined as the estimate of the difference of change between two variables or more variables. It defines the changes of two variables together. In Python, The covariance can...

2 minutes read.

Python sorted() function

Python sorted() function The sorted() function returns a sorted list of the specified iterable object. Syntax sorted(iterable, *, key=None, reverse=False) Parameter iterable: It is a required parameter that represents the sequence to sort, list, dictionary, tuple etc. key:...

1 minute read.

Python program to check if two strings are anagram or not

Python program to check if two strings are anagram or not Problem: This is a python program that takes two strings and checks if given strings are anagram or not. Examples: Input: string1...

1 minute read.

Python String encode() method

Python String encode() method The string.encode() method in Python returns an encoded version of the string. The default encoding is the current default string encoding Syntax String.encode([encoding[,errors]]) Parameter Encoding: This parameter represents a String specifying...

2 minutes read.

What Does the Percent Sign (%) Mean in Python?

In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand. Example: value1 = 8 value2 = 2 remainder =...

4 minutes read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

What is Python 2

Python is a widely used high-level language. The initial work on developing python was begun in the late 1980s. In 1989, Guido Van Rossum started to work on it. Initially,...

3 minutes read.