JavaScript · Scalar Types

Scalar Types

import * as numkong from 'numkong';import { Float16Array } from 'numkong';import { BFloat16Array } from 'numkong';import { E4M3Array } from 'numkong';import { E5M2Array } from 'numkong';import { BinaryArray } from 'numkong'; 8.0.0-rc.1 · b1f866ab

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 NaN
  • BFloat16Array: 1 sign + 8 exponent + 7 mantissa bits, 2 bytes per element, full f32 dynamic range, supports Inf and NaN
  • E4M3Array: 1 sign + 4 exponent + 3 mantissa bits, 1 byte per element, range ±448, no Inf, NaN is 0x7F only
  • E5M2Array: 1 sign + 5 exponent + 2 mantissa bits, 1 byte per element, range ±57344, supports Inf and NaN
  • BinaryArray: packed bits in bytes, 8 elements per byte

Use .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");

API

Types

NumericArrayNumeric arrays supported by distance metrics with auto-detected dtype.
numkong.ts:114 ↗
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.
numkong.ts:121 ↗
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.
numkong.ts:124 ↗
type NumKongArray = | Float64Array | Float32Array | Float16Array | BFloat16Array | E4M3Array | E5M2Array | Int8Array | Uint8Array | BinaryArray
DTypeNumeric data type enum, an integer switch that compiles to a jump table.
numkong.ts:33 ↗
enum DType

Values

F640
F321
F162
BF163
E4M34
E5M25
E2M36
E3M27
I88
U89
U110
I3211
U3212
ClassFloat16ArrayIEEE 754 half-precision float, f16.5
types.ts:430 ↗
class Float16Array extends Uint16Array
  • 16-bit floating point: 1 sign bit, 5 exponent bits, 10 mantissa bits.
  • Range: ~±65504, precision: ~3-4 decimal digits.
  • Common in GPU inference, model compression, and mixed-precision training.
  • Supported natively on Apple Silicon, NVIDIA GPUs as fp16, and AMD GPUs.
Float16Array
types.ts:431 ↗
new Float16Array(
    length: number | ArrayLike<number> | ArrayBufferLike,
    byteOffset?: number,
    arrayLength?: number,
)
toFloat32ArrayConverts the entire f16 array to f32.
types.ts:453 ↗
toFloat32Array(): Float32Array

Returns

Float32Array with decoded values
getFloat32Gets the f32 value at the specified index.
types.ts:469 ↗
getFloat32(index: number): number

Parameters

indexnumberArray index

Returns

Decoded f32 value
setFloat32Sets the value at the specified index from an f32 value.
types.ts:481 ↗
setFloat32(index: number, value: number): void

Parameters

indexnumberArray index
valuenumberf32 value to encode and store
to*× 5
types.ts:488 ↗
toString(): string
ClassBFloat16ArrayBrain float 16, bf16.3
types.ts:512 ↗
class BFloat16Array extends Uint16Array
  • 16-bit floating point: 1 sign bit, 8 exponent bits, 7 mantissa bits.
  • Range: same as f32, ~±3.4e38, precision: ~2-3 decimal digits.
  • Designed by Google for TPUs, optimized for ML training with a wider range than f16.
  • Supported on Google TPUs, Intel Sapphire Rapids, AMD Genoa, ARM Neoverse V2.
  • Truncated f32, top 16 bits, making conversion very cheap.
BFloat16Array
types.ts:513 ↗
new BFloat16Array(
    length: number | ArrayLike<number> | ArrayBufferLike,
    byteOffset?: number,
    arrayLength?: number,
)
toFloat32*× 3
types.ts:530 ↗
toFloat32Array(): Float32Array
*× 6
types.ts:541 ↗
getFloat32(index: number): number
Symbol
getFloat32
setFloat32
getFloat32
setFloat32
getFloat32
setFloat32
ClassE4M3ArrayFP8 E4M3, 4-bit exponent and 3-bit mantissa.1
types.ts:578 ↗
class E4M3Array extends Uint8Array
  • 8-bit floating point: 1 sign bit, 4 exponent bits, 3 mantissa bits.
  • Range: ~±448, precision: ~1 decimal digit.
  • Optimized for forward pass inference with higher precision than E5M2.
  • Supported on NVIDIA Hopper H100 GPUs, AMD Instinct MI300.
