Python Glossary Русский

all Python 3mathterm

math.degrees

trigonometry

Converts an angle from radians to degrees. math.degrees(math.pi) → 180.0.

Syntax

math.degrees(x)

There is no normalisation involved — it is a plain multiplication by 180/π, so 3π becomes 540.0 and a negative angle from math.atan2 stays negative, somewhere in [-180, 180]. When you want the familiar 0..360 range, wrap it yourself: math.degrees(a) % 360.

Examples

import math
print(math.degrees(math.pi))      # → 180.0
print(math.degrees(math.pi/2))    # → 90.0
print(math.degrees(2*math.pi))    # → 360.0
print(math.degrees(0))            # → 0.0
print(math.degrees(math.pi/4))    # → 45.0
import math
# Не ровно 30.0! math.pi/6 не представим в double точно, и ошибка видна в ответе.
# помечено средозависимым: карточка учит НАЛИЧИЮ погрешности, а не её цифрам.
print(math.degrees(math.pi/6))    # → ?  (29.999999999999996 на Linux/x86-64; хвост зависит от libm)
import math
# Сравнивать такие значения нужно с допуском, а не на равенство:
print(math.isclose(math.degrees(math.pi/6), 30.0))  # → True
print(round(math.degrees(math.pi/6), 10))  # → 30.0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy