Reference · Math
math.cos
signature
math.cos(num) -> floatWhat it does
The cosine of an angle in radians, as a float, as Python's math.cos. An angle in
degrees is converted first with math.radians.
Examples
print(math.cos(0), math.cos(math.pi), math.cos(1))print(math.cos(math.radians(60)), math.cos(2 * math.pi))output
1.0 -1.0 0.54030230586813980.5000000000000001 1.0Edge cases
- Floating point:
math.cos(math.pi / 2)is about6.1e-17, not0, and the cosine of 60 degrees is0.5000000000000001. Round when a clean figure is wanted. - A huge angle is still reduced exactly, as Python does.
NaNgivesNaN; an infinite angle is a domain error.
print(math.cos(math.pi / 2), round(math.cos(math.pi / 2), 12))print(math.cos(math.radians(60)), round(math.cos(math.radians(60)), 10))print(math.cos(1e22), math.cos(math.nan))output
0.00000000000000006123233995736766 0.00.5000000000000001 0.50.523214785395139 NaNErrors
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.cos(-math.inf))output
line 1: `math.cos`: math domain error (-inf)this one fails
print(math.cos(null))output
line 1: `math.cos` expects a number as argument 1, got nullChaining
The dot product of two unit vectors is the cosine of the angle between them, so the angle comes back with math.acos:
print(60 | math.radians | math.cos | round(10))print([0, 60, 180] | map(math.radians) | map(math.cos) | map((x) => round(x, 10)))print(0.5 | math.acos | math.degrees)output
0.5[1.0, 0.5, -1.0]60.00000000000001See also
math.sin, math.tan, math.acos, math.radians, math.cosh
Thread
Thread 0 posts
On math.cos(): 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