File size: 3,495 Bytes
780c9fe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
---
title: Math.atan()
short-title: atan()
slug: Web/JavaScript/Reference/Global_Objects/Math/atan
page-type: javascript-static-method
browser-compat: javascript.builtins.Math.atan
sidebar: jsref
---
The **`Math.atan()`** static method returns the inverse tangent (in radians) of a number, that is
<!-- prettier-ignore-start -->
<math display="block">
<semantics><mrow><mrow><mo lspace="0em" rspace="0.16666666666666666em">πΌπππ.ππππ</mo><mo stretchy="false">(</mo><mi>π‘</mi><mo stretchy="false">)</mo></mrow><mo>=</mo><mo lspace="0em" rspace="0em">arctan</mo><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mo>=</mo><mtext>the unique </mtext><mi>y</mi><mo>β</mo><mrow><mo>[</mo><mrow><mo>β</mo><mfrac><mi>Ο</mi><mn>2</mn></mfrac><mo>,</mo><mfrac><mi>Ο</mi><mn>2</mn></mfrac></mrow><mo>]</mo></mrow><mtext> such that </mtext><mo lspace="0em" rspace="0em">tan</mo><mo stretchy="false">(</mo><mi>y</mi><mo stretchy="false">)</mo><mo>=</mo><mi>x</mi></mrow><annotation encoding="TeX">\mathtt{\operatorname{Math.atan}(x)} = \arctan(x) = \text{the unique } y \in \left[-\frac{\pi}{2}, \frac{\pi}{2}\right] \text{ such that } \tan(y) = x</annotation></semantics>
</math>
<!-- prettier-ignore-end -->
{{InteractiveExample("JavaScript Demo: Math.atan()")}}
```js interactive-example
// Calculates angle of a right-angle triangle in radians
function calcAngle(opposite, adjacent) {
return Math.atan(opposite / adjacent);
}
console.log(calcAngle(8, 10));
// Expected output: 0.6747409422235527
console.log(calcAngle(5, 3));
// Expected output: 1.0303768265243125
```
## Syntax
```js-nolint
Math.atan(x)
```
### Parameters
- `x`
- : A number.
### Return value
The inverse tangent (angle in radians between <math><semantics><mrow><mo>-</mo><mfrac><mi>Ο</mi><mn>2</mn></mfrac></mrow><annotation encoding="TeX">-\frac{\pi}{2}</annotation></semantics></math> and <math><semantics><mfrac><mi>Ο</mi><mn>2</mn></mfrac><annotation encoding="TeX">\frac{\pi}{2}</annotation></semantics></math>, inclusive) of `x`. If `x` is {{jsxref("Infinity")}}, it returns <math><semantics><mfrac><mi>Ο</mi><mn>2</mn></mfrac><annotation encoding="TeX">\frac{\pi}{2}</annotation></semantics></math>. If `x` is `-Infinity`, it returns <math><semantics><mrow><mo>-</mo><mfrac><mi>Ο</mi><mn>2</mn></mfrac></mrow><annotation encoding="TeX">-\frac{\pi}{2}</annotation></semantics></math>.
## Description
Because `atan()` is a static method of `Math`, you always use it as `Math.atan()`, rather than as a method of a `Math` object you created (`Math` is not a constructor).
## Examples
### Using Math.atan()
```js
Math.atan(-Infinity); // -1.5707963267948966 (-Ο/2)
Math.atan(-0); // -0
Math.atan(0); // 0
Math.atan(1); // 0.7853981633974483 (Ο/4)
Math.atan(Infinity); // 1.5707963267948966 (Ο/2)
// The angle that the line (0,0) -- (x,y) forms with the x-axis in a Cartesian coordinate system
const theta = (x, y) => Math.atan(y / x);
```
Note that you may want to avoid the `theta` function and use {{jsxref("Math.atan2()")}} instead, which has a wider range (between -Ο and Ο) and avoids outputting `NaN` for cases such as when `x` is `0`.
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{jsxref("Math.acos()")}}
- {{jsxref("Math.asin()")}}
- {{jsxref("Math.atan2()")}}
- {{jsxref("Math.cos()")}}
- {{jsxref("Math.sin()")}}
- {{jsxref("Math.tan()")}}
- CSS {{cssxref("atan()")}} function
|