كائن JavaScript الرياضي
يسمح لك كائن JavaScript Math بأداء مهام رياضية على الأرقام.
مثال
Math.PI;
الكائن الرياضي
على عكس الكائنات الأخرى ، لا يحتوي الكائن الرياضي على مُنشئ.
كائن الرياضيات ثابت.
يمكن استخدام جميع الأساليب والخصائص دون إنشاء كائن رياضي أولاً.
خصائص الرياضيات (الثوابت)
بناء الجملة لأي خاصية رياضية هو :.Math.property
يوفر JavaScript 8 ثوابت رياضية يمكن الوصول إليها كخصائص رياضية:
مثال
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
طرق الرياضيات
صيغة أي طرق للرياضيات هي: Math.method(number)
عدد صحيح
هناك 4 طرق شائعة لتقريب رقم إلى عدد صحيح:
Math.round (x) | إرجاع x مقربًا إلى أقرب عدد صحيح |
الرياضيات.ceil (x) | لإرجاع x مقربًا لأعلى إلى أقرب عدد صحيح |
الرياضيات.الطابق (x) | إرجاع x مقربًا لأسفل إلى أقرب عدد صحيح |
Math.trunc (x) | إرجاع الجزء الصحيح من x ( جديد في ES6 ) |
Math.round ()
Math.round(x)
تُرجع أقرب عدد صحيح:
أمثلة
Math.round(4.6);
Math.round(4.5);
Math.round(4.4);
Math.ceil ()
Math.ceil(x)
تُرجع قيمة x مقربًا لأعلى إلى أقرب عدد صحيح لها:
مثال
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)
تُرجع قيمة x مقربة إلى أقرب عدد صحيح لها:
مثال
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)
تُرجع الجزء الصحيح من x:
مثال
Math.trunc(4.9);
Math.trunc(4.7);
Math.trunc(4.4);
Math.trunc(4.2);
Math.trunc(-4.2);
الرياضيات.
Math.sign(x)
تُرجع إذا كانت x سالبة أو خالية أو موجبة:
مثال
Math.sign(-4);
Math.sign(0);
Math.sign(4);
تمت إضافة Math.trunc () و Math.sign () إلى JavaScript 2015 - ES6 .
Math.pow ()
Math.pow(x, y)
تُرجع قيمة x إلى أس y:
مثال
Math.pow(8, 2);
الرياضيات.
Math.sqrt(x)
تُرجع الجذر التربيعي لـ x:
مثال
Math.sqrt(64);
Math.abs ()
Math.abs(x)
تُرجع القيمة المطلقة (الموجبة) لـ x:
مثال
Math.abs(-4.7);
Math.sin ()
Math.sin(x)
تُرجع الجيب (قيمة بين -1 و 1) للزاوية x (مقدرة بالتقدير الدائري).
إذا كنت تريد استخدام الدرجات بدلاً من الراديان ، فعليك تحويل الدرجات إلى الراديان:
الزاوية بالتقدير الدائري = الزاوية بالدرجات × PI / 180.
مثال
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
Math.cos ()
Math.cos(x)
تُرجع جيب التمام (قيمة بين -1 و 1) للزاوية x (مقدرة بالتقدير الدائري).
إذا كنت تريد استخدام الدرجات بدلاً من الراديان ، فعليك تحويل الدرجات إلى الراديان:
الزاوية بالتقدير الدائري = الزاوية بالدرجات × PI / 180.
مثال
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
Math.min () و Math.max ()
Math.min()
ويمكن Math.max()
استخدامها للعثور على أدنى أو أعلى قيمة في قائمة الوسيطات:
مثال
Math.min(0, 150, 30, 20, -8, -200);
مثال
Math.max(0, 150, 30, 20, -8, -200);
Math.random ()
Math.random()
تُرجع رقمًا عشوائيًا بين 0 (ضمنيًا) و 1 (حصريًا):
مثال
Math.random();
سوف تتعلم المزيد Math.random()
في الفصل التالي من هذا البرنامج التعليمي.
طريقة Math.log ()
Math.log(x)
تُرجع اللوغاريتم الطبيعي لـ x.
يُرجع اللوغاريتم الطبيعي الوقت اللازم للوصول إلى مستوى معين من النمو:
أمثلة
Math.log(1);
Math.log(2);
Math.log(3);
Math.E و Math.log () توأمان.
كم مرة يجب أن نضرب Math.E لنحصل على 10؟
Math.log(10);
طريقة Math.log2 ()
Math.log2(x)
تُرجع اللوغاريتم الأساسي 2 لـ x.
كم مرة يجب أن نضرب 2 لنحصل على 8؟
Math.log2(8);
طريقة Math.log10 ()
Math.log10(x)
تُرجع لوغاريتم x للأساس 10.
كم مرة يجب أن نضرب 10 لنحصل على 1000؟
Math.log10(1000);
طرق الرياضيات جافا سكريبت
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) |
المرجع الرياضي الكامل
للحصول على مرجع كامل ، انتقل إلى مرجع كائن الرياضيات الكامل .
يحتوي المرجع على أوصاف وأمثلة لجميع خصائص وأساليب الرياضيات.