Reference · Math
math.acos
math.acos(num) -> floatWhat it does
The arc cosine: the angle in radians, between 0 and pi, whose cosine is the
number, as Python's math.acos. The number must be between -1 and 1. The angle
in degrees comes from math.degrees.
Examples
print(math.acos(1), math.acos(0), math.acos(-1))print(math.acos(0.5), math.degrees(math.acos(0.5)))0.0 1.5707963267948966 3.1415926535897931.0471975511965979 60.00000000000001Edge cases
- Floating point:
math.acos(0.5)in degrees is59.99999999999999. Round when a clean figure is wanted. - A value a hair past
1from a computation (1.0000000000000002, a dot product of unit vectors can give it) is a domain error, so clamp a computed value to-1..1first. math.acos(math.cos(x))givesxback, to the last digit, only forxbetween0andpi:-1comes back as1.NaNgivesNaN.
print(math.degrees(math.acos(0.5)), round(math.degrees(math.acos(0.5)), 10))let c = 1.0000000000000002print(math.acos(min([1, max([-1, c])])))print(math.acos(math.cos(1)), math.acos(math.cos(-1)))print(math.acos(math.nan))60.00000000000001 60.00.00.9999999999999999 0.9999999999999999NaNErrors
A number outside -1..1, infinity included, is a runtime error (a domain error), not
an error value: it stops the request, and ?? does not catch it. A value that is not
a number is a runtime error too.
print(math.acos(2))line 1: `math.acos`: math domain error (2)print(math.acos(math.inf))line 1: `math.acos`: math domain error (inf)Chaining
The angle between two vectors, from their dot product:
let a = [1, 0]let b = [1, 1]print(vec.dot(a, b) / (vec.norm(a) * vec.norm(b)) | math.acos | math.degrees | round(6))print([1, 0, -1] | map(math.acos))45.0[0.0, 1.5707963267948966, 3.141592653589793]See also
math.cos, math.asin, math.atan2, math.degrees, vec.dot
Thread 0 posts
On math.acos(): 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