大网数据广告活动
Web 标准与前端开发

Math.f16round()

· 52CE 我爱测

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

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

JavaScript Demo: Math.f16round()

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)

Parameters

  • doubleFloat
    • : A number.

Return value

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

Description

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.

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

Using Math.f16round()

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

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

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

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

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

Math.f16round(100000); // Infinity

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。