Trigonometric functions in Python (sin, cos, tan, arcsin, arccos, arctan)

Modified: | Tags: Python, Mathematics

In Python, you can calculate trigonometric functions (sin, cos, tan) and inverse trigonometric functions (arcsin, arccos, arctan) with the math module.

For information on trigonometric functions in NumPy, see the following article.

All sample code in this article assumes that the math module has been imported.

import math

Pi (π): math.pi

The mathematical constant pi (π) is available as a constant in the math module and is represented by math.pi.

print(math.pi)
# 3.141592653589793

Angle conversion between radians and degrees: math.degrees(), math.radians()

In the math module, trigonometric and inverse trigonometric functions use radians as the unit of angles.

To convert between radians and degrees, use math.degrees() and math.radians().

math.degrees() converts radians to degrees, while math.radians() converts degrees to radians.

print(math.degrees(math.pi))
# 180.0

print(math.radians(180))
# 3.141592653589793

Sine and arc sine: math.sin(), math.asin()

Use math.sin() for the sine function and math.asin() for its inverse.

Here's an example of finding the sine of 30 degrees. Use math.radians() to convert degrees to radians.

sin30 = math.sin(math.radians(30))
print(sin30)
# 0.49999999999999994

The sine of 30 degrees should be 0.5, but since pi is an irrational number, there may be small errors in the calculation due to approximations.

To round to a specific number of decimal places, use the round() function, specifying the desired decimal places as the second argument. Note that this function employs "round half to even" or "bankers' rounding", meaning it rounds to the nearest even number.

print(round(sin30, 1))
# 0.5

To compare values while accounting for potential errors, you can use math.isclose().

print(math.isclose(sin30, 0.5))
# True

Similarly, here's an example of finding the inverse sine of 0.5. Since math.asin() returns radians, math.degrees() is used to convert the result to degrees.

asin05 = math.degrees(math.asin(0.5))
print(asin05)
# 29.999999999999996

print(round(asin05, 1))
# 30.0

Cosine and arc cosine: math.cos(), math.acos()

Use math.cos() for the cosine function and math.acos() for its inverse.

Here's an example of finding the cosine of 60 degrees and the arc cosine of 0.5.

print(math.cos(math.radians(60)))
# 0.5000000000000001

print(math.degrees(math.acos(0.5)))
# 59.99999999999999

To round to a specific number of decimal places, you can use round() as explained in the previous section.

Tangent and arc tangent: math.tan(), math.atan(), math.atan2()

Use math.tan() for the tangent function. The inverse functions are math.atan() and math.atan2(), with the differences between them discussed below.

Here's an example of finding the tangent of 45 degrees and the arc tangent of 1.

print(math.tan(math.radians(45)))
# 0.9999999999999999

print(math.degrees(math.atan(1)))
# 45.0

To round to a specific number of decimal places, you can use round() as explained in the previous section.

Differences between math.atan() and math.atan2()

While both math.atan() and math.atan2() compute the arc tangent, they differ in the number of arguments they accept and the range of their output values.

The return value of math.atan(x) is -90 to 90 degrees

math.atan(x) takes one argument and returns "arctan(x)" in radians. The returned value ranges from -pi/2 to pi/2 (-90 degrees to 90 degrees).

print(math.degrees(math.atan(0)))
# 0.0

print(math.degrees(math.atan(1)))
# 45.0

print(math.degrees(math.atan(-1)))
# -45.0

print(math.degrees(math.atan(math.inf)))
# 90.0

print(math.degrees(math.atan(-math.inf)))
# -90.0

In the example above, math.inf represents infinity.

The return value of math.atan2(y, x) is -180 to 180 degrees

math.atan2(y, x) takes two arguments and returns "arctan(y / x)" in radians. This angle is the polar angle of the vector from the origin to the point (x, y) in the polar coordinate plane, and the returned value ranges from -pi to pi (-180 degrees to 180 degrees).

math.atan2() is more suitable than math.atan() when working in the polar coordinate plane, as it can also correctly determine angles in the second and third quadrants.

Note that the argument order is y, x, not x, y.

print(math.degrees(math.atan2(0, 1)))
# 0.0

print(math.degrees(math.atan2(1, 1)))
# 45.0

print(math.degrees(math.atan2(1, 0)))
# 90.0

print(math.degrees(math.atan2(1, -1)))
# 135.0

print(math.degrees(math.atan2(0, -1)))
# 180.0

print(math.degrees(math.atan2(-1, -1)))
# -135.0

print(math.degrees(math.atan2(-1, 0)))
# -90.0

print(math.degrees(math.atan2(-1, 1)))
# -45.0

Negative zero

In math.atan2(), as in the example above, the angle in the negative x-axis direction (y is 0 and x has a negative value) is pi (180 degrees). However, when y is negative zero, the angle is -pi (-180 degrees). Be cautious if you need to handle the sign precisely.

print(math.degrees(math.atan2(-0.0, -1)))
# -180.0

Negative zero can be generated as a result of certain operations.

print(-1 / math.inf)
# -0.0

print(-1.0 * 0.0)
# -0.0

There is no negative zero in integers (int).

print(-0.0)
# -0.0

print(-0)
# 0

For cases when both x and y are zero (origin), the result may differ depending on their signs.

print(math.degrees(math.atan2(0.0, 0.0)))
# 0.0

print(math.degrees(math.atan2(-0.0, 0.0)))
# -0.0

print(math.degrees(math.atan2(-0.0, -0.0)))
# -180.0

print(math.degrees(math.atan2(0.0, -0.0)))
# 180.0

Not only math.atan2(), but also math.sin(), math.asin(), math.tan(), and math.atan() can produce results with different signs due to negative zero.

print(math.sin(0.0))
# 0.0

print(math.sin(-0.0))
# -0.0

print(math.asin(0.0))
# 0.0

print(math.asin(-0.0))
# -0.0

print(math.tan(0.0))
# 0.0

print(math.tan(-0.0))
# -0.0

print(math.atan(0.0))
# 0.0

print(math.atan(-0.0))
# -0.0

print(math.atan2(0.0, 1.0))
# 0.0

print(math.atan2(-0.0, 1.0))
# -0.0

Related Categories

Related Articles