Float16Array
Float16Array stores half-precision floating-point values in a typed array. It is useful when smaller numeric representation matters for memory footprint or GPU-oriented workflows.
Overview
Float16Array stores half-precision floating-point values in a typed array. It is useful when smaller numeric representation matters for memory footprint or GPU-oriented workflows.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 135 | 135 | 129 | 18.2 | 135 | 18.2 | |
| The getFloat16() method of DataView instances reads 2 bytes starting at the specified byte offset of this DataView and interprets them as a 16-bit floating point number. There is no alignment constraint; multi-byte values may be fetched from any offset within bounds. | 135 | 135 | 129 | 18.2 | 135 | 18.2 |
| The setFloat16() method of DataView instances takes a number and stores it as a 16-bit floating point number in the 2 bytes starting at the specified byte offset of this DataView. There is no alignment constraint; multi-byte values may be stored at any offset within bounds. | 135 | 135 | 129 | 18.2 | 135 | 18.2 |
| The Float16Array() constructor creates Float16Array objects. The contents are initialized to 0 unless initialization data is explicitly provided. | 135 | 135 | 129 | 18.2 | 135 | 18.2 |
| The Math.f16round() static method returns the nearest 16-bit half precision float representation of a number. | 135 | 135 | 129 | 18.2 | 135 | 18.2 |
Syntax
const f16 = new Float16Array([1.5, 2.5, 3.5]);
console.log(f16); // Float16Array [1.5, 2.5, 3.5]
// Rounding to half precision using Math.f16round
Math.f16round(1.337); // 1.3369140625 Live demo
Check Float16Array support
Create a Float16Array when the constructor exists.
Inspect byte length
Compare the memory size of Float16Array and Float32Array.
Map over half-precision values
Transform half-precision values while keeping the typed-array structure.
Use cases
ML-style buffers
Store model-related numeric data in a more compact format when full float precision is unnecessary.
Graphics pipelines
Prepare data for rendering and GPU-adjacent workflows where compact buffers are valuable.
Cautions
- Half precision trades accuracy for size, so verify that the reduced precision is acceptable for the domain.
- Typed array code becomes harder to read quickly; keep buffer ownership and conversion steps explicit.
Accessibility
- No direct accessibility effect, but efficient numeric processing can support smoother interactive visuals.
- Do not use precision-saving optimizations if they degrade the clarity of user-facing values.
Related links
Powered by web-features