How to get the arctangent (in radians) of a number in JavaScript?


In this tutorial, we will learn how to get the arctangent (in radians) of a number in JavaScript.

We can use the JavaScript Math object to perform mathematical operations on integers. Math is a pre-defined object with attributes and methods to perform different mathematical functions and constants. This isn't a function object. Math functions work with the Number data type, and it is incompatible with BigInt.

Math, unlike many other global objects, does not have a function Object(). Math's methods and properties are all fixed. The Math.PI represents the constant pi, while the sine function is also referred to as Math.sin(x). Here x is the method's input. In JavaScript, constants are declared within the real values’ full precision.

Following are the methods to get the arctangent (in radians) of a number in JavaScript.

Using Math.atan() Method

The Math.atan() method gives a number ranging from (-π/2) to (π/2) radians. Because atan() is a static Math function, it is always used as Math.atan(), instead of using it as a method of a Math object that you constructed (Math is not a constructor). The arctangent of the supplied value in radians is returned by the Math.atan() function.

Syntax

Math.atan(value);

Here the value is being supplied as a parameter to the Math.atan() method to find its arctangent.

Example

In this example, we see how the Math.atan() method is being used to find the arctangent in radians of a number being passed as a parameter.

The value1 variable takes an input of a negative number -1 and returns the arctangent in radians while the variable value2 takes the input of a decimal number and returns the arctangent of the number. The variable value3 takes the input of a positive integer and returns the value of the arctangent in radians to the console and the variable value4 takes the input of a string which returns that it is not a number to the console.

<html> <body> <h2> Get the arctangent using <i> Math.atan() </i> method. </h2> <p id = "root"> </p> <p id = "root1"> </p> <p id = "root2"> </p> <p id = "root3"> </p> </body> <script> let root = document.getElementById("root"); let root1 = document.getElementById("root1"); let root2 = document.getElementById("root2"); let root3 = document.getElementById("root3"); let value1 = Math.atan(-1); root.innerHTML = " Math.atan(-1) = " + value1; let value2 = Math.atan(0.2); root1.innerHTML = " Math.atan(0.2) = " + value2; let value3 = Math.atan(29); root2.innerHTML = " Math.atan(29) = " + value3; let value4 = Math.atan("Text"); root3.innerHTML = " Math.atan('Text') = " + value4; </script> </html>

Using the math.js Library

Math.js is a large JavaScript and Node.js math library. It has a versatile expression parser with symbolic computing support, as well as a huge set of constants and built-in functions. And also it provides an optimal and integrated solution for working with various data types such as integers, complex numbers, fractions, units, matrices, and big numbers. It is powerful as well as simple to use.

It is compatible with the built-in Math library in JavaScript, and it also includes a versatile expression parser. It can perform symbolic computations. It includes a significant number of built-in functions and constants.

Syntax

math.atan(x);

Here the x is the number supplied as the parameter to the atan() function. It returns the arc tangent of x computed.

Example

In this example, the math.atan() method is used to calculate the arctangent in radians of an integer supplied as a parameter.

The value1 variable takes a negative number -4 as an input and returns the arctangent in radians. The variable value2 accepts a decimal number as input and returns its arctangent. The variable value3 accepts a positive integer as input and returns the arctangent in radians to the console. The variable value4 accepts a zero as input and returns 0.

<html> <body> <h2> Get the arctangent using math.js <i>math.atan()</i> method.</h2> <p id = "root"></p> <p id = "root1"></p> <p id = "root2"></p> <p id = "root3"></p> </body> <script src="https://unpkg.com/mathjs/lib/browser/math.js"></script> <script> let root = document.getElementById("root"); let root1 = document.getElementById("root1"); let root2 = document.getElementById("root2"); let root3 = document.getElementById("root3"); let value1 = math.atan(-4); root.innerHTML = ' math.atan(-4) = ' + value1; let value2 = math.atan(0.22); root1.innerHTML = ' math.atan(0.22) = ' + value2; let value3 = math.atan(17); root2.innerHTML = ' math.atan(17) = ' + value3; let value4 = math.atan(0); root3.innerHTML = ' math.atan(0) = ' + value4; </script> </html>

Updated on: 06-Sep-2022

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements