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.
import numpy as np
angles = np.array([0, np.pi / 4, np.pi / 3])
print(np.round(np.tan(angles), 4))2Practical Example
Here is a real-world application of np.tan() showing how it is used in production NumPy code.
import numpy as np
near_singularity = np.array([np.pi / 2 - 0.0001])
print(np.tan(near_singularity))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.
import numpy as np
angles = np.array([0, np.pi / 4, np.pi / 3])
print(np.round(np.tan(angles), 4))