Math.f16round()

Math.f16round() 静态方法返回数字最接近的 16 位半精度 浮点表示形式。

¥The Math.f16round() static method returns the nearest 16-bit half precision float representation of a number.

Try it

语法

¥Syntax

js
Math.f16round(doubleFloat)

参数

¥Parameters

doubleFloat

一个号码。

返回值

¥Return value

doubleFloat 最接近的 16 位半精度 浮点表示。

¥The nearest 16-bit half precision float representation of doubleFloat.

描述

¥Description

Math.f16roundMath.fround() 的 16 位对应项。它旨在在与 float16 数字交互时平滑一些粗糙的边缘,例如从 Float16Array 读取时。在内部,JavaScript 继续将数字视为 64 位浮点数,它只对尾数的第 10 位执行 "四舍五入到偶数",并将所有后续尾数位设置为 0。如果数字超出 16 位浮点数的范围,则返回 Infinity-Infinity

¥Math.f16round is the 16-bit counterpart of Math.fround(). It is intended to smooth some rough edges when interacting with float16 numbers, such as when reading from a Float16Array. Internally, JavaScript continues to treat the number as a 64-bit float, it just performs a "round to even" on the 10th bit of the mantissa, and sets all following mantissa bits to 0. If the number is outside the range of a 16-bit float, Infinity or -Infinity is returned.

因为 f16round()Math 的静态方法,所以你始终将其用作 Math.f16round(),而不是用作你创建的 Math 对象的方法(Math 不是构造函数)。

¥Because f16round() is a static method of Math, you always use it as Math.f16round(), rather than as a method of a Math object you created (Math is not a constructor).

示例

¥Examples

使用 Math.f16round()

¥Using Math.f16round()

数字 1.5 可以在二进制数字系统中精确表示,并且在 16 位和 64 位中相同:

¥The number 1.5 can be precisely represented in the binary numeral system, and is identical in 16-bit and 64-bit:

js
Math.f16round(1.5); // 1.5
Math.f16round(1.5) === 1.5; // true

但是,数字 1.337 无法在二进制数字系统中精确表示,因此它在 16 位和 64 位中有所不同:

¥However, the number 1.337 cannot be precisely represented in the binary numeral system, so it differs in 16-bit and 64-bit:

js
Math.f16round(1.337); // 1.3369140625
Math.f16round(1.337) === 1.337; // false

100000 对于 16 位浮点数来说太大,因此返回 Infinity

¥100000 is too big for a 16-bit float, so Infinity is returned:

js
Math.f16round(100000); // Infinity

规范

Specification
Float16Array
# sec-math.f16round

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看