JavaScript Math.acos() Method

The Math.acos function in JavaScript computes the arc cosine (or inverse cosine) of a specified number, returning the result in radians. The input value provided to the Math.acos function must lie within the interval of -1 to 1; if the input is outside this range, the function will return NaN.

Syntax

The acos function is expressed using the syntax below:

Example

Math.acos(x);

Parameter

x: This is a numerical value that ranges from -1 to 1, signifying the cosine of a given angle.

Return

It returns the arc cosine of the number.

JavaScript Math acos method examples

In this section, we will explore the acos function by examining several examples.

Example 1: Math.acos method

Let's see a simple example of the acos method.

Example

Example

console.log(Math.acos(-1));  

console.log(Math.acos(0));  

console.log(Math.acos(0.5));  

console.log(Math.acos(1));

Output:

Output

3.141592653589793

1.5707963267948966

1.0471975511965979

0

Explanation:

In this illustration, the function Math.acos(x) computes the arccosine of the numerical input x. It is important to note that the output is provided in radians rather than degrees. The permissible range for the input is from -1 to 1 (inclusive), and the resulting value will lie between 0 and π (approximately 3.14159). For instance, when evaluating Math.acos(-1), where

The cosine of π yields -1, which implies that arccos(-1) equals π; thus, the result is 3.14159265358979. For Math.acos(0), since the cosine of π/2 is 0, it follows that arccos(0) is π/2, leading to an output of 1.5707963267948966. When we evaluate Math.acos(0.5), we note that the cosine of π/3 is 0.5, indicating that arccos(0.5) equals π/3, resulting in an output of 1.0471975511965979. Again, for Math.acos(0.5), the cosine of π/3 remains 0.5, confirming that arccos(0.5) equals π/3, yielding the same output of 1.0471975511965979. Lastly, considering Math.acos(1), where the cosine of 0 is 1, we find that arccos(1) results in 0, thus the output is 0.

Example 2: When acos method returns NaN

Let’s examine several scenarios in which the acos function yields NaN (Not a Number).

Example

Example

console.log(Math.acos());  

console.log(Math.acos(2));  

console.log(Math.acos(-2));

Output:

Output

NaN

NaN

NaN

Explanation:

In this illustration, the Math.acos function in JavaScript computes the arc cosine of a given number expressed in radians. We have not supplied any arguments, indicating that we are invoking Math.acos without any parameters. Consequently, this piece of code will yield NaN since the acos function is defined exclusively for numbers within the interval of -1 to 1. For instance, in console.log(Math.acos(2)), the argument provided is 2. Therefore, it evaluates whether 2 falls within the -1 to 1 range. Since 2 is outside this interval, the function returns NaN. Similarly, in console.log(Math.acos(-2)), the input is -2, which again lies outside the permissible range for the acos function, resulting in an output of NaN.

Example 3: Testing the Math.acos method with a user-defined input

We can evaluate the acos function by utilizing input provided by the user.

Example

Example

<!DOCTYPE html>

<html>

<head>

<title>Testing the acos() method</title>

</head>

<body>

<form>  

  Enter a number: <input type="text" id="num">  

  <input type="button" onclick="display()" value="Submit">  

  </form>  

  <p><span id="result"></span></p>  

<script>  

function display()  

{  

  var x=document.getElementById("num").value;  

document.getElementById("result").innerHTML=Math.acos(x);  

}  

</script>  

</body>

</html>

Output:

Upon inputting a value within the range of -1 to 1, the outcome (expressed in radians) will be displayed beneath the form:

Since the input provided is either invalid or exceeds the permissible range, it results in a return value of NaN.

Explanation:

The combination of HTML and JavaScript enables users to input a number, compute its arccosine, and subsequently present the result on the display. Within the <script> block, we create a function named display. This function retrieves the user-entered value from the input field identified by the id num through the use of document.getElementById("num").value. We assign this retrieved value to a variable referred to as x. Next, the function calculates the arccosine of x using the JavaScript built-in method Math.acos(x), which provides the output in radians. Finally, the result is shown in an element with the id result, by updating its innerHTML property.

In the HTML, we designed a straightforward form that requests the user to input a number into a text field. We included a button titled submit. When this button is clicked, it invokes the display function. Beneath the form, there exists a <p> tag that houses a <span> where the output will be presented. This implementation lacks input validation; however, it serves as a practical example for basic mathematical input and output on a web page. If the entered value is invalid or falls outside the specified range of (-1 to 1), it will return NaN.

Conclusion

In JavaScript, the Math.acos function computes the arccosine of a specified number, representing the angle in radians whose cosine corresponds to the provided value. This method only accepts arguments within the interval of -1 to 1 and produces an output that ranges from 0 to π radians. For any values that fall outside this specified range, it returns NaN, making it particularly valuable for trigonometric computations, especially in the fields of geometry and graphics.

This article has discussed the Math.acos function along with several examples. Additionally, it contains a section of commonly asked questions that may assist you in your interview preparation.

The function Math.acos serves a specific purpose in programming, particularly within the realm of mathematics and trigonometry. It is utilized to compute the arc cosine of a given number. The arc cosine function is the inverse of the cosine function, which means it takes a value from the range of -1 to 1 and returns an angle in radians, specifically between 0 and π (or 0 and 180 degrees).

Here are some key points to consider regarding the utilization of Math.acos:

  • Input Range: The input for Math.acos must be a numerical value that lies within the interval of -1 to 1. Any value outside this range will result in an error or an invalid output.
  • Output: The output of this function is an angle, represented in radians. For example, if you input 1 into Math.acos, the function will return 0 radians, as the cosine of 0 is 1.
  • Use Cases: Math.acos is particularly useful in various applications, such as calculating angles in geometry, determining the angle between two vectors in physics, or solving problems in navigation and computer graphics.
  • Example: To illustrate its application, if you were to execute Math.acos(0.5), the function would return approximately 1.0472 radians, which corresponds to an angle of 60 degrees.

In summary, Math.acos is a valuable function for retrieving the angle whose cosine is a specified number, adhering to the defined input range and producing outputs in radians.

In JavaScript, there exists a static method known as Math.asin, which is used to calculate the arcsine of a specified number. This method yields the angle in radians corresponding to the input value.

  1. What does Math.acos output?

The Math.acos function yields the arc cosine of a given numerical input in radians. The output will be a value ranging from 0 to π (pi).

  1. Is Math.acos a static method?

Indeed, the Math object provides a static method, which allows us to invoke it directly using the Math identifier, such as Math.acos, rather than requiring an instance of the Math object.

  1. What is the syntax for the Math.acos method?

The format for this function is Math.acos(x). The argument x represents the value for which we aim to determine the arccosine.

  1. What type of value is required for x?

The variable x is required to be a numerical value that falls within the range of -1 to 1. It signifies the cosine of a given angle.

Input Required

This code uses input(). Please provide values below: