Reference · Math

math.cos

signature
math.cos(num) -> float

What 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.0

Edge cases

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 NaN

Errors

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 null

Chaining

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.00000000000001

See also

math.sin, math.tan, math.acos, math.radians, math.cosh

Thread
5Useful? Like it, or add to its thread below.

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