Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get the arccosine (in radians) of a number in JavaScript?
This tutorial will help you to find the arccosine in radians of a number in JavaScript.
Cosine is the ratio of the adjacent side and the hypotenuse of the right-angled triangle. To get the cosine of a number, the Math.cos() function is used in JavaScript. The value returned will be between -1 and 1.
The inverse of cosine is called arccosine. An angle's trigonometric arccosine value can be calculated using the Math.acos() method. This returns a value between 0 and ? (pi).
The input to acos() must be between -1 and 1 inclusive. The function returns the arccosine value in radians. Mathematical representation: acos(n) = cos?ยน(n).
Syntax
Math.acos(number)
Parameters
number ? A number between -1 and 1 for which the arccosine value in radians is to be found.
Return Value
Returns the arccosine of the number in radians (between 0 and ?). If the number is outside the range [-1, 1], it returns NaN.
Example 1: Basic Usage
This example demonstrates Math.acos() with valid inputs between -1 and 1, and invalid inputs that return NaN.
<html>
<body>
<h4>Finding the arccosine of a number using <i>Math.acos()</i></h4>
<p id="validInputs"></p>
<p id="invalidInputs"></p>
<script>
// Valid inputs (between -1 and 1)
let a1 = Math.acos(0.75);
let a2 = Math.acos(-0.9);
let a3 = Math.acos(-1);
let a4 = Math.acos(0);
let a5 = Math.acos(0.5);
let a6 = Math.acos(1);
// Invalid inputs (outside range or non-numeric)
let b1 = Math.acos(-2);
let b2 = Math.acos(2);
let b3 = Math.acos("Learn");
document.getElementById("validInputs").innerHTML =
"Valid inputs (between -1 and 1):" + "<br>" +
"Math.acos(0.75) = " + a1 + "<br>" +
"Math.acos(-0.9) = " + a2 + "<br>" +
"Math.acos(-1) = " + a3 + "<br>" +
"Math.acos(0) = " + a4 + "<br>" +
"Math.acos(0.5) = " + a5 + "<br>" +
"Math.acos(1) = " + a6;
document.getElementById("invalidInputs").innerHTML =
"Invalid inputs (return NaN):" + "<br>" +
"Math.acos(-2) = " + b1 + "<br>" +
"Math.acos(2) = " + b2 + "<br>" +
"Math.acos('Learn') = " + b3;
</script>
</body>
</html>
Example 2: Mathematical Operations
This example shows how Math.acos() works with mathematical expressions. The expressions are evaluated first, then the arccosine is calculated.
<html>
<body>
<h4>Using Math.acos() with mathematical operations</h4>
<p id="mathOperations"></p>
<script>
document.getElementById("mathOperations").innerHTML =
"Math.acos(1 + 2) = " + Math.acos(1 + 2) + "<br>" +
"Math.acos(0.1 + 0.2) = " + Math.acos(0.1 + 0.2) + "<br>" +
"Math.acos(2 - 1) = " + Math.acos(2 - 1) + "<br>" +
"Math.acos(Math.PI/4) = " + Math.acos(Math.PI/4);
</script>
</body>
</html>
Key Points
- Input must be between -1 and 1 (inclusive)
- Returns value between 0 and ? radians
- Returns
NaNfor invalid inputs - Supported in all modern browsers
Conclusion
The Math.acos() method calculates the arccosine of a number in radians. It only accepts values between -1 and 1, returning NaN for invalid inputs. This method is essential for trigonometric calculations in JavaScript.
