type NumericArray = Float64Array | Float32Array | Int8Array | Uint8Array These standard TypedArrays are auto-detected by the N-API binding.
JavaScript has no built-in f16, bf16, fp8, or packed-bit numeric model.
NumKong handles that with explicit dtype tags and wrapper arrays.
The supported low-precision types and their bit layouts are:
Float16Array: 1 sign + 5 exponent + 10 mantissa bits, 2 bytes per element, range ±65504, supports Inf and NaNBFloat16Array: 1 sign + 8 exponent + 7 mantissa bits, 2 bytes per element, full f32 dynamic range, supports Inf and NaNE4M3Array: 1 sign + 4 exponent + 3 mantissa bits, 1 byte per element, range ±448, no Inf, NaN is 0x7F onlyE5M2Array: 1 sign + 5 exponent + 2 mantissa bits, 1 byte per element, range ±57344, supports Inf and NaNBinaryArray: packed bits in bytes, 8 elements per byteUse .byteLength for the exact payload size.
import { Float16Array, E4M3Array, DType, dot, angular } from "numkong";
const a16 = new Float16Array([1, 2, 3]);
const b16 = new Float16Array([4, 5, 6]);
console.log(dot(a16, b16, DType.F16));
console.log(a16.byteLength);
const a8 = new E4M3Array([1, 2, 3]);
const b8 = new E4M3Array([4, 5, 6]);
console.log(angular(a8, b8, DType.E4M3));
console.log(a8.byteLength);If the underlying storage is a raw Uint16Array or Uint8Array, JS itself cannot know whether those bytes mean integers, f16, bf16, mini-floats, or packed bits.
That is exactly when the DType tag becomes mandatory. cast converts many values between all supported types, naming both sides by their dtype string:
numkong.cast(f32Source, "f32", bf16Dest, "bf16");
numkong.cast(f32Source, "f32", e4m3Dest, "e4m3");NumericArrayNumeric arrays supported by distance metrics with auto-detected dtype.type NumericArray = Float64Array | Float32Array | Int8Array | Uint8Array These standard TypedArrays are auto-detected by the N-API binding.
DistanceArrayExtended array types supported by distance metrics with explicit dtype parameter.type DistanceArray = Float64Array | Float32Array | Int8Array | Uint8Array | Uint16Array Includes Uint16Array, the backing type for Float16Array and BFloat16Array, in addition to the auto-detected types. Pass a dtype string as the third argument to distance functions when using custom types.
NumKongArrayUnion type for all array types, including custom types for conversions.type NumKongArray = | Float64Array | Float32Array | Float16Array | BFloat16Array | E4M3Array | E5M2Array | Int8Array | Uint8Array | BinaryArray DTypeNumeric data type enum, an integer switch that compiles to a jump table.Float16ArrayIEEE 754 half-precision float, f16.5class Float16Array extends Uint16Array Float16Arraynew Float16Array(
length: number | ArrayLike<number> | ArrayBufferLike,
byteOffset?: number,
arrayLength?: number,
) toFloat32ArrayConverts the entire f16 array to f32.getFloat32Gets the f32 value at the specified index.getFloat32(index: number): number indexnumber | Array index |
|---|
setFloat32Sets the value at the specified index from an f32 value.setFloat32(index: number, value: number): void indexnumber | Array index | |
|---|---|---|
valuenumber | f32 value to encode and store |
to*× 5toString(): string BFloat16ArrayBrain float 16, bf16.3class BFloat16Array extends Uint16Array BFloat16Arraynew BFloat16Array(
length: number | ArrayLike<number> | ArrayBufferLike,
byteOffset?: number,
arrayLength?: number,
) toFloat32*× 3toFloat32Array(): Float32Array *× 6getFloat32(index: number): number | Symbol |
|---|
getFloat32 |
setFloat32 |
getFloat32 |
setFloat32 |
getFloat32 |
setFloat32 |
E4M3ArrayFP8 E4M3, 4-bit exponent and 3-bit mantissa.1class E4M3Array extends Uint8Array E4M3Arraynew E4M3Array(
length: number | ArrayLike<number> | ArrayBufferLike,
byteOffset?: number,
arrayLength?: number,
) E5M2ArrayFP8 E5M2, 5-bit exponent and 2-bit mantissa.1class E5M2Array extends Uint8Array E5M2Arraynew E5M2Array(
length: number | ArrayLike<number> | ArrayBufferLike,
byteOffset?: number,
arrayLength?: number,
) BinaryArrayBit-packed binary array, dtype u1.6class BinaryArray extends Uint8Array BinaryArraynew BinaryArray(bitLength: number) fromFloat32ArrayCreates a BinaryArray from a Float32Array (positive values = 1, else 0).static fromFloat32Array(vector: Float32Array): BinaryArray vectorFloat32Array | Source floating-point vector |
|---|
fromFloat64ArrayCreates a BinaryArray from a Float64Array (positive values = 1, else 0).static fromFloat64Array(vector: Float64Array): BinaryArray vectorFloat64Array | Source floating-point vector |
|---|
getBitGets the bit value at the specified index.getBit(index: number): number indexnumber | Bit index (0 to bitLength-1) |
|---|
setBitSets the bit value at the specified index.setBit(index: number, value: number): void indexnumber | Bit index (0 to bitLength-1) | |
|---|---|---|
valuenumber | 0 or 1 |
bitLengthReturns the logical bit length of the array.readonly bitLength: number castF16ToF32Convert a single FP16 value, as uint16 bits, to FP32.const castF16ToF32: (bits: number) => number castF32ToF16Convert a single FP32 value to FP16, returning uint16 bits.const castF32ToF16: (value: number) => number castBF16ToF32Convert a single BF16 value, as uint16 bits, to FP32.const castBF16ToF32: (bits: number) => number castF32ToBF16Convert a single FP32 value to BF16, returning uint16 bits.const castF32ToBF16: (value: number) => number castE4M3ToF32Convert a single E4M3 value, as uint8 bits, to FP32.const castE4M3ToF32: (bits: number) => number castF32ToE4M3Convert a single FP32 value to E4M3, returning uint8 bits.const castF32ToE4M3: (value: number) => number castE5M2ToF32Convert a single E5M2 value, as uint8 bits, to FP32.const castE5M2ToF32: (bits: number) => number castF32ToE5M2Convert a single FP32 value to E5M2, returning uint8 bits.const castF32ToE5M2: (value: number) => number castBulk conversion between different numeric types; modifies the destination array in place.const cast: (src: TypedArray, srcType: string, dst: TypedArray, dstType: string) => void