JavaScript acos: How to Use Math.acos() Function
The acos() method comes in handy in programming contexts dealing with any trigonometric expressions. The acos() is a static method of Math, and it can be used without creating an object.
JavaScript Math acos
The Math.acos() is a built-in JavaScript function that is used to find the arccosine value of a given argument. To find an arccosine value of a given argument in JavaScript, use the Math.acos() method.
The Math.acos() function is used to return the arccosine of a number in radians. The Math.acos() method returns the numeric value between 0 and pi radians.
Syntax
Math.acos(x)
Parameter(s)
The variable x lies between -1 and 1 whose arccosine value is to be determined.
Return Value
The arccosine value is between 0 and pi radians.
See the following figure.
Note:
- If the passed value lies outside the range [-1,1], the method returns NaN.
Compatibility
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
- Android webview
- Chrome for Android
- Firefox for Android
- Opera for Android
- Safari on iOS
- Samsung Internet
- Node.js
JavaScript version: ECMAScript 1
Consider the following examples.
JavaScript Math acos() Code Example
The following example demonstrates the use of the Javascript Math acos() method.
// example1.js var a = -1; var b = 1; var c = 0.5; var d = 0; console.log(Math.acos(a)); console.log(Math.acos(b)); console.log(Math.acos(c)); console.log(Math.acos(d));
Output
node example1 3.141592653589793 0 1.0471975511965979 1.5707963267948966
Example 2
The following example demonstrates the case where values outside the range of [-1,1] are passed as parameters.
// example2.js var a = 2; var b = -2; console.log(Math.acos(a)); console.log(Math.acos(b));
Output
node example2 NaN NaN
Example 3
The following example demonstrates an application of this method in a simple programming context.
Given the three sides of a right-angled triangle, find all of its angles.
// example3.js var h; var b; var p; const r = require('readline'); const rl = r.createInterface({ input: process.stdin, output: process.stdout }); const prompt1 = () => { return new Promise((resolve, reject) => { rl.question('Hypotenuse: ', (answer) => { h = answer; resolve(); }); }); }; const prompt2 = () => { return new Promise((resolve, reject) => { rl.question('Base: ', (answer) => { b = answer; resolve(); }); }); }; const prompt3 = () => { return new Promise((resolve, reject) => { rl.question('Perpendicular: ', (answer) => { p = answer; resolve(); }); }); }; const main = async () => { await prompt1(); await prompt2(); await prompt3(); rl.close(); console.log('The three angles of the triangle are(in radians):'); console.log(Math.acos(0)); //pi/2 radians console.log(Math.acos(b / h)); //base angle console.log(Math.acos(0) - Math.acos(b / h)); //third angle } main();
Output
Test Case 1: ->node example3 Hypotenuse: 6.928 Base: 6 Perpendicular: 3.464 The three angles of the triangle are: 1.5707963267948966 0.523547964303085 1.0472483624918114 Test Case2: ->node example3 Hypotenuse: 42.426 Base: 30 Perpendicular: 30 The three angles of the triangle are(in radians): 1.5707963267948966 0.7853885732135081 0.7854077535813885