Python Glossary Русский

all Python 3mathterm

math.cos

trigonometry

Returns the cosine of an angle given in radians. The result is always in [-1, 1]. For degrees: math.cos(math.radians(angle)).

Syntax

math.cos(x)

math.pi is only a float approximation, so math.cos(math.pi / 2) comes out as 6.1e-17 rather than an exact zero — comparing the result with == is nearly always wrong, use math.isclose() with an abs_tol. And the function does not check units: math.cos(60) quietly returns the cosine of 60 radians, a plausible-looking but wrong answer.

Examples

import math
print(math.cos(0))            # → 1.0
print(math.cos(math.pi))      # → -1.0
print(math.cos(math.pi/2))    # → ?  (значение зависит от системной libm — карточка учит наличию погрешности, а не её цифрам)
print(round(math.cos(math.pi/3), 2))  # → 0.5
print(math.cos(math.pi/4))    # → 0.7071...
print(math.cos(-math.pi))     # → -1.0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy