Gathering detailed insights and metrics for @petamoriken/float16
Gathering detailed insights and metrics for @petamoriken/float16
Gathering detailed insights and metrics for @petamoriken/float16
Gathering detailed insights and metrics for @petamoriken/float16
Stage 3 IEEE 754 half-precision floating-point ponyfill
npm install @petamoriken/float16
Typescript
Module System
Node Version
NPM Version
99.5
Supply Chain
100
Quality
78.7
Maintenance
100
Vulnerability
100
License
JavaScript (97.95%)
HTML (1.59%)
TypeScript (0.46%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
102 Stars
1,994 Commits
9 Forks
6 Watchers
6 Branches
6 Contributors
Updated on Jul 10, 2025
Latest Version
3.9.2
Package Id
@petamoriken/float16@3.9.2
Unpacked Size
273.79 kB
Size
39.71 kB
File Count
34
NPM Version
10.9.2
Node Version
22.14.0
Published on
Mar 09, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
24
IEEE 754 half-precision floating-point ponyfill for JavaScript
See TC39 proposal or the archive of the ES Discuss Float16Array topic for details
1npm install @petamoriken/float16
[!NOTE] Native float16 features are supported since Deno v1.43.
1deno add jsr:@petamoriken/float16
[!NOTE] Native float16 features are supported since Bun v1.1.23.
1bun add @petamoriken/float16
1import { 2 Float16Array, isFloat16Array, isTypedArray, 3 getFloat16, setFloat16, 4 f16round, 5} from "@petamoriken/float16";
Deliver a browser/float16.mjs
or browser/float16.js
file in the npm package
from your Web server with the JavaScript Content-Type
HTTP header.
1<!-- Module Scripts --> 2<script type="module"> 3 import { 4 Float16Array, isFloat16Array, isTypedArray, 5 getFloat16, setFloat16, 6 f16round, 7 } from "DEST/TO/float16.mjs"; 8</script>
1<!-- Classic Scripts --> 2<script src="DEST/TO/float16.js"></script> 3<script> 4 const { 5 Float16Array, isFloat16Array, isTypedArray, 6 getFloat16, setFloat16, 7 f16round, 8 } = float16; 9</script>
1<!-- Module Scripts --> 2<script type="module"> 3 import { 4 Float16Array, isFloat16Array, isTypedArray, 5 getFloat16, setFloat16, 6 f16round, 7 } from "https://cdn.jsdelivr.net/npm/@petamoriken/float16/+esm"; 8</script>
1<!-- Classic Scripts --> 2<script src="https://cdn.jsdelivr.net/npm/@petamoriken/float16/browser/float16.min.js"></script> 3<script> 4 const { 5 Float16Array, isFloat16Array, isTypedArray, 6 getFloat16, setFloat16, 7 f16round, 8 } = float16; 9</script>
This package only requires ES2015 features and does not use
environment-dependent features (except for inspect/
), so you can use it
without any problems. It works fine with
the current officially supported versions of Node.js.
Float16Array
implemented by Proxy
and Reflect
, so IE11 is never supported
even if you use polyfills.
lib/
and browser/
directories in the npm package have JavaScript files
already transpiled, and they have been tested automatically in the following
environments:
Float16Array
Float16Array
is similar to TypedArray
such as Float32Array
(MDN).
1const array = new Float16Array([1.0, 1.1, 1.2, 1.3]); 2for (const value of array) { 3 // 1, 1.099609375, 1.2001953125, 1.2998046875 4 console.log(value); 5} 6 7// Float16Array(4) [ 2, 2.19921875, 2.3984375, 2.599609375 ] 8array.map((value) => value * 2);
isFloat16Array
[!WARNING] This API returns
false
for ECMAScript's nativeFloat16Array
isFloat16Array
is a utility function to check whether the value given as an
argument is an instance of Float16Array
or not.
1const buffer = new ArrayBuffer(256);
2
3// true
4isFloat16Array(new Float16Array(buffer));
5
6// false
7isFloat16Array(new Float32Array(buffer));
8isFloat16Array(new Uint16Array(buffer));
9isFloat16Array(new DataView(buffer));
isTypedArray
isTypedArray
is a utility function to check whether the value given as an
argument is an instance of a type of TypedArray
or not. Unlike
util.types.isTypedArray
in Node.js, this returns true
for Float16Array
.
1const buffer = new ArrayBuffer(256);
2
3// true
4isTypedArray(new Float16Array(buffer));
5isTypedArray(new Float32Array(buffer));
6isTypedArray(new Uint16Array(buffer));
7
8// false
9isTypedArray(new DataView(buffer));
getFloat16
, setFloat16
getFloat16
and setFloat16
are similar to DataView
methods such as
DataView#getFloat32
(MDN)
and DataView#setFloat32
(MDN).
1declare function getFloat16(view: DataView, byteOffset: number, littleEndian?: boolean): number;
2declare function setFloat16(view: DataView, byteOffset: number, value: number, littleEndian?: boolean): void;
1const buffer = new ArrayBuffer(256);
2const view = new DataView(buffer);
3
4view.setUint16(0, 0x1234);
5getFloat16(view, 0); // 0.0007572174072265625
6
7// You can append methods to DataView instance
8view.getFloat16 = (...args) => getFloat16(view, ...args);
9view.setFloat16 = (...args) => setFloat16(view, ...args);
10
11view.getFloat16(0); // 0.0007572174072265625
12
13view.setFloat16(0, Math.PI, true);
14view.getFloat16(0, true); // 3.140625
f16round
(alias: hfround
)f16round
is similar to Math.fround
(MDN).
This function returns nearest half-precision float representation of a number.
1declare function f16round(x: number): number;
1Math.fround(1.337); // 1.3370000123977661 2f16round(1.337); // 1.3369140625
Float16Array
limitations (edge cases)Float16Array
has some limitations, because it is impossible to completely reproduce the behavior of TypedArray
. Be careful when checking if it is a TypedArray
or not by using ArrayBuffer.isView
, and when using Web standards such as structuredClone
and WebGL.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
instance.
1ArrayBuffer.isView(new Float32Array(10)); // true
2ArrayBuffer.isView(new Float16Array(10)); // false
The structured clone algorithm copies complex JavaScript objects. It is used
internally when invoking structuredClone()
, to transfer data between Web
Workers via postMessage()
, storing objects with IndexedDB, or copying objects
for other APIs
(MDN).
It can't clone Proxy
object such as Float16Array
instance, you need to
convert it to Uint16Array
or deal with ArrayBuffer
directly.
1const array = new Float16Array([1.0, 1.1, 1.2]); 2const cloned = structuredClone({ buffer: array.buffer });
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.
1// WebGL 2 example
2const vertices = new Float16Array([
3 -0.5, -0.5, 0,
4 0.5, -0.5, 0,
5 0.5, 0.5, 0,
6]);
7
8const buffer = gl.createBuffer();
9gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
10
11// wrap in Uint16Array
12gl.bufferData(gl.ARRAY_BUFFER, new Uint16Array(vertices.buffer), gl.STATIC_DRAW);
13gl.vertexAttribPointer(location, 3, gl.HALF_FLOAT, false, 0, 0);
14
15gl.bindBuffer(gl.ARRAY_BUFFER, null);
16gl.enableVertexAttribArray(location);
See JSDoc comments in src/Float16Array.mjs
for details. If you don't write
hacky code, you shouldn't have any problems.
Float16Array
custom inspectionconsole.log
more readable.
1import { Float16Array } from "@petamoriken/float16"; 2import { customInspect } from "@petamoriken/float16/inspect"; 3 4Float16Array.prototype[Symbol.for("nodejs.util.inspect.custom")] = customInspect;
1import { Float16Array } from "@petamoriken/float16"; 2import { customInspect } from "@petamoriken/float16/inspect"; 3 4// deno-lint-ignore no-explicit-any 5(Float16Array.prototype as any)[Symbol.for("nodejs.util.inspect.custom")] = customInspect;
This repository uses corepack for package manager manager. You may have to activate yarn in corepack.
1corepack enable yarn
Download devDependencies.
1yarn
Build lib/
, browser/
files.
1yarn run build
Build docs/
files (for browser test).
1yarn run docs
This repository uses corepack for package manager manager. You may have to activate yarn in corepack.
1corepack enable yarn
Download devDependencies.
1yarn
1NODE_ENV=test yarn build:lib 2yarn test
1NODE_ENV=test yarn build:browser 2yarn docs
Access docs/test/index.html
with browsers.
You can access current test page
(power-assert version) in
master
branch.
MIT License
This software contains productions that are distributed under
the Apache 2.0 License.
Specifically, index.d.ts
is modified from the original
TypeScript lib files.
No vulnerabilities found.
Reason
30 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
SAST tool is run on all commits
Details
Reason
3 existing vulnerabilities detected
Details
Reason
dangerous workflow patterns detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More