aboutsummaryrefslogtreecommitdiff
path: root/files/tr/web/javascript/reference/global_objects/math/index.html
blob: 774ed159f211ca561b593f15eea3a812907f2c24 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
---
title: Math
slug: Web/JavaScript/Reference/Global_Objects/Math
tags:
  - JavaScript
  - Math
  - Namespace
  - NeedsTranslation
  - Reference
  - TopicStub
translation_of: Web/JavaScript/Reference/Global_Objects/Math
---
<div></div>

<div>{{JSRef}}</div>

<p><strong><code>Math</code></strong> is a built-in object that has properties and methods for mathematical constants and functions. It’s not a function object.</p>

<p><code>Math</code> works with the {{jsxref("Number")}} type. It doesn't work with {{jsxref("BigInt")}}.</p>

<h2 id="Description">Description</h2>

<p>Unlike many other global objects, <code>Math</code> is not a constructor. All properties and methods of <code>Math</code> are static. You refer to the constant pi as <code>Math.PI</code> and you call the sine function as <code>Math.sin(<var>x</var>)</code>, where <code><var>x</var></code> is the method’s argument. Constants are defined with the full precision of real numbers in JavaScript.</p>

<div class="note">
<p><strong>Note:</strong> Many <code>Math</code> functions have a precision that’s <em>implementation-dependent.</em></p>

<p>This means that different browsers can give a different result. Even the same JavaScript engine on a different OS or architecture can give different results!</p>
</div>

<h2 id="Static_properties">Static properties</h2>

<dl>
 <dt>{{jsxref("Math.E")}}</dt>
 <dd>Euler's constant and the base of natural logarithms; approximately <code>2.718</code>.</dd>
 <dt>{{jsxref("Math.LN2")}}<code>lllllđđlđ</code></dt>
 <dd>lllllđđ v<img alt="" src="https://mdn.mozillademos.org/files/14829/trigonometry.png" style="height: 166px; width: 200px;">ili ga ti tu of <code>2</code>; approximately <code>0.693</code>.</dd>
 <dt>{{jsxref("Math.LN10")}}</dt>
 <dd>Natural logarithm of <code>10</code>; approximately <code>2.303</code>.</dd>
 <dt>{{jsxref("Math.LOG2E")}}</dt>
 <dd>Base-2 logarithm of <code>E</code>; approximately <code>1.443</code>.</dd>
 <dt>{{jsxref("Math.LOG10E")}}</dt>
 <dd>Base-10 logarithm of <code>E</code>; approximately <code>0.434</code>.</dd>
 <dt>{{jsxref("Math.PI")}}</dt>
 <dd>Ratio of the a circle's circumference to its diameter; approximately <code>3.14159</code>.</dd>
 <dt>{{jsxref("Math.SQRT1_2")}}</dt>
 <dd>Square root of ½ (or equivalently, <sup>1</sup>/<sub>√2</sub>); approximately <code>0.707</code>.</dd>
 <dt>{{jsxref("Math.SQRT2")}}</dt>
 <dd>Square root of <code>2</code>; approximately <code>1.414</code>.</dd>
</dl>

<h2 id="Static_methods">Static methods</h2>

