Friday, December 20, 2024

MySQL ACOS() Function

 The ACOS() function in MySQL returns the arc cosine (inverse of cosine) of a number. It is used to calculate the angle (in radians) whose cosine is the given number. The result is a value in the range from 0 to π (0 to 3.14159), since the cosine of an angle ranges from -1 to 1.

Syntax:

ACOS(number)

Parameters:

  • number: The cosine value (a floating-point number) for which you want to find the angle. It must be in the range of -1 to 1, as the inverse cosine of values outside this range is undefined.

Return Value:

  • The function returns the angle in radians, which will be between 0 and π.

Example:

SELECT ACOS(1) AS angle;  -- Returns 0 radians
SELECT ACOS(0) AS angle;  -- Returns π/2 radians (approximately 1.5708)
SELECT ACOS(-1) AS angle; -- Returns π radians (approximately 3.1416)

Important Notes:

  • If the value of number is less than -1 or greater than 1, MySQL will return NULL, because the cosine of any angle cannot exceed this range.
  • The result is in radians, but you can convert the result to degrees if needed by using the DEGREES() function.

Example of conversion from radians to degrees:

SELECT DEGREES(ACOS(0.5)) AS angle_in_degrees;  -- Converts result to degrees

This will return the angle in degrees.

No comments:

Post a Comment