Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Math
  6. f16round()

Math.f16round()

Baseline 2025
Newly available

Since ⁨April 2025⁩, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

TheMath.f16round() static method returns the nearest16-bit half precision float representation of a number.

Try it

console.log(Math.f16round(5.5));// Expected output: 5.5console.log(Math.f16round(5.05));// Expected output: 5.05078125console.log(Math.f16round(5));// Expected output: 5console.log(Math.f16round(-5.05));// Expected output: -5.05078125

Syntax

js
Math.f16round(doubleFloat)

Parameters

doubleFloat

A number.

Return value

The nearest16-bit half precision float representation ofdoubleFloat.

Description

Math.f16round is the 16-bit counterpart ofMath.fround(). It is intended to smooth some rough edges when interacting with float16 numbers, such as when reading from aFloat16Array. 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 to0. If the number is outside the range of a 16-bit float,Infinity or-Infinity is returned.

Becausef16round() is a static method ofMath, you always use it asMath.f16round(), rather than as a method of aMath 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:

js
Math.f16round(1.5); // 1.5Math.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:

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

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

js
Math.f16round(100000); // Infinity

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-math.f16round

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp