Float16Array Half Precision Storage
Float16Array stores 2-byte half-precision floats: exact for grid values, rounded to the nearest f16 step otherwise; 65520 and up overflow to Infinity.
Code
// Float16Array storage: half-precision floats use 2 bytes per value instead
// of 4, trading precision for size (10-bit mantissa keeps ~3 decimal digits)
const readings = [1.0, 1.5, 24.75, 0.3333, 65519, 100000];
const half = new Float16Array(readings);
console.log("byte length:", half.byteLength, "for", readings.length, "values");
console.log("exact roundtrip:", half[0] === 1, half[2] === 24.75);
console.log("0.3333 ->", half[3]);
console.log("100000 ->", half[5], "(f16 max is 65504)");
console.log("65519 ->", half[4]);
// 1.5 occupies bits 0x3e00 in half precision
console.log("1.5 bits are 0x3e00:", new Uint16Array(half.buffer)[1] === 0x3e00);
// NaN propagates through half precision too
half[5] = Number.NaN;
console.log("NaN stays NaN:", Number.isNaN(half[5]));
const bytes = new Uint8Array(half.buffer);
console.log("two bytes per value:", bytes.length === readings.length * 2);Output
Captured from a sandboxed run of this snippet in the build pipeline.
byte length: 12 for 6 values
exact roundtrip: true true
0.3333 -> 0.333251953125
100000 -> Infinity (f16 max is 65504)
65519 -> 65504
1.5 bits are 0x3e00: true
NaN stays NaN: true
two bytes per value: trueReading the code
Demo state sits in top-level bindings: readings, half, bytes.
What to watch for
- Zero-fetch snippet: every identifier resolves inside the language distribution.
Putting it to use
The run emits 8 labeled sections — byte length:, exact roundtrip:, 0.3333 ->, 100000 ->, 65519 ->, 1.5 bits are 0x3e00:, NaN stays NaN:, two bytes per value: — one per step.
Core lines
const readings = [1.0, 1.5, 24.75, 0.3333, 65519, 100000];
const bytes = new Uint8Array(half.buffer);
const half = new Float16Array(readings);
Interactive Tools for This Topic
FAQ
- Is a package install part of the setup?
- No. Installation is unnecessary because only built-in APIs are used. Its state lives in module-level bindings: `readings`, `half`, `bytes`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.