E4M3Array
types.ts:579 ↗
new E4M3Array(
    length: number | ArrayLike<number> | ArrayBufferLike,
    byteOffset?: number,
    arrayLength?: number,
)
ClassE5M2ArrayFP8 E5M2, 5-bit exponent and 2-bit mantissa.1
types.ts:644 ↗
class E5M2Array extends Uint8Array
  • 8-bit floating point: 1 sign bit, 5 exponent bits, 2 mantissa bits.
  • Range: ~±57344, precision: <1 decimal digit.
  • Optimized for backward pass training with wider range than E4M3.
  • Supported on NVIDIA Hopper H100 GPUs, AMD Instinct MI300.
E5M2Array
types.ts:645 ↗
new E5M2Array(
    length: number | ArrayLike<number> | ArrayBufferLike,
    byteOffset?: number,
    arrayLength?: number,
)
ClassBinaryArrayBit-packed binary array, dtype u1.6
types.ts:710 ↗
class BinaryArray extends Uint8Array
  • 1-bit per element, packed into bytes: 8 bits per byte, most significant bit first.
  • Dimension count must be a multiple of 8, the values per byte.
  • Used for binary embeddings, hashing, and Hamming/Jaccard distances.
  • Common in semantic search with binary quantization, as in Cohere and Voyage embeddings.
BinaryArray
types.ts:713 ↗
new BinaryArray(bitLength: number)
fromFloat32ArrayCreates a BinaryArray from a Float32Array (positive values = 1, else 0).
types.ts:760 ↗
static fromFloat32Array(vector: Float32Array): BinaryArray

Parameters

vectorFloat32ArraySource floating-point vector

Returns

Binary array with quantized values
fromFloat64ArrayCreates a BinaryArray from a Float64Array (positive values = 1, else 0).
types.ts:775 ↗
static fromFloat64Array(vector: Float64Array): BinaryArray

Parameters

vectorFloat64ArraySource floating-point vector

Returns

Binary array with quantized values
getBitGets the bit value at the specified index.
types.ts:723 ↗
getBit(index: number): number

Parameters

indexnumberBit index (0 to bitLength-1)

Returns

0 or 1
setBitSets the bit value at the specified index.
types.ts:737 ↗
setBit(index: number, value: number): void

Parameters

indexnumberBit index (0 to bitLength-1)
valuenumber0 or 1
bitLengthReturns the logical bit length of the array.
types.ts:751 ↗
readonly bitLength: number

Functions

castF16ToF32Convert a single FP16 value, as uint16 bits, to FP32.
numkong.ts:91 ↗
const castF16ToF32: (bits: number) => number
castF32ToF16Convert a single FP32 value to FP16, returning uint16 bits.
numkong.ts:93 ↗
const castF32ToF16: (value: number) => number
castBF16ToF32Convert a single BF16 value, as uint16 bits, to FP32.
numkong.ts:95 ↗
const castBF16ToF32: (bits: number) => number
castF32ToBF16Convert a single FP32 value to BF16, returning uint16 bits.
numkong.ts:97 ↗
const castF32ToBF16: (value: number) => number
castE4M3ToF32Convert a single E4M3 value, as uint8 bits, to FP32.
numkong.ts:99 ↗
const castE4M3ToF32: (bits: number) => number
castF32ToE4M3Convert a single FP32 value to E4M3, returning uint8 bits.
numkong.ts:101 ↗
const castF32ToE4M3: (value: number) => number
castE5M2ToF32Convert a single E5M2 value, as uint8 bits, to FP32.
numkong.ts:103 ↗
const castE5M2ToF32: (bits: number) => number
castF32ToE5M2Convert a single FP32 value to E5M2, returning uint8 bits.
numkong.ts:105 ↗
const castF32ToE5M2: (value: number) => number
castBulk conversion between different numeric types; modifies the destination array in place.
numkong.ts:107 ↗
const cast: (src: TypedArray, srcType: string, dst: TypedArray, dstType: string) => void