Math.f16round()
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Die Math.f16round()
-statische Methode gibt die nächste 16-Bit-Halbpräzisions-Fließkommadarstellung einer Zahl zurück.
Probieren Sie es aus
console.log(Math.f16round(5.5));
// Expected output: 5.5
console.log(Math.f16round(5.05));
// Expected output: 5.05078125
console.log(Math.f16round(5));
// Expected output: 5
console.log(Math.f16round(-5.05));
// Expected output: -5.05078125
Syntax
Math.f16round(doubleFloat)
Parameter
doubleFloat
-
Eine Zahl.
Rückgabewert
Die nächste 16-Bit-Halbpräzisions-Fließkommadarstellung von doubleFloat
.
Beschreibung
Math.f16round
ist das 16-Bit-Pendant zu Math.fround()
. Es soll einige Schwierigkeiten beim Arbeiten mit float16-Zahlen glätten, wie z. B. beim Lesen aus einem Float16Array
. Intern behandelt JavaScript die Zahl weiterhin als 64-Bit-Fließkommazahl. Es führt lediglich ein "Round to Even" auf der 10. Stelle der Mantisse durch und setzt alle folgenden Mantissen-Bits auf 0
. Befindet sich die Zahl außerhalb des Bereichs eines 16-Bit-Fließkommazahlformats, wird Infinity
oder -Infinity
zurückgegeben.
Da f16round()
eine statische Methode von Math
ist, wird sie immer als Math.f16round()
verwendet und nicht als Methode eines von Ihnen erstellten Math
-Objekts (Math
ist kein Konstruktor).
Beispiele
Verwendung von Math.f16round()
Die Zahl 1.5 kann im binären Zahlensystem genau dargestellt werden und ist identisch in 16-Bit und 64-Bit:
Math.f16round(1.5); // 1.5
Math.f16round(1.5) === 1.5; // true
Die Zahl 1.337 kann jedoch im binären Zahlensystem nicht genau dargestellt werden, daher unterscheidet sie sich in 16-Bit und 64-Bit:
Math.f16round(1.337); // 1.3369140625
Math.f16round(1.337) === 1.337; // false
100000 ist zu groß für ein 16-Bit-Fließkommazahlformat, daher wird Infinity
zurückgegeben:
Math.f16round(100000); // Infinity
Spezifikationen
Specification |
---|
Float16Array # sec-math.f16round |
Browser-Kompatibilität
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
f16round |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support