🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.tan()

AI & DATA SCIENCE // np-tan

np.tan() computes the trigonometric tangent of each element in an array, with input angles interpreted in radians, equal to sine divided by cosine.

Syntax

np.tan(arr)

Deep Dive Course

tan(x) equals sin(x) divided by cos(x), and unlike sine and cosine, it's unbounded — it approaches positive or negative infinity as the angle approaches an odd multiple of pi/2, 90 degrees, where cosine is 0. Passing an input very close to one of those undefined points doesn't raise an error; due to floating-point imprecision, it instead typically produces a very large finite number rather than exactly inf, since the input almost never lands on the mathematically exact undefined point.

1Understanding np.tan()

tan(x) equals sin(x) divided by cos(x), and unlike sine and cosine, it's unbounded — it approaches positive or negative infinity as the angle approaches an odd multiple of pi/2, 90 degrees, where cosine is 0. Passing an input very close to one of those undefined points doesn't raise an error; due to floating-point imprecision, it instead typically produces a very large finite number rather than exactly inf, since the input almost never lands on the mathematically exact undefined point.

💡

Be cautious feeding angle values near an odd multiple of 90 degrees, pi/2 radians, into np.tan() — the result grows extremely large and numerically unstable near those points, even though it won't typically produce an outright error.

editor.html
import numpy as np

angles = np.array([0, np.pi / 4, np.pi / 3])
print(np.round(np.tan(angles), 4))
localhost:3000

2Practical Example

Here is a real-world application of np.tan() showing how it is used in production NumPy code.

editor.html
import numpy as np

near_singularity = np.array([np.pi / 2 - 0.0001])
print(np.tan(near_singularity))
localhost:3000

3Best Practices

Follow these guidelines when working with np.tan():

1. Be aware that tan() is undefined, approaching infinity, near odd multiples of pi/2, and check for that condition explicitly if your inputs could realistically land near there

2. Compute tan(x) as sin(x) divided by cos(x) manually only if you specifically need to inspect the intermediate values — np.tan() is both simpler and more numerically direct otherwise

3. Use np.arctan()/np.arctan2() for the inverse operation, converting a ratio or slope back into an angle, rather than trying to invert tan() manually

⚠️

Tip: Be cautious feeding angle values near an odd multiple of 90 degrees, pi/2 radians, into np.tan() — the result grows extremely large and numerically unstable near those points, even though it won't typically produce an outright error.

editor.html
import numpy as np

angles = np.array([0, np.pi / 4, np.pi / 3])
print(np.round(np.tan(angles), 4))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

angles = np.array([0, np.pi / 4, np.pi / 3])
print(np.round(np.tan(angles), 4))
Example 02Advanced Example
import numpy as np

near_singularity = np.array([np.pi / 2 - 0.0001])
print(np.tan(near_singularity))

Best Practices

  • Be aware that tan() is undefined, approaching infinity, near odd multiples of pi/2, and check for that condition explicitly if your inputs could realistically land near there
  • Compute tan(x) as sin(x) divided by cos(x) manually only if you specifically need to inspect the intermediate values — np.tan() is both simpler and more numerically direct otherwise
  • Use np.arctan()/np.arctan2() for the inverse operation, converting a ratio or slope back into an angle, rather than trying to invert tan() manually

Interview Question

Why doesn't np.tan(np.pi / 2) raise a division-by-zero style error, given that cos(pi/2) is 0?

Hint: Think about what np.pi / 2 actually equals as a floating-point number, versus the true mathematical value.

np.pi is only a finite-precision floating-point approximation of the true irrational constant, so np.pi / 2 is a tiny bit off from the true mathematical value of 90 degrees in radians, meaning cos of that value is an extremely small number very close to zero, but not exactly zero. Dividing by that tiny non-zero number produces an extremely large, but still finite, floating-point result rather than an actual division-by-zero error or an infinite value, which is why np.tan() near a singularity typically returns a huge number instead of raising an exception.

Exercises

MediumPractice using np.tan() in a real scenario.
View Solution
import numpy as np

angles = np.array([0, np.pi / 4, np.pi / 3])
print(np.round(np.tan(angles), 4))

Frequently Asked Questions

Why doesn't np.tan(np.pi / 2) raise a division-by-zero style error, given that cos(pi/2) is 0?

np.pi is only a finite-precision floating-point approximation of the true irrational constant, so np.pi / 2 is a tiny bit off from the true mathematical value of 90 degrees in radians, meaning cos of that value is an extremely small number very close to zero, but not exactly zero. Dividing by that tiny non-zero number produces an extremely large, but still finite, floating-point result rather than an actual division-by-zero error or an infinite value, which is why np.tan() near a singularity typically returns a huge number instead of raising an exception.

Related Functions

np-sinnp-cosnp-arctan