<dl>
 <dt>{{jsxref("Global_Objects/Math/abs", "Math.abs(<var>x</var>)")}}</dt>
 <dd>Returns the absolute value of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/acos", "Math.acos(<var>x</var>)")}}</dt>
 <dd>Returns the arccosine of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/acosh", "Math.acosh(<var>x</var>)")}}</dt>
 <dd>Returns the hyperbolic arccosine of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/asin", "Math.asin(<var>x</var>)")}}</dt>
 <dd>Returns the arcsine of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/asinh", "Math.asinh(<var>x</var>)")}}</dt>
 <dd>Returns the hyperbolic arcsine of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/atan", "Math.atan(<var>x</var>)")}}</dt>
 <dd>Returns the arctangent of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/atanh", "Math.atanh(<var>x</var>)")}}</dt>
 <dd>Returns the hyperbolic arctangent of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/atan2", "Math.atan2(<var>y</var>, <var>x</var>)")}}</dt>
 <dd>Returns the arctangent of the quotient of its arguments.</dd>
 <dt>{{jsxref("Global_Objects/Math/cbrt", "Math.cbrt(<var>x</var>)")}}</dt>
 <dd>Returns the cube root of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/ceil", "Math.ceil(<var>x</var>)")}}</dt>
 <dd>Returns the smallest integer greater than or equal to <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/clz32", "Math.clz32(<var>x</var>)")}}</dt>
 <dd>Returns the number of leading zeroes of the 32-bit integer <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/cos", "Math.cos(<var>x</var>)")}}</dt>
 <dd>Returns the cosine of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/cosh", "Math.cosh(<var>x</var>)")}}</dt>
 <dd>Returns the hyperbolic cosine of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/exp", "Math.exp(<var>x</var>)")}}</dt>
 <dd>Returns <code>E<var><sup>x</sup></var></code>, where <code><var>x</var></code> is the argument, and <code>E</code> is Euler's constant (<code>2.718</code>…, the base of the natural logarithm).</dd>
 <dt>{{jsxref("Global_Objects/Math/expm1", "Math.expm1(<var>x</var>)")}}</dt>
 <dd>Returns subtracting <code>1</code> from <code>exp(<var>x</var>)</code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/floor", "Math.floor(<var>x</var>)")}}</dt>
 <dd>Returns the largest integer less than or equal to <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/fround", "Math.fround(<var>x</var>)")}}</dt>
 <dd>Returns the nearest <a href="https://en.wikipedia.org/wiki/Single-precision_floating-point_format" title="link to the wikipedia page on single precision">single precision</a> float representation of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/hypot", "Math.hypot([<var>x</var>[, <var>y</var>[, …]]])")}}</dt>
 <dd>Returns the square root of the sum of squares of its arguments.</dd>
 <dt>{{jsxref("Global_Objects/Math/imul", "Math.imul(<var>x</var>, <var>y</var>)")}}</dt>
 <dd>Returns the result of the 32-bit integer multiplication of <code><var>x</var></code> and <code><var>y</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/log", "Math.log(<var>x</var>)")}}</dt>
 <dd>Returns the natural logarithm (㏒<sub>e</sub>; also, ㏑) of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/log1p", "Math.log1p(<var>x</var>)")}}</dt>
 <dd>Returns the natural logarithm (㏒<sub>e</sub>; also ㏑) of <code>1 + <var>x</var></code> for the number <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/log10", "Math.log10(<var>x</var>)")}}</dt>
 <dd>Returns the base-10 logarithm of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/log2", "Math.log2(<var>x</var>)")}}</dt>
 <dd>Returns the base-2 logarithm of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/max", "Math.max([<var>x</var>[, <var>y</var>[, …]]])")}}</dt>
 <dd>Returns the largest of zero or more numbers.</dd>
 <dt>{{jsxref("Global_Objects/Math/min", "Math.min([<var>x</var>[, <var>y</var>[, …]]])")}}</dt>
 <dd>Returns the smallest of zero or more numbers.</dd>
 <dt>{{jsxref("Global_Objects/Math/pow", "Math.pow(<var>x</var>, <var>y</var>)")}}</dt>
 <dd>Returns base <code><var>x</var></code> to the exponent power <code><var>y</var></code> (that is, <code><var>x</var><var><sup>y</sup></var></code>).</dd>
 <dt>{{jsxref("Global_Objects/Math/random", "Math.random()")}}</dt>
 <dd>Returns a pseudo-random number between <code>0</code> and <code>1</code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/round", "Math.round(<var>x</var>)")}}</dt>
 <dd>Returns the value of the number <code><var>x</var></code> rounded to the nearest integer.</dd>
 <dt>{{jsxref("Global_Objects/Math/sign", "Math.sign(<var>x</var>)")}}</dt>
 <dd>Returns the sign of the <code><var>x</var></code>, indicating whether <code><var>x</var></code> is positive, negative, or zero.</dd>
 <dt>{{jsxref("Global_Objects/Math/sin", "Math.sin(<var>x</var>)")}}</dt>
 <dd>Returns the sine of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/sinh", "Math.sinh(<var>x</var>)")}}</dt>
 <dd>Returns the hyperbolic sine of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/sqrt", "Math.sqrt(<var>x</var>)")}}</dt>
 <dd>Returns the positive square root of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/tan", "Math.tan(<var>x</var>)")}}</dt>
 <dd>Returns the tangent of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/tanh", "Math.tanh(<var>x</var>)")}}</dt>
 <dd>Returns the hyperbolic tangent of <code><var>x</var></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/trunc", "Math.trunc(<var>x</var>)")}}</dt>
 <dd>Returns the integer portion of <code><var>x</var></code>, removing any fractional digits.</dd>
</dl>

<h2 id="Examples">Examples</h2>

<h3 id="Converting_between_degrees_and_radians">Converting between degrees and radians</h3>

<p>The trigonometric functions <code>sin()</code>, <code>cos()</code>, <code>tan()</code>, <code>asin()</code>, <code>acos()</code>, <code>atan()</code>, and <code>atan2()</code> expect (and return) angles in <em>radians</em>.</p>

<p>Since humans tend to think in degrees, and some functions (such as CSS transforms) can accept degrees, it is a good idea to keep functions handy that convert between the two:</p>

<pre class="brush: js"><code class="language-js"><span class="keyword token">function</span> <span class="function token">degToRad</span><span class="punctuation token">(</span><span class="parameter token">degrees</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
  <span class="keyword token">return</span> degrees <span class="operator token">*</span> (Math<span class="punctuation token">.</span><span class="constant token">PI</span> <span class="operator token">/</span> <span class="number token">180</span><span class="punctuation token">);</span>
<span class="punctuation token">}</span><span class="punctuation token">;</span></code>

function radToDeg(rad) {
  return rad / (Math.PI / 180);
};</pre>

<h3 id="Calculating_the_height_of_an_equalateral_triangle">Calculating the height of an equalateral triangle</h3>

<p>If we want to calculate the height of an equalateral triangle, and we know its side length is 100, we can use the formulae <em>length of the adjacent multiplied by the tangent of the angle is equal to the opposite.</em></p>

<p><img alt="" src="https://mdn.mozillademos.org/files/14829/trigonometry.png" style="display: block; margin: 0 auto;"></p>

<p>In JavaScript, we can do this with the following:</p>

<pre class="brush: js">50 * Math.tan(degToRad(60)).</pre>

<p>We use our <code>degToRad()</code> function to convert 60 degrees to radians, as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/tan"><code>Math.tan()</code></a> expects an input value in radians.</p>

<h3 id="Returning_a_random_integer_between_two_bounds">Returning a random integer between two bounds</h3>

<p>This can be achieved with a combination of <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random">Math.random()</a></code> and <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor">Math.floor()</a></code>:</p>

<pre class="brush: js">function random(min, max) {
  const num = Math.floor(Math.random() * (max - min + 1)) + min;
  return num;
}

random(1, 10);</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-math-object', 'Math')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>



<p>{{Compat("javascript.builtins.Math")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Number")}}</li>
</ul>