Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Get the Trigonometric tangent of an array of angles given in degrees with Python
The trigonometric tangent function returns the ratio of sine to cosine for each angle. To calculate the tangent of angles given in degrees, we use NumPy's tan() function combined with degree-to-radian conversion.
Syntax
numpy.tan(x, out=None, where=True)
Parameters
- x ? Input array of angles in radians
- out ? Optional output array where results are stored
- where ? Optional condition to control where calculation is applied
Converting Degrees to Radians
Since numpy.tan() expects angles in radians, we multiply degrees by ?/180 ?
import numpy as np
# Array of angles in degrees
angles_deg = np.array([0, 30, 45, 60, 90])
# Convert to radians and calculate tangent
angles_rad = angles_deg * np.pi / 180
tangent_values = np.tan(angles_rad)
print("Angles (degrees):", angles_deg)
print("Tangent values:", tangent_values)
Angles (degrees): [ 0 30 45 60 90] Tangent values: [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00 1.63312394e+16]
Complete Example
Let's create a comprehensive example with various angles ?
import numpy as np
print("Trigonometric tangent of angles in degrees")
# Array of angles in degrees
angles = np.array([0., 30., 45., 60., 90., 180., -180.])
print("Array of angles (degrees):")
print(angles)
print("\nArray datatype:", angles.dtype)
print("Array dimensions:", angles.ndim)
print("Number of elements:", angles.size)
# Calculate tangent (convert degrees to radians)
tangent_result = np.tan(angles * np.pi / 180)
print("\nTangent values:")
print(tangent_result)
Trigonometric tangent of angles in degrees Array of angles (degrees): [ 0. 30. 45. 60. 90. 180. -180.] Array datatype: float64 Array dimensions: 1 Number of elements: 7 Tangent values: [ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00 1.63312394e+16 -1.22464680e-16 1.22464680e-16]
Key Points
- Tangent of 90° approaches infinity (shown as very large number)
- Tangent of 180° and -180° are approximately zero due to floating-point precision
- Always convert degrees to radians using
* np.pi / 180 - The function is equivalent to
np.sin(x) / np.cos(x)
Conclusion
Use numpy.tan() with degree-to-radian conversion to calculate tangent values. Remember that tangent approaches infinity at 90° and multiples, which appears as very large numbers in the output.
Advertisements
