Lesson

MySQL ACOS() Function

10 min reading
Free Course

MySQL ACOS() Function: Computing Inverse Cosine Angles

The ACOS() function returns the arc cosine (inverse cosine) of a number in MySQL. Given a cosine ratio between -1 and 1, it outputs the angle in radians from 0 to PI().

You will reach for ACOS() when implementing spherical distance formulas, such as the Haversine or spherical law of cosines calculations between customer coordinates. It always returns a double-precision floating-point value or NULL.

Syntax

ACOS(x)
  • x: A numeric expression representing a cosine value. Valid inputs fall strictly within the closed interval [-1, 1].

Practical Example

Suppose demo_customers stores precomputed dot-product cosine similarities between customer delivery zones and a warehouse hub. You can convert each cosine ratio into degrees using DEGREES(ACOS(cosine_val)).

SELECT
    customer_id,
    city,
    cosine_val,
    ROUND(ACOS(cosine_val), 4) AS angle_radians,
    ROUND(DEGREES(ACOS(cosine_val)), 2) AS angle_degrees
FROM demo_customers
WHERE cosine_val BETWEEN -1 AND 1;
customer_id city cosine_val angle_radians angle_degrees
201 Austin 1.0 0.0000 0.00
204 Denver 0.5 1.0472 60.00
209 Seattle -0.5 2.0944 120.00

When cosine_val equals 1.0, the angle is 0 radians. When cosine_val is 0.5, ACOS(0.5) returns roughly 1.0472 radians (60 degrees).

Common Mistakes & NULL Behavior

  • Out-of-domain inputs: In MySQL, ACOS(2) or ACOS(-1.0001) does not raise an error. Instead, it silently returns NULL.
  • Floating-point drift in geo queries: Spherical cosine formulas can produce 1.0000000000000002 due to IEEE 754 rounding. Passing that directly to ACOS() returns NULL and breaks distance reports. Clamp the argument first with LEAST(1, GREATEST(-1, expr)).
  • NULL propagation: ACOS(NULL) returns NULL, matching standard MySQL scalar behavior.

Frequently Asked Questions

Why does ACOS(1.0000001) return NULL in MySQL?

Real cosine values never exceed 1 or drop below -1. Because 1.0000001 lies outside [-1, 1], MySQL returns NULL rather than throwing a domain error.

How do I convert the ACOS() output from radians to degrees?

Wrap the result in DEGREES(), like DEGREES(ACOS(0)), which converts 1.570796... radians cleanly into 90 degrees.

Does ACOS() work on negative numbers?

Yes, as long as the number is at least -1. For instance, ACOS(-1) returns 3.141592653589793, which equals PI().

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum