JavaScript Wiskunde Voorwerp
Die JavaScript Math-objek laat jou toe om wiskundige take op getalle uit te voer.
Voorbeeld
Math.PI;
Die Math Object
Anders as ander voorwerpe, het die Math-voorwerp geen konstruktor nie.
Die Wiskunde-voorwerp is staties.
Alle metodes en eienskappe kan gebruik word sonder om eers 'n Math-objek te skep.
Wiskunde-eienskappe (konstante)
Die sintaksis vir enige Wiskunde-eienskap is: .Math.property
JavaScript bied 8 wiskundige konstantes wat as Wiskunde-eienskappe verkry kan word:
Voorbeeld
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
Wiskunde Metodes
Die sintaksis vir Wiskunde enige metodes is:Math.method(number)
Getal tot heelgetal
Daar is 4 algemene metodes om 'n getal tot 'n heelgetal af te rond:
Wiskunderonde(x) | Wys x afgerond tot sy naaste heelgetal |
Math.plafon(x) | Wys x afgerond tot sy naaste heelgetal |
Math.floor(x) | Wys x afgerond tot sy naaste heelgetal |
Math.trunc(x) | Wys die heelgetaldeel van x ( nuut in ES6 ) |
Math.round()
Math.round(x)
gee die naaste heelgetal terug:
Voorbeelde
Math.round(4.6);
Math.round(4.5);
Math.round(4.4);
Math.ceil()
Math.ceil(x)
gee die waarde van x terug afgerond tot sy naaste heelgetal:
Voorbeeld
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)
gee die waarde van x terug afgerond tot sy naaste heelgetal:
Voorbeeld
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)
gee die heelgetaldeel van x terug:
Voorbeeld
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)
gee terug as x negatief, nul of positief is:
Voorbeeld
Math.sign(-4);
Math.sign(0);
Math.sign(4);
Math.trunc() en Math.sign() is by JavaScript 2015 - ES6 gevoeg .
Math.pow()
Math.pow(x, y)
gee die waarde van x terug na die mag van y:
Voorbeeld
Math.pow(8, 2);
Math.sqrt()
Math.sqrt(x)
gee die vierkantswortel van x terug:
Voorbeeld
Math.sqrt(64);
Math.abs()
Math.abs(x)
gee die absolute (positiewe) waarde van x terug:
Voorbeeld
Math.abs(-4.7);
Math.sin ()
Math.sin(x)
gee die sinus ('n waarde tussen -1 en 1) van die hoek x terug (aangegee in radiale).
As jy grade in plaas van radiale wil gebruik, moet jy grade na radiale omskakel:
Hoek in radiale = Hoek in grade x PI / 180.
Voorbeeld
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
Math.cos()
Math.cos(x)
gee die cosinus ('n waarde tussen -1 en 1) van die hoek x terug (aangegee in radiale).
As jy grade in plaas van radiale wil gebruik, moet jy grade na radiale omskakel:
Hoek in radiale = Hoek in grade x PI / 180.
Voorbeeld
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
Math.min() en Math.max()
Math.min()
en Math.max()
kan gebruik word om die laagste of hoogste waarde in 'n lys argumente te vind:
Voorbeeld
Math.min(0, 150, 30, 20, -8, -200);
Voorbeeld
Math.max(0, 150, 30, 20, -8, -200);
Math.random()
Math.random()
gee 'n ewekansige getal tussen 0 (inklusief) en 1 (eksklusief):
Voorbeeld
Math.random();
Jy sal meer oor Math.random()
in die volgende hoofstuk van hierdie tutoriaal leer.
Die Math.log()-metode
Math.log(x)
gee die natuurlike logaritme van x terug.
Die natuurlike logaritme gee die tyd terug wat nodig is om 'n sekere vlak van groei te bereik:
Voorbeelde
Math.log(1);
Math.log(2);
Math.log(3);
Math.E en Math.log() is tweelinge.
Hoeveel keer moet ons Math.E vermenigvuldig om 10 te kry?
Math.log(10);
Die Math.log2() Metode
Math.log2(x)
gee die basis 2-logaritme van x terug.
Hoeveel keer moet ons 2 vermenigvuldig om 8 te kry?
Math.log2(8);
Die Math.log10() Metode
Math.log10(x)
gee die basis 10-logaritme van x terug.
Hoeveel keer moet ons 10 vermenigvuldig om 1000 te kry?
Math.log10(1000);
JavaScript Wiskunde Metodes
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) |
Voltooi Wiskundeverwysing
Vir 'n volledige verwysing, gaan na ons Volledige Wiskunde-objekverwysing .
Die verwysing bevat beskrywings en voorbeelde van alle Wiskunde eienskappe en metodes.