@petamoriken/float16
half precision floating point for JavaScript
See ES Discuss Float16Array topic
Install
yarn add @petamoriken/float16npm install @petamoriken/float16 --saveImport
Node.js or Bundler (webpack, rollup.js, esbuild, etc)
// ES Modules
import { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } from "@petamoriken/float16";// CommonJS
const { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } = require("@petamoriken/float16");Browser
Serve browser/float16.mjs / browser/float16.js files from your Web server as the JavaScript Content-Type.
<!-- Module Scripts -->
<script type="module">
import { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } from "DEST/TO/float16.mjs";
</script><!-- Classic Scripts -->
<script src="DEST/TO/float16.js"></script>
<script>
const { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } = float16;
</script>Or use jsDelivr CDN.
<!-- Module Scripts -->
<script type="module">
import { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } from "https://cdn.jsdelivr.net/npm/@petamoriken/float16/+esm";
</script><!-- Classic Scripts -->
<script src="https://cdn.jsdelivr.net/npm/@petamoriken/float16/browser/float16.min.js"></script>
<script>
const { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } = float16;
</script>ES modules are also available on the Skypack CDN.
<!-- Module Scripts -->
<script type="module">
import { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } from "https://cdn.skypack.dev/@petamoriken/float16?min";
</script>Deno
import { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } from "https://deno.land/x/float16/index.mjs";Support
This packageās Float16Array uses Proxy object, so IE11 is never supported.
lib/ and browser/ directories in the npm package have JavaScript files already built, whose target are
- Firefox: last 2 versions and ESR
- Chrome: last 2 versions
- Edge: last 2 versions
- Safari: last 2 versions
However, this package only uses up to the ES2015 features, so you should be able to use it without any problems.
If you build it yourself using bundler to support older browsers, transpile the JavaScript files in the src/ directory.
API
Float16Array
This API is similar to TypedArray such as Float32Array (MDN).
const array = new Float16Array([1.0, 1.1, 1.2]);
for (const val of array) {
console.log(val); // => 1, 1.099609375, 1.19921875
}
array.reduce((prev, current) => prev + current); // 3.298828125isFloat16Array
declare function isFloat16Array(value: unknown): value is Float16Array;isFloat16Array(new Float16Array()); // true
isFloat16Array(new Float32Array()); // false
isFloat16Array(new Uint16Array()); // falseDataView
declare function getFloat16(view: DataView, byteOffset: number, littleEndian?: boolean): number;
declare function setFloat16(view: DataView, byteOffset: number, value: number, littleEndian?: boolean): void;These APIs are similar to DataView methods such as DataView#getFloat32 (MDN) and DataView#setFloat32 (MDN).
const buffer = new ArrayBuffer(10);
const view = new DataView(buffer);
view.setUint16(0, 0x1234);
getFloat16(view, 0); // 0.0007572174072265625
// You can append methods to DataView instance
view.getFloat16 = (...args) => getFloat16(view, ...args);
view.setFloat16 = (...args) => setFloat16(view, ...args);
view.getFloat16(0); // 0.0007572174072265625
view.setFloat16(0, Math.PI, true);
view.getFloat16(0, true); // 3.140625hfround
declare function hfround(x: number): number;This API is similar to Math.fround (MDN).
This function returns nearest half precision float representation of a number.
Math.fround(1.337); // 1.3370000123977661
hfround(1.337); // 1.3369140625Float16Array limitations (edge cases)
The instanceof Operator
Since Float16Array is made by inheriting from Uint16Array, so you canāt use the instanceof operator to check if it is a Uint16Array or not.
new Uint16Array(10) instanceof Uint16Array; // true
new Float16Array(10) instanceof Uint16Array; // trueActually, I could use Proxyās getPrototypeOf handler to trap it, but that would be too complex and have some limitations.
In addition, it is a bad idea to use instanceof to detect the type of TypedArray, because it canāt be used to detect the type of objects from other Realms, such as iframe and vm. It is recommended to use Object#toString or @@toStringTag for this purpose.
function isUint16Array(target) {
if (target === null || typeof target !== "object") {
return false;
}
return Object.prototype.toString.call(target) === "[object Uint16Array]";
}For Node.js, you can use util.types (document) instead. Want to do a more solid TypedArray check for other environments? Then you can use this code š
Built-in Functions
Built-in TypedArray objects use āinternal slotsā for built-in methods. Some limitations exist because the Proxy object canāt trap internal slots (explanation).
This package isnāt polyfill, in other words, it doesnāt change native global functions and static/prototype methods.
E.g. ArrayBuffer.isView is the butlt-in method that checks if it has the [[ViewedArrayBuffer]] internal slot. It returns false for Proxy object such as Float16Array.
ArrayBuffer.isView(new Float32Array(10)); // true
ArrayBuffer.isView(new Float16Array(10)); // falseWebGL
WebGL requires Uint16Array for buffer or texture data whose types are gl.HALF_FLOAT (WebGL 2) or ext.HALF_FLOAT_OES (WebGL 1 extension). Do not apply the Float16Array object directly to gl.bufferData or gl.texImage2D etc.
// WebGL 2 example
const vertices = new Float16Array([
-0.5, -0.5, 0,
0.5, -0.5, 0,
0.5, 0.5, 0,
]);
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
// wrap in Uint16Array
gl.bufferData(gl.ARRAY_BUFFER, new Uint16Array(vertices.buffer), gl.STATIC_DRAW);
gl.vertexAttribPointer(location, 3, gl.HALF_FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.enableVertexAttribArray(location);Others
See JSDoc comments in src/Float16Array.mjs for details. If you donāt write hacky code, you shouldnāt have any problems.
Build
First, download devDependencies.
yarnBuild lib/, browser/ files.
yarn run buildBuild docs/ files (for browser test).
yarn run docsTest
First, download devDependencies.
yarnNode.js Test
yarn build:lib
yarn testBrowser Test
yarn build:browser
yarn docsAccess docs/test/index.html with browsers.
You can access current test page (power-assert version) in master branch.