杰瑞科技汇

Python中math.arctan函数如何使用?

Of course! In Python, you can calculate the arctangent (also known as the inverse tangent) using the math module. The math module provides two primary functions for this, each suited for a different range of inputs.

Python中math.arctan函数如何使用?-图1
(图片来源网络,侵删)

Summary

Function Input (y, x) Output Range Use Case
math.atan(y) A single number y -π/2 to π/2 When you only have the slope (y/x) and don't care about the quadrant.
math.atan2(y, x) Two numbers y and x to When you have the Cartesian coordinates (y, x) and need to determine the correct quadrant.

math.atan(y)

This function calculates the arctangent of a single number y. It's equivalent to calculating the arctangent of the slope y/x where x is implicitly 1.

Key Point: The result is always an angle in the range -π/2 to π/2 (or -90° to 90°). This means it cannot distinguish between angles in the first and third quadrants, or the second and fourth quadrants. For example, atan(1) and atan(1) will give the same result, even though the angles could be 45° or 225°.

Syntax

import math
result = math.atan(y)

Example

Let's find the angle whose tangent is 1. The arctangent of 1 is π/4 radians (or 45°).

import math
# The tangent of an angle is y/x. Here, we are finding the angle for tan(angle) = 1.
# This is equivalent to atan(1/1).
angle_rad = math.atan(1)
print(f"The arctangent of 1 is: {angle_rad} radians")
# Output: The arctangent of 1 is: 0.7853981633974483 radians
# To convert radians to degrees
angle_deg = math.degrees(angle_rad)
print(f"This is equal to: {angle_deg} degrees")
# Output: This is equal to: 45.0 degrees

Example with a Negative Number

The arctangent of -1 is -π/4 radians (or -45°).

Python中math.arctan函数如何使用?-图2
(图片来源网络,侵删)
import math
angle_rad = math.atan(-1)
print(f"The arctangent of -1 is: {angle_rad} radians")
# Output: The arctangent of -1 is: -0.7853981633974483 radians
angle_deg = math.degrees(angle_rad)
print(f"This is equal to: {angle_deg} degrees")
# Output: This is equal to: -45.0 degrees

math.atan2(y, x) (Recommended for most cases)

This is a more powerful and often more useful function. It takes two arguments, y and x, representing the Cartesian coordinates of a point. It calculates the angle from the positive x-axis to the point (x, y).

Key Point: The result is always an angle in the range -π to π (or -180° to 180°). Crucially, it uses the signs of both x and y to determine the correct quadrant of the angle, solving the ambiguity problem of math.atan.

Syntax

import math
result = math.atan2(y, x)

Example 1: Positive Coordinates (First Quadrant)

Point (1, 1). The angle should be 45°.

import math
# Point (x, y) = (1, 1)
angle_rad = math.atan2(1, 1)
print(f"The angle for point (1, 1) is: {angle_rad} radians")
# Output: The angle for point (1, 1) is: 0.7853981633974483 radians
angle_deg = math.degrees(angle_rad)
print(f"This is equal to: {angle_deg} degrees")
# Output: This is equal to: 45.0 degrees

Example 2: Negative Coordinates (Third Quadrant)

Point (-1, -1). The angle should be -135° (or 225°).

Python中math.arctan函数如何使用?-图3
(图片来源网络,侵删)
import math
# Point (x, y) = (-1, -1)
angle_rad = math.atan2(-1, -1)
print(f"The angle for point (-1, -1) is: {angle_rad} radians")
# Output: The angle for point (-1, -1) is: -2.356194490192345 radians
angle_deg = math.degrees(angle_rad)
print(f"This is equal to: {angle_deg} degrees")
# Output: This is equal to: -135.0 degrees

Notice that math.atan2(-1, -1) correctly gives -135°, while math.atan(-1 / -1) would have incorrectly given 45°.

Example 3: Different Quadrants

Let's see how atan2 handles all four quadrants.

Point (x, y) Angle (Radians) Angle (Degrees)
(1, 1) π/4 45°
(-1, 1) 3π/4 135°
(-1, -1) -3π/4 -135°
(1, -1) -π/4 -45°
import math
print(f"atan2(1, 1)  -> {math.degrees(math.atan2(1, 1)):.1f}°")
print(f"atan2(1, -1) -> {math.degrees(math.atan2(1, -1)):.1f}°")
print(f"atan2(-1, -1)-> {math.degrees(math.atan2(-1, -1)):.1f}°")
print(f"atan2(-1, 1) -> {math.degrees(math.atan2(-1, 1)):.1f}°")
# Output:
# atan2(1, 1)  -> 45.0°
# atan2(1, -1) -> 135.0°
# atan2(-1, -1)-> -135.0°
# atan2(-1, 1) -> -45.0°

When to Use Which?

Use Case Recommended Function Why?
You have the slope y/x and only care about the angle relative to the nearest x-axis. math.atan(y/x) Simpler, but be cautious of division by zero if x can be 0.
You have Cartesian coordinates (x, y) and need the true angle from the positive x-axis. math.atan2(y, x) Highly recommended. It's more robust, handles division by zero automatically, and correctly identifies the quadrant. This is the standard function for converting Cartesian to polar coordinates.
You need to find the angle of a 2D vector. math.atan2(y, x) This is exactly what atan2 is designed for.

Important Considerations

  1. Import the math Module: You must always import math before you can use these functions.
  2. Units: The output is always in radians. To convert to degrees, use math.degrees(). To convert from degrees to radians, use math.radians().
  3. Domain:
    • math.atan(y) accepts any real number y.
    • math.atan2(y, x) accepts any real numbers y and x. However, if both x and y are zero, the function is undefined (as the angle is ambiguous). Python will raise a ValueError in this case.
  4. Performance: math.atan2 is generally slightly slower than math.atan because it performs more logic to determine the quadrant. However, the difference is negligible for most applications, and the correctness of atan2 is almost always worth the tiny performance cost.
分享:
扫描分享到社交APP
上一篇
下一篇