Objek Matematika JavaScript
Objek JavaScript Math memungkinkan Anda untuk melakukan tugas matematika pada angka.
Contoh
Math.PI;
Objek Matematika
Tidak seperti objek lain, objek Math tidak memiliki konstruktor.
Objek Math adalah statis.
Semua metode dan properti dapat digunakan tanpa membuat objek Math terlebih dahulu.
Properti Matematika (Konstanta)
Sintaks untuk setiap properti Math adalah : .Math.property
JavaScript menyediakan 8 konstanta matematika yang dapat diakses sebagai properti Matematika:
Contoh
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
Metode Matematika
Sintaks untuk Math metode apa pun adalah: Math.method(number)
Bilangan ke Bilangan Bulat
Ada 4 metode umum untuk membulatkan angka ke bilangan bulat:
Matematika.bulat(x) | Mengembalikan x dibulatkan ke bilangan bulat terdekat |
Matematika.ceil(x) | Mengembalikan x dibulatkan ke atas ke bilangan bulat terdekat |
Matematika.lantai(x) | Mengembalikan x dibulatkan ke bawah ke bilangan bulat terdekat |
Matematika.trunc(x) | Mengembalikan bagian bilangan bulat dari x ( baru di ES6 ) |
Matematika.bulat()
Math.round(x)
mengembalikan bilangan bulat terdekat:
Contoh
Math.round(4.6);
Math.round(4.5);
Math.round(4.4);
matematika.ceil()
Math.ceil(x)
mengembalikan nilai x yang dibulatkan ke atas ke bilangan bulat terdekat:
Contoh
Math.ceil(4.9);
Math.ceil(4.7);
Math.ceil(4.4);
Math.ceil(4.2);
Math.ceil(-4.2);
Matematika.lantai()
Math.floor(x)
mengembalikan nilai x yang dibulatkan ke bawah ke bilangan bulat terdekat:
Contoh
Math.floor(4.9);
Math.floor(4.7);
Math.floor(4.4);
Math.floor(4.2);
Math.floor(-4.2);
Matematika.trunc()
Math.trunc(x)
mengembalikan bagian bilangan bulat dari x:
Contoh
Math.trunc(4.9);
Math.trunc(4.7);
Math.trunc(4.4);
Math.trunc(4.2);
Math.trunc(-4.2);
matematika.tanda()
Math.sign(x)
kembali jika x negatif, nol atau positif:
Contoh
Math.sign(-4);
Math.sign(0);
Math.sign(4);
Math.trunc() dan Math.sign() ditambahkan ke JavaScript 2015 - ES6 .
Matematika.pow()
Math.pow(x, y)
mengembalikan nilai x ke pangkat y:
Contoh
Math.pow(8, 2);
Matematika.sqrt()
Math.sqrt(x)
mengembalikan akar kuadrat dari x:
Contoh
Math.sqrt(64);
matematika.ab()
Math.abs(x)
mengembalikan nilai absolut (positif) dari x:
Contoh
Math.abs(-4.7);
matematika.sin()
Math.sin(x)
mengembalikan sinus (nilai antara -1 dan 1) dari sudut x (diberikan dalam radian).
Jika Anda ingin menggunakan derajat alih-alih radian, Anda harus mengonversi derajat ke radian:
Sudut dalam radian = Sudut dalam derajat x PI / 180.
Contoh
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
matematika.cos()
Math.cos(x)
mengembalikan kosinus (nilai antara -1 dan 1) dari sudut x (diberikan dalam radian).
Jika Anda ingin menggunakan derajat alih-alih radian, Anda harus mengonversi derajat ke radian:
Sudut dalam radian = Sudut dalam derajat x PI / 180.
Contoh
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
Math.min() dan Math.max()
Math.min()
dan Math.max()
dapat digunakan untuk menemukan nilai terendah atau tertinggi dalam daftar argumen:
Contoh
Math.min(0, 150, 30, 20, -8, -200);
Contoh
Math.max(0, 150, 30, 20, -8, -200);
matematika.acak()
Math.random()
mengembalikan nomor acak antara 0 (inklusif), dan 1 (eksklusif):
Contoh
Math.random();
Anda akan mempelajari lebih lanjut Math.random()
di bab berikutnya dari tutorial ini.
Metode Math.log()
Math.log(x)
mengembalikan logaritma natural dari x.
Logaritma natural mengembalikan waktu yang dibutuhkan untuk mencapai tingkat pertumbuhan tertentu:
Contoh
Math.log(1);
Math.log(2);
Math.log(3);
Math.E dan Math.log() adalah kembar.
Berapa kali kita harus mengalikan Math.E untuk mendapatkan 10?
Math.log(10);
Metode Math.log2()
Math.log2(x)
mengembalikan logaritma basis 2 dari x.
Berapa kali kita harus mengalikan 2 untuk mendapatkan 8?
Math.log2(8);
Metode Math.log10()
Math.log10(x)
mengembalikan logaritma basis 10 dari x.
Berapa kali kita harus mengalikan 10 untuk mendapatkan 1000?
Math.log10(1000);
Metode Matematika 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) |
Referensi Matematika Lengkap
Untuk referensi lengkap, buka Referensi Objek Matematika Lengkap kami .
Referensi berisi deskripsi dan contoh semua properti dan metode Math.