Matematický objekt JavaScriptu
Objekt JavaScript Math umožňuje provádět matematické úlohy na číslech.
Příklad
Math.PI;
Matematický objekt
Na rozdíl od jiných objektů nemá objekt Math žádný konstruktor.
Objekt Math je statický.
Všechny metody a vlastnosti lze použít bez předchozího vytvoření objektu Math.
Matematické vlastnosti (konstanty)
Syntaxe jakékoli vlastnosti Math je : .Math.property
JavaScript poskytuje 8 matematických konstant, ke kterým lze přistupovat jako vlastnosti Math:
Příklad
Math.E // returns Euler's number
Math.PI // returns PI
Math.SQRT2 // returns the square root of 2
Math.SQRT1_2 // returns the square root of 1/2
Math.LN2 // returns the natural logarithm of 2
Math.LN10 // returns the natural logarithm of 10
Math.LOG2E // returns base 2 logarithm of E
Math.LOG10E // returns base 10 logarithm of E
Matematické metody
Syntaxe pro všechny metody Math je: Math.method(number)
Číslo na celé číslo
Existují 4 běžné způsoby, jak zaokrouhlit číslo na celé číslo:
Math.round(x) | Vrátí x zaokrouhlené na nejbližší celé číslo |
Math.ceil(x) | Vrátí x zaokrouhlené nahoru na nejbližší celé číslo |
Math.floor(x) | Vrátí x zaokrouhlené dolů na nejbližší celé číslo |
Math.trunc(x) | Vrátí celočíselnou část x ( nové v ES6 ) |
Math.round()
Math.round(x)
vrátí nejbližší celé číslo:
Příklady
Math.round(4.6);
Math.round(4.5);
Math.round(4.4);
Math.ceil()
Math.ceil(x)
vrátí hodnotu x zaokrouhlenou nahoru na nejbližší celé číslo:
Příklad
Math.ceil(4.9);
Math.ceil(4.7);
Math.ceil(4.4);
Math.ceil(4.2);
Math.ceil(-4.2);
Math.floor()
Math.floor(x)
vrátí hodnotu x zaokrouhlenou dolů na nejbližší celé číslo:
Příklad
Math.floor(4.9);
Math.floor(4.7);
Math.floor(4.4);
Math.floor(4.2);
Math.floor(-4.2);
Math.trunc()
Math.trunc(x)
vrátí celočíselnou část x:
Příklad
Math.trunc(4.9);
Math.trunc(4.7);
Math.trunc(4.4);
Math.trunc(4.2);
Math.trunc(-4.2);
Math.sign()
Math.sign(x)
vrátí, pokud je x záporné, nulové nebo kladné:
Příklad
Math.sign(-4);
Math.sign(0);
Math.sign(4);
Math.trunc() a Math.sign() byly přidány do JavaScriptu 2015 - ES6 .
Math.pow()
Math.pow(x, y)
vrátí hodnotu x na mocninu y:
Příklad
Math.pow(8, 2);
Math.sqrt()
Math.sqrt(x)
vrátí druhou odmocninu z x:
Příklad
Math.sqrt(64);
Math.abs()
Math.abs(x)
vrátí absolutní (kladnou) hodnotu x:
Příklad
Math.abs(-4.7);
Math.sin ()
Math.sin(x)
vrátí sinus (hodnotu mezi -1 a 1) úhlu x (zadaného v radiánech).
Pokud chcete místo radiánů použít stupně, musíte stupně převést na radiány:
Úhel v radiánech = Úhel ve stupních x PI / 180.
Příklad
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
Math.cos()
Math.cos(x)
vrátí kosinus (hodnotu mezi -1 a 1) úhlu x (zadaného v radiánech).
Pokud chcete místo radiánů použít stupně, musíte stupně převést na radiány:
Úhel v radiánech = Úhel ve stupních x PI / 180.
Příklad
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
Math.min() a Math.max()
Math.min()
a Math.max()
lze jej použít k nalezení nejnižší nebo nejvyšší hodnoty v seznamu argumentů:
Příklad
Math.min(0, 150, 30, 20, -8, -200);
Příklad
Math.max(0, 150, 30, 20, -8, -200);
Math.random()
Math.random()
vrátí náhodné číslo mezi 0 (včetně) a 1 (mimo):
Příklad
Math.random();
Více se dozvíte Math.random()
v další kapitole tohoto tutoriálu.
Metoda Math.log().
Math.log(x)
vrátí přirozený logaritmus x.
Přirozený logaritmus vrací čas potřebný k dosažení určité úrovně růstu:
Příklady
Math.log(1);
Math.log(2);
Math.log(3);
Math.E a Math.log() jsou dvojčata.
Kolikrát musíme vynásobit Math.E, abychom dostali 10?
Math.log(10);
Metoda Math.log2().
Math.log2(x)
vrátí základní 2 logaritmus x.
Kolikrát musíme vynásobit 2, abychom dostali 8?
Math.log2(8);
Metoda Math.log10().
Math.log10(x)
vrátí základní 10 logaritmus x.
Kolikrát musíme vynásobit 10, abychom dostali 1000?
Math.log10(1000);
Matematické metody JavaScriptu
Method | Description |
---|---|
abs(x) | Returns the absolute value of x |
acos(x) | Returns the arccosine of x, in radians |
acosh(x) | Returns the hyperbolic arccosine of x |
asin(x) | Returns the arcsine of x, in radians |
asinh(x) | Returns the hyperbolic arcsine of x |
atan(x) | Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians |
atan2(y, x) | Returns the arctangent of the quotient of its arguments |
atanh(x) | Returns the hyperbolic arctangent of x |
cbrt(x) | Returns the cubic root of x |
ceil(x) | Returns x, rounded upwards to the nearest integer |
cos(x) | Returns the cosine of x (x is in radians) |
cosh(x) | Returns the hyperbolic cosine of x |
exp(x) | Returns the value of Ex |
floor(x) | Returns x, rounded downwards to the nearest integer |
log(x) | Returns the natural logarithm (base E) of x |
max(x, y, z, ..., n) | Returns the number with the highest value |
min(x, y, z, ..., n) | Returns the number with the lowest value |
pow(x, y) | Returns the value of x to the power of y |
random() | Returns a random number between 0 and 1 |
round(x) | Rounds x to the nearest integer |
sign(x) | Returns if x is negative, null or positive (-1, 0, 1) |
sin(x) | Returns the sine of x (x is in radians) |
sinh(x) | Returns the hyperbolic sine of x |
sqrt(x) | Returns the square root of x |
tan(x) | Returns the tangent of an angle |
tanh(x) | Returns the hyperbolic tangent of a number |
trunc(x) | Returns the integer part of a number (x) |
Kompletní matematický odkaz
Úplnou referenci najdete v naší kompletní referenci matematických objektů .
Odkaz obsahuje popisy a příklady všech matematických vlastností a metod.