Obiekt matematyczny JavaScript
Obiekt JavaScript Math umożliwia wykonywanie zadań matematycznych na liczbach.
Przykład
Math.PI;
Obiekt matematyczny
W przeciwieństwie do innych obiektów, obiekt Math nie ma konstruktora.
Obiekt Math jest statyczny.
Wszystkie metody i właściwości mogą być używane bez wcześniejszego tworzenia obiektu Math.
Właściwości matematyczne (stałe)
Składnia dowolnej właściwości Math to : .Math.property
JavaScript udostępnia 8 stałych matematycznych, do których można uzyskać dostęp jako właściwości matematyczne:
Przykład
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
Metody matematyczne
Składnia dowolnych metod matematycznych to:Math.method(number)
Liczba do liczby całkowitej
Istnieją 4 popularne metody zaokrąglania liczby do liczby całkowitej:
Math.round(x) | Zwraca x zaokrąglone do najbliższej liczby całkowitej |
Math.ceil(x) | Zwraca x zaokrąglone w górę do najbliższej liczby całkowitej |
Matematyka.podłoga(x) | Zwraca x zaokrąglone w dół do najbliższej liczby całkowitej |
Math.trunc(x) | Zwraca część całkowitą x ( nowość w ES6 ) |
Math.round()
Math.round(x)
zwraca najbliższą liczbę całkowitą:
Przykłady
Math.round(4.6);
Math.round(4.5);
Math.round(4.4);
Matematyka.ceil()
Math.ceil(x)
zwraca wartość x zaokrągloną w górę do najbliższej liczby całkowitej:
Przykład
Math.ceil(4.9);
Math.ceil(4.7);
Math.ceil(4.4);
Math.ceil(4.2);
Math.ceil(-4.2);
Matematyka.podłoga()
Math.floor(x)
zwraca wartość x zaokrągloną w dół do najbliższej liczby całkowitej:
Przykład
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)
zwraca część całkowitą x:
Przykład
Math.trunc(4.9);
Math.trunc(4.7);
Math.trunc(4.4);
Math.trunc(4.2);
Math.trunc(-4.2);
Matematyka.znak()
Math.sign(x)
zwraca, jeśli x jest ujemne, puste lub dodatnie:
Przykład
Math.sign(-4);
Math.sign(0);
Math.sign(4);
Math.trunc() i Math.sign() zostały dodane do JavaScript 2015 - ES6 .
Matematyka.pow()
Math.pow(x, y)
zwraca wartość x do potęgi y:
Przykład
Math.pow(8, 2);
Matematyka.sqrt()
Math.sqrt(x)
zwraca pierwiastek kwadratowy z x:
Przykład
Math.sqrt(64);
Math.abs()
Math.abs(x)
zwraca bezwzględną (dodatnią) wartość x:
Przykład
Math.abs(-4.7);
Math.sin ()
Math.sin(x)
zwraca sinus (wartość od -1 do 1) kąta x (podanego w radianach).
Jeśli chcesz używać stopni zamiast radianów, musisz przekonwertować stopnie na radiany:
Kąt w radianach = Kąt w stopniach x PI / 180.
Przykład
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
Matematyka.cos()
Math.cos(x)
zwraca cosinus (wartość od -1 do 1) kąta x (podanego w radianach).
Jeśli chcesz używać stopni zamiast radianów, musisz przekonwertować stopnie na radiany:
Kąt w radianach = Kąt w stopniach x PI / 180.
Przykład
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
Math.min() i Math.max()
Math.min()
i Math.max()
może być użyty do znalezienia najniższej lub najwyższej wartości na liście argumentów:
Przykład
Math.min(0, 150, 30, 20, -8, -200);
Przykład
Math.max(0, 150, 30, 20, -8, -200);
Matematyka.losowy()
Math.random()
zwraca losową liczbę od 0 (włącznie) do 1 (wyłącznie):
Przykład
Math.random();
Więcej o tym dowiesz się Math.random()
w kolejnym rozdziale tego samouczka.
Metoda Math.log()
Math.log(x)
zwraca logarytm naturalny z x.
Logarytm naturalny zwraca czas potrzebny do osiągnięcia określonego poziomu wzrostu:
Przykłady
Math.log(1);
Math.log(2);
Math.log(3);
Math.E i Math.log() są bliźniakami.
Ile razy musimy pomnożyć Math.E, aby otrzymać 10?
Math.log(10);
Metoda Math.log2()
Math.log2(x)
zwraca logarytm o podstawie 2 z x.
Ile razy musimy pomnożyć 2, aby otrzymać 8?
Math.log2(8);
Metoda Math.log10()
Math.log10(x)
zwraca logarytm o podstawie 10 z x.
Ile razy musimy pomnożyć 10, aby otrzymać 1000?
Math.log10(1000);
Metody matematyczne JavaScript
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) |
Pełne odniesienie do matematyki
Aby uzyskać pełne informacje, przejdź do naszego Kompletnego odniesienia do obiektów matematycznych .
Odniesienie zawiera opisy i przykłady wszystkich właściwości i metod matematycznych.