File size: 2,591 Bytes
780c9fe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
---
title: Math.f16round()
short-title: f16round()
slug: Web/JavaScript/Reference/Global_Objects/Math/f16round
page-type: javascript-static-method
browser-compat: javascript.builtins.Math.f16round
sidebar: jsref
---
The **`Math.f16round()`** static method returns the nearest [16-bit half precision](https://en.wikipedia.org/wiki/Half-precision_floating-point_format) float representation of a number.
{{InteractiveExample("JavaScript Demo: Math.f16round()")}}
```js interactive-example
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
```js-nolint
Math.f16round(doubleFloat)
```
### Parameters
- `doubleFloat`
- : A number.
### Return value
The nearest [16-bit half precision](https://en.wikipedia.org/wiki/Half-precision_floating-point_format) float representation of `doubleFloat`.
## Description
`Math.f16round` is the 16-bit counterpart of {{jsxref("Math.fround()")}}. It is intended to smooth some rough edges when interacting with float16 numbers, such as when reading from a {{jsxref("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, {{jsxref("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:
```js
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:
```js
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:
```js
Math.f16round(100000); // Infinity
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Polyfill of `Math.f16round` in `core-js`](https://github.com/zloirock/core-js#float16-methods)
- [es-shims polyfill of `Math.f16round`](https://www.npmjs.com/package/math.f16round)
- {{jsxref("Math.fround()")}}
- {{jsxref("Math.round()")}}
|