JavaScript -Mathematikobjekt
Mit dem JavaScript-Math-Objekt können Sie mathematische Aufgaben mit Zahlen ausführen.
Beispiel
Math.PI;
Das Math-Objekt
Im Gegensatz zu anderen Objekten hat das Math-Objekt keinen Konstruktor.
Das Math-Objekt ist statisch.
Alle Methoden und Eigenschaften können verwendet werden, ohne zuerst ein Math-Objekt zu erstellen.
Mathematische Eigenschaften (Konstanten)
Die Syntax für alle Math-Eigenschaften lautet: .Math.property
JavaScript bietet 8 mathematische Konstanten, auf die als mathematische Eigenschaften zugegriffen werden kann:
Beispiel
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
Mathematische Methoden
Die Syntax für alle Math-Methoden lautet:Math.method(number)
Zahl zu Ganzzahl
Es gibt 4 gängige Methoden, um eine Zahl auf eine ganze Zahl zu runden:
Math.Runde(x) | Gibt x auf die nächste ganze Zahl gerundet zurück |
Math.ceil(x) | Gibt x aufgerundet auf die nächste ganze Zahl zurück |
Math.Etage(x) | Gibt x abgerundet auf die nächste ganze Zahl zurück |
Math.trunc(x) | Gibt den ganzzahligen Teil von x zurück ( neu in ES6 ) |
Math.round()
Math.round(x)
gibt die nächste ganze Zahl zurück:
Beispiele
Math.round(4.6);
Math.round(4.5);
Math.round(4.4);
Math.ceil()
Math.ceil(x)
gibt den Wert von x aufgerundet auf die nächste ganze Zahl zurück:
Beispiel
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)
gibt den Wert von x abgerundet auf die nächste ganze Zahl zurück:
Beispiel
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)
gibt den ganzzahligen Teil von x zurück:
Beispiel
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)
gibt zurück, wenn x negativ, null oder positiv ist:
Beispiel
Math.sign(-4);
Math.sign(0);
Math.sign(4);
Math.trunc() und Math.sign() wurden zu JavaScript 2015 – ES6 hinzugefügt .
Math.pow()
Math.pow(x, y)
gibt den Wert von x hoch y zurück:
Beispiel
Math.pow(8, 2);
Math.sqrt()
Math.sqrt(x)
liefert die Quadratwurzel von x:
Beispiel
Math.sqrt(64);
Math.abs()
Math.abs(x)
gibt den absoluten (positiven) Wert von x zurück:
Beispiel
Math.abs(-4.7);
Math.sin ()
Math.sin(x)
liefert den Sinus (Wert zwischen -1 und 1) des Winkels x (angegeben im Bogenmaß).
Wenn Sie Grad statt Bogenmaß verwenden möchten, müssen Sie Grad in Bogenmaß umwandeln:
Winkel im Bogenmaß = Winkel in Grad x PI / 180.
Beispiel
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
Math.cos()
Math.cos(x)
gibt den Kosinus (einen Wert zwischen -1 und 1) des Winkels x (angegeben im Bogenmaß) zurück.
Wenn Sie Grad statt Bogenmaß verwenden möchten, müssen Sie Grad in Bogenmaß umwandeln:
Winkel im Bogenmaß = Winkel in Grad x PI / 180.
Beispiel
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
Math.min() und Math.max()
Math.min()
und Math.max()
kann verwendet werden, um den niedrigsten oder höchsten Wert in einer Liste von Argumenten zu finden:
Beispiel
Math.min(0, 150, 30, 20, -8, -200);
Beispiel
Math.max(0, 150, 30, 20, -8, -200);
Math.random()
Math.random()
gibt eine Zufallszahl zwischen 0 (einschließlich) und 1 (ausschließlich) zurück:
Beispiel
Math.random();
Math.random()
Im nächsten Kapitel dieses Tutorials erfahren Sie mehr darüber .
Die Math.log()-Methode
Math.log(x)
gibt den natürlichen Logarithmus von x zurück.
Der natürliche Logarithmus gibt die Zeit an, die benötigt wird, um ein bestimmtes Wachstumsniveau zu erreichen:
Beispiele
Math.log(1);
Math.log(2);
Math.log(3);
Math.E und Math.log() sind Zwillinge.
Wie oft müssen wir Math.E multiplizieren, um 10 zu erhalten?
Math.log(10);
Die Math.log2()-Methode
Math.log2(x)
gibt den Logarithmus zur Basis 2 von x zurück.
Wie oft müssen wir 2 multiplizieren, um 8 zu erhalten?
Math.log2(8);
Die Math.log10()-Methode
Math.log10(x)
gibt den Logarithmus zur Basis 10 von x zurück.
Wie oft müssen wir 10 multiplizieren, um 1000 zu erhalten?
Math.log10(1000);
JavaScript-Mathematikmethoden
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) |
Vollständige Mathe-Referenz
Eine vollständige Referenz finden Sie in unserer Complete Math Object Reference .
Die Referenz enthält Beschreibungen und Beispiele aller mathematischen Eigenschaften und Methoden.