Languages
[Edit]
EN

Python - math.atan() method example

0 points
Created by:
Mark-Rotteveel
537

The math.atan function returns number in radians in the range -math.pi/2 to +math.pi/2. The function calculates the inverted tangent function value.

import math


def calculate_angle(a, b):
    return math.atan(a / b)


#   |\
#   | \ h - for this function this information is not necessary
# a |  \
#   |__*\ <- angle
#     b


# a, b and h build isosceles right triangle
a = 3
b = a
print(calculate_angle(a, b))  # 0.7853981633974483 <-  45 degrees

# a, b and h build half of equilateral triangle
a = 3
b = a * math.sqrt(3)
print(calculate_angle(a, b))  # 0.5235987755982987 <- ~30 degrees

1. Documentation

Syntax
math.atan(number)
Parameters

number - a number value that represents the result of the operation opposite / adjacent on the right triangle.

Result

number value in radians in the range -math.pi/2 to +math.pi/2.

If the value can not be calculated ValueError occures.

Description

atan is a method that takes only one parameter and returns an approximation of the result of the mathematical function arctangent(x).

2. Working with degrees

import math


def calculate_angle(a, b):
    angle = math.atan(a / b)

    return (180 / math.pi) * angle


#   |\
#   | \ h - for this function this information is not necessary
# a |  \
#   |__*\ <- angle
#     b


# a, b and h build isosceles right triangle
a = 3
b = a
print(calculate_angle(a, b))  # 0.7853981633974483 <-  45 degrees

# a, b and h build half of equilateral triangle
a = 3
b = a * math.sqrt(3)
print(calculate_angle(a, b))  # 0.5235987755982987 <- ~30 degrees

References

  1. Inverse trigonometric functions - Wikipedia

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Cross technology - Math.atan()

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join