Matematika
Math
jedan je od globalnih ili standardnih ugrađenih objekata JavaScript-a i može se koristiti bilo gdje gdje možete koristiti JavaScript. Sadrži korisne konstante kao π i Euler je konstantna i funkcije kao što su floor()
, round()
i ceil()
.
U ovom ćemo članku pogledati primjere mnogih od tih funkcija. Ali prvo, naučimo više o Math
predmetu.
Primjer
Sljedeći primjer pokazuje kako koristiti Math
objekt za pisanje funkcije koja izračunava površinu kruga:
function calculateCircleArea(radius) { return Math.PI * Math.pow(radius, 2); } calculateCircleArea(1); // 3.141592653589793
Matematika maks
Math.max()
je funkcija koja vraća najveću vrijednost s popisa numeričkih vrijednosti prosljeđenih kao parametri. Ako se kao parametar prenese ne-numerička vrijednost, Math.max()
vratit će se NaN
.
Niz numeričkih vrijednosti može se proslijediti kao jedan parametar u Math.max()
upotrebu bilo spread (...)
ili apply
. Bilo koja od ovih metoda može, međutim, uspjeti kada količina vrijednosti polja postane previsoka.
Sintaksa
Math.max(value1, value2, value3, ...);
Parametri
Brojevi ili ograničeni niz brojeva.
Povratna vrijednost
Najveća od zadanih numeričkih vrijednosti ili NaN
ako je bilo koja zadana vrijednost ne numerička.
Primjeri
Brojevi kao parametri
Math.max(4, 13, 27, 0, -5); // returns 27
Nevažeći parametar
Math.max(4, 13, 27, 'eight', -5); // returns NaN
Niz kao parametar, koristeći Spread (…)
let numbers = [4, 13, 27, 0, -5]; Math.max(...numbers); // returns 27
Niz kao parametar, koristeći Apply
let numbers = [4, 13, 27, 0, -5]; Math.max.apply(null, numbers); // returns 27
Matematika Min
Funkcija Math.min () vraća najmanji od nula ili više brojeva.
Možete mu proslijediti bilo koji broj argumenata.
Math.min(7, 2, 9, -6); // returns -6
Matematika PI
Math.PI
je statično svojstvo predmeta Math i definira se kao omjer opsega kruga i njegovog promjera. Pi je otprilike 3,14149, a često ga predstavlja grčko slovo π.
Primjeri
Math.PI \\ 3.141592653589793
Više informacija:
MDN
Matematička pobjeda
Math.pow()
vraća vrijednost broja u moć drugog broja.
Sintaksa
Math.pow(base, exponent)
, gdje base
je osnovni broj i exponent
broj za povišenje base
.
pow()
je statička metoda Math
, stoga se uvijek naziva kao Math.pow()
metoda na drugom objektu.
Primjeri
Math.pow(5, 2); // 25 Math.pow(7, 4); // 2401 Math.pow(9, 0.5); // 3 Math.pow(-8, 2); // 64 Math.pow(-4, 3); // -64
Matematički kvadrat
Funkcija Math.sqrt()
vraća kvadratni korijen broja.
Ako se unese negativan broj, NaN
vraća se.
sqrt()
je statička metoda Math
, stoga se uvijek naziva kao Math.sqrt()
metoda na drugom objektu.
Sintaksa
Math.sqrt(x)
, gdje x
je broj.
Primjeri
Math.sqrt(25); // 5 Math.sqrt(169); // 13 Math.sqrt(3); // 1.732050807568 Math.sqrt(1); // 1 Math.sqrt(-5); // NaN
Matematički trunc
Math.trunc()
je metoda Math standardnog objekta koja vraća samo cjelobrojni dio određenog broja jednostavnim uklanjanjem frakcijskih jedinica. To rezultira ukupnim zaokruživanjem prema nuli. Bilo koji ulaz koji nije broj rezultirat će izlazom NaN.
Careful: This method is an ECMAScript 2015 (ES6) feature and thus is not supported by older browsers.
Examples
Math.trunc(0.1) // 0 Math.trunc(1.3) // 1 Math.trunc(-0.9) // -0 Math.trunc(-1.5) // -1 Math.trunc('foo') // NaN
Math Ceil
The Math.ceil()
is a method of the Math standard object that rounds a given number upwards to the next integer. Take note that for negative numbers this means that the number will get rounded “towards 0” instead of the number of greater absolute value (see examples).
Examples
Math.ceil(0.1) // 1 Math.ceil(1.3) // 2 Math.ceil(-0.9) // -0 Math.ceil(-1.5) // -1
Math Floor
Math.floor()
is a method of the Math standard object that rounds a given number downwards to the next integer. Take note that for negative numbers this means that the number will get rounded “away from 0” instead of to the number of smaller absolute value since Math.floor()
returns the largest integer less than or equal to the given number.
Examples
Math.floor(0.9) // 0 Math.floor(1.3) // 1 Math.floor(0.5) // 0 Math.floor(-0.9) // -1 Math.floor(-1.3) // -2
An application of math.floor: How to Create a JavaScript Slot Machine
For this exercise, we have to generate three random numbers using a specific formula and not the general one. Math.floor(Math.random() * (3 - 1 + 1)) + 1;
slotOne = Math.floor(Math.random() * (3 - 1 + 1)) + 1; slotTwo = Math.floor(Math.random() * (3 - 1 + 1)) + 1; slotThree = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
Another example: Finding the remainder
Example
5 % 2 = 1 because Math.floor(5 / 2) = 2 (Quotient) 2 * 2 = 4 5 - 4 = 1 (Remainder)
Usage
In mathematics, a number can be checked even or odd by checking the remainder of the division of the number by 2.
17 % 2 = 1 (17 is Odd) 48 % 2 = 0 (48 is Even)
Note Do not confuse it with modulus%
does not work well with negative numbers.
More math-related articles:
- Converting an am/pm clock to 24 hour time
- Simpson's rule
- What is a Hexagon?