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

Money and Business

Using math, Python's standard module for mathematical functions, you can compute trigonometric functions (sin, cos, tan) and inverse trigonometric functions (arcsin, arccos, arctan).

The following contents are explained here with sample codes.

  • Pi (3.1415926..):math.pi
  • Angle conversion (radians, degrees):math.degrees(),math.radians()
  • Sine, Inverse sine:math.sin(),math.asin()
  • cosine, inverse cosine:math.cos(),math.acos()
  • Tangent, Inverse tangent:math.tan(),math.atan(),math.atan2()
  • Differences below:math.atan(),math.atan2()

Pi (3.1415926..): math.pi

Pi is provided as a constant in the math module. It is expressed as follows.

import math

# 3.141592653589793

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

Trigonometric and inverse trigonometric functions in the math module use the radian as the unit of angle.

Use math.degrees() and math.radians() to convert between radians (arc degree method) and degrees (degree method).

Math.degrees() converts from radians to degrees, and math.radians() converts from degrees to radians.

# 180.0

# 3.141592653589793

Sine, Inverse sine: math.sin(), math.asin()

The function to find the sine (sin) is math.sin() and the function to find the inverse sine (arcsin) is math.asin().

Here is an example of finding the sine of 30 degrees, using math.radians() to convert degrees to radians.

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

The sine of 30 degrees is 0.5, but there is an error because pi, an irrational number, cannot be calculated accurately.

If you wish to round to the appropriate number of digits, use the round() function or the format() method or the format() function.

Note that the return value of round() is a number (int or float), but the return value of format() is a string. If you want to use it for subsequent calculations, use round().

print(round(sin30, 3))
print(type(round(sin30, 3)))
# 0.5
# <class 'float'>

# 0.5
# <class 'str'>

print(format(sin30, '.3'))
print(type(format(sin30, '.3')))
# 0.5
# <class 'str'>

The round() function specifies the number of decimal places as its second argument. Note that this is not strictly rounding. See the following article for details.

The format() method and format() function specify the number of decimal places in the formatting specification string. See the following article for details.

If you want to compare, you can also use math.isclose().

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

Similarly, here is an example of finding the inverse sine of 0.5. math.asin() returns radians, which are converted to degrees with math.degrees().

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

print(round(asin05, 3))
# 30.0

cosine, inverse cosine: math.cos(), math.acos()

The function to find the cosine (cos) is math.cos(), and the function to find the inverse cosine (arc cosine, arccos) is math.acos().

Here is an example of finding the cosine of 60 degrees and the inverse cosine of 0.5.

# 0.5000000000000001

# 59.99999999999999

If you wish to round to the appropriate digit, you can use round() or format() as with sine.

Tangent, Inverse tangent: math.tan(), math.atan(), math.atan2()

The function to find the tangent (tan) is math.tan(), and the function to find the inverse tangent (arctan) is math.atan() or math.atan2().
Math.atan2() is described later.

An example of finding the tangent of 45 degrees and the inverse tangent of 1 degree is shown below.

# 0.9999999999999999

# 45.0

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

Both math.atan() and math.atan2() are functions that return the inverse tangent, but they differ in the number of arguments and the range of return values.

math.atan(x) has one argument and returns arctan(x) in radians. The return value will be between -pi \ 2 and pi \ 2 (-90 to 90 degrees).

# 0.0

# 45.0

# -45.0

# 90.0

# -90.0

In the example above, math.inf represents infinity.

math.atan2(y, x) has two arguments and returns arctan(y \ x) in radians. This angle is the angle (declination) that the vector from the origin to the coordinates (x, y) makes with the positive direction of the x axis in the polar coordinate plane, and the returned value is between -pi and pi (-180 to 180 degrees).

Since angles in the second and third quadrants can also be obtained correctly, math.atan2() is more appropriate than math.atan() when considering the polar coordinate plane.

Note that the order of the arguments 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

As in the example above, the negative direction of the x-axis (y is zero and x is negative) is pi (180 degrees), but when y is negative zero, it is -pi (-180 degrees). Be careful if you want to handle the sign strictly.

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

Negative zeros are the result of the following operations

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

print(-1.0 * 0.0)
# -0.0

Integers are not treated as negative zeros.

# -0.0

# 0

Even when both x and y are zero, the result depends on the sign.

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

There are other examples where the sign of the result changes depending on negative zeros, such as math.atan2() as well as math.sin(), math.asin(), math.tan(), and math.atan().

# 0.0

# -0.0

# 0.0

# -0.0

# 0.0

# -0.0

# 0.0

# -0.0

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

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

Note that the examples so far are the results of running the program in CPython. Note that other implementations or environments may handle negative zeros differently.

Copied title and URL