Reference · Math
math.tan
signature
math.tan(num) -> floatWhat it does
The tangent of an angle in radians, as a float, as Python's math.tan. An angle in
degrees is converted first with math.radians.
Examples
print(math.tan(0), math.tan(math.pi / 4), math.tan(1))print(math.tan(math.radians(45)), math.tan(-math.pi / 4))output
0.0 0.9999999999999999 1.55740772465490230.9999999999999999 -0.9999999999999999Edge cases
- Floating point: the tangent of 45 degrees is
0.9999999999999999. Round when a clean figure is wanted. math.pi / 2is not exactly a right angle, somath.tan(math.pi / 2)is a huge number (about1.6e16), not infinity and not an error.NaNgivesNaN; an infinite angle is a domain error.math.tan(x)ismath.sin(x) / math.cos(x)to within a last digit, which differs by platform: compare withmath.isclose, not==.
print(math.tan(math.radians(45)), round(math.tan(math.radians(45)), 10))print(math.tan(math.pi / 2) > 1e16, math.isfinite(math.tan(math.pi / 2)))print(math.tan(math.nan))print(math.isclose(math.tan(1), math.sin(1) / math.cos(1)))output
0.9999999999999999 1.0true trueNaNtrueErrors
inf or -inf is a runtime error (a domain error), not an error value: it stops the
request. A value that is not a number is a runtime error too; a decimal is converted
first with float.
this one fails
print(math.tan(math.inf))output
line 1: `math.tan`: math domain error (inf)this one fails
print(math.tan(dec("1")))output
line 1: `math.tan` expects a number as argument 1, got dec (this works on floats: convert a dec with float(x))Chaining
A slope from an angle, and the angle back with math.atan:
print(30 | math.radians | math.tan)print(30 | math.radians | math.tan | math.atan | math.degrees)output
0.577350269189625729.999999999999996See also
math.sin, math.cos, math.atan, math.atan2, math.radians, math.tanh
Thread
Thread 0 posts
On math.tan(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo