Trigonometric Functions



acos() function

The acos() function returns the arc cosine of x in radians.

Syntax

Following is the syntax for acos() function −

acos(x)

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

Parameters

  • x − This must be a numeric value in the range -1 to 1. If x is greater than 1, then it will generate 'math domain error'.

Return Value

This method returns arc cosine of x, in radians. The result is between 0 and pi.

Example

The following example shows the usage of the acos() method −

from math import acos

x = 0.5
val = acos(x)
print ("x: ",x, "acos(x): ", val)

x = 0.0
val = acos(x)
print ("x: ",x, "acos(x): ", val)

x = -1
val = acos(x)
print ("x: ",x, "acos(x): ", val)

x = 1
val = acos(x)
print ("x: ",x, "acos(x): ", val)

When we run the above program, it produces the following output

x: 0.5 acos(x): 1.0471975511965979
x: 0.0 acos(x): 1.5707963267948966
x: -1 acos(x): 3.141592653589793
x: 1 acos(x): 0.0

asin() Function

The asin() function returns the arc sine of x (in radians).

Syntax

Following is the syntax for the asin() function −

asin(x)

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

Parameters

  • x − This must be a numeric value in the range -1 to 1. If x is greater than 1, then it will generate 'math domain error'.

Return Value

This method returns arc sine of x, in radians. The result is between -pi/2 and pi/2.

Example

The following example shows the usage of the asin() method.

from math import asin

x = 0.5
val = asin(x)
print ("x: ",x, "asin(x): ", val)

x = 0.0
val = asin(x)
print ("x: ",x, "asin(x): ", val)

x = -1
val = asin(x)
print ("x: ",x, "asin(x): ", val)

x = 1
val = asin(x)
print ("x: ",x, "asin(x): ", val)

When we run the above program, it produces the following output

x: 0.5 asin(x): 0.5235987755982989
x: 0.0 asin(x): 0.0
x: -1 asin(x): -1.5707963267948966
x: 1 asin(x): 1.5707963267948966

atan() Function

The atan() function returns the arc tangent of x, in radians.

Syntax

Following is the syntax for atan() function −

atan(x)

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

Parameters

  • x − This must be a numeric value.

Return Value

This function returns arc tangent of x, in radians. The result is between -pi/2 and pi/2.

Example

The following example shows the usage of the atan() method −

from math import atan

x = 0.5
val = atan(x)
print ("x: ",x, "atan(x): ", val)

x = 0.0
val = atan(x)
print ("x: ",x, "atan(x): ", val)

x = -1
val = atan(x)
print ("x: ",x, "atan(x): ", val)

x = 1
val = atan(x)
print ("x: ",x, "atan(x): ", val)

When we run the above program, it produces the following output

x: 0.5 atan(x): 0.4636476090008061
x: 0.0 atan(x): 0.0
x: -1 atan(x): -0.7853981633974483
x: 1 atan(x): 0.7853981633974483

atan2() Function

The atan2() function returns atan(y / x), in radians. For example, atan(1) and atan2(1, 1) are both pi/4, but atan2(-1, -1) is -3*pi/4.

Syntax

Following is the syntax for atan2() function −

atan2(y, x)

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

Parameters

  • y − This must be a numeric value in radians.

  • x − This must be a numeric in radians.

Return Value

This function returns atan(y / x), in radians. The result is between -pi and pi.

Example

The following example shows the usage of atan2() method −

from math import atan2

x,y = (-0.50,-0.50)
val = atan2(x,y)
print ("x: ",x, "y:",y, "atan2(x,y): ", val)

x,y = (0.50,0.50)
val = atan2(x,y)
print ("x: ",x, "y:",y, "atan2(x,y): ", val)

x,y= (5,5)
val = atan2(x,y)
print ("x: ",x, "y:",y, "atan2(x,y): ", val)

x,y = (-10,10)
val = atan2(x,y)
print ("x: ",x, "y:", y, "atan2(x,y): ", val)

x,y = (10,20)
val = atan2(x,y)
print ("x: ",x, "y:", y, "atan2(x,y): ", val)

When we run the above program, it produces the following output

x: -0.5 y: -0.5 atan2(x,y): -2.356194490192345
x: 0.5 y: 0.5 atan2(x,y): 0.7853981633974483
x: 5 y: 5 atan2(x,y): 0.7853981633974483
x: -10 y: 10 atan2(x,y): -0.7853981633974483
x: 10 y: 20 atan2(x,y): 0.4636476090008061

cos() function

The cos() function returns the cosine of x radians.

Syntax

Following is the syntax for cos() function −

cos(x)

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

Parameters

  • x − This must be a numeric value in radians.

Return Value

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

Example

The following example shows the usage of cos() method −

from math import cos, pi

x = 3
val = cos(x)
print ("x: ",x, "cos(x): ", val)

x = -3
val = cos(x)
print ("x: ",x, "cos(x): ", val)

x = 0
val = cos(x)
print ("x: ",x, "cos(x): ", val)

x = pi
val = cos(x)
print ("x: ",x, "cos(x): ", val)

x = 2*pi
val = cos(x)
print ("x: ",x, "cos(x): ", val)

When we run the above program, it produces the following output

x: 3 cos(x): -0.9899924966004454
x: -3 cos(x): -0.9899924966004454
x: 0 cos(x): 1.0
x: 3.141592653589793 cos(x): -1.0
x: 6.283185307179586 cos(x): 1.0

dist() Function

This function returns the Euclidean distance between two points p and q, each given as a sequence (or iterable) of coordinates. The two points must have the same dimension. The Euclidean distance between two points in the plane with coordinates (x, y) and (a, b) is given by $\mathrm{dist \: ((x,y),(a,b)) \: = \: \sqrt{(x − a)^2 + (y − b)^2}}$

Syntax

math.dist(p, q)

Parameters

  • p and q − iterables with two numeric operands.

Return value

This function returns the Euclidean distance between two points.

Example

from math import dist
p = [3,5]
q = [6,9]
val = dist(p,q)
print ("p: ",p, "q:", q, "dist(p,q): ", val)

p = [0,0]
q = [3,3]
val = dist(p,q)
print ("p: ",p, "q:", q, "dist(p,q): ", val)

It will produce the following output

p: [3, 5] q: [6, 9] dist(p,q): 5.0
p: [0, 0] q: [3, 3] dist(p,q): 4.242640687119285

hypot() Function

The function hypot() return the Euclidean norm, sqrt(x*x + y*y). This is length of vector from origin to point (x,y)

Syntax

Following is the syntax for hypot() function −

hypot(x, y)

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.

Parameters

  • x − This must be a numeric value.

  • y − This must be a numeric value.

Return Value

This function returns Euclidean norm, sqrt(x*x + y*y).

Example

The following example shows the usage of hypot() function −

from math import hypot

x =3
y =2
val = hypot(x,y)
print ("x: ",x, "y:", y, "hypot(x,y): ", val)

x = -3
y = 3
val = hypot(x,y)
print ("x: ",x, "y:", y, "hypot(x,y): ", val)

x =0
y =2
val = hypot(x,y)
print ("x: ",x, "y:", y, "hypot(x,y): ", val)

When we run the above program, it produces the following output

x: 3 y: 2 hypot(x,y): 3.605551275463989
x: -3 y: 3 hypot(x,y): 4.242640687119285
x: 0 y: 2 hypot(x,y): 2.0

sin() Function

The sin() function returns the sine of x, in radians.

Syntax

Following is the syntax for sin() function −

math.sin(x)

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

Parameters

  • x − This must be a numeric value.

Return Value

This function returns a numeric value between −1 and 1, which represents the sine of the parameter x.

Example

The following example shows the usage of sin() method −

from math import sin, pi

x = 3
val = sin(x)
print ("x: ",x, "sin(x): ", val)

x = −3
val = sin(x)
print ("x: ",x, "sin(x): ", val)

x = 0
val = sin(x)
print ("x: ",x, "sin(x): ", val)

x = pi
val = sin(x)
print ("x: ",x, "sin(x): ", val)

x = pi/2
val = sin(x)
print ("x: ",x, "sin(x): ", val)

When we run the above program, it produces the following output −

x: 3 sin(x): 0.1411200080598672
x: -3 sin(x): -0.1411200080598672
x: 0 sin(x): 0.0
x: 3.141592653589793 sin(x): 1.2246467991473532e-16
x: 1.5707963267948966 sin(x): 1.0

tan() Function

The tan() function returns the tangent of x radians.

Syntax

Following is the syntax for tan() function −

tan(x)

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.

Parameters

  • x − This must be a numeric value.

Return Value

This function returns a numeric value between -1 and 1, which represents the tangent of the parameter x.

Example

The following example shows the usage of tan() function −

from math import tan, pi

x = 3
val = tan(x)
print ("x: ",x, "tan(x): ", val)

x = -3
val = tan(x)
print ("x: ",x, "tan(x): ", val)

x = 0
val = tan(x)
print ("x: ",x, "tan(x): ", val)

x = pi
val = tan(x)
print ("x: ",x, "tan(x): ", val)

x = pi/2
val = tan(x)
print ("x: ",x, "tan(x): ", val)

When we run the above program, it produces the following output

x: 3 tan(x): -0.1425465430742778
x: -3 tan(x): 0.1425465430742778
x: 0 tan(x): 0.0
x: 3.141592653589793 tan(x): -1.2246467991473532e-16
x: 1.5707963267948966 tan(x): 1.633123935319537e+16
python_maths.htm
Advertisements