Skip to main content

Overview

numpy-ts supports 14 data types that mirror NumPy’s numeric type system. Every NDArray has a single dtype that determines how its elements are stored in memory and how arithmetic behaves.

Supported dtypes

Default dtype

When you create an array without specifying a dtype, numpy-ts uses float64, matching NumPy:

Creating arrays with specific dtypes

Pass the dtype option to any creation function:

BigInt for int64 and uint64

JavaScript number only has 53 bits of integer precision. For full 64-bit integer support, numpy-ts uses BigInt64Array and BigUint64Array under the hood. This means int64 and uint64 arrays exchange values as bigint, not number.
When working with int64 or uint64 arrays, get() returns bigint and set() expects bigint. Mixing number and bigint in JavaScript throws a TypeError, so be explicit about conversions.

Complex numbers

numpy-ts provides a Complex class for complex number support, with two complex dtypes:
  • complex128 — each element is a pair of float64 values (real, imaginary)
  • complex64 — each element is a pair of float32 values (real, imaginary)
The Complex class supports arithmetic:

Float16 support

numpy-ts includes a float16 dtype with automatic fallback for broad runtime compatibility.
  • Uses native Float16Array on modern runtimes (Node 23+, Chrome 127+, Firefox 129+, Safari 18.2+).
  • Falls back to Float32Array on older runtimes — float16 always works, no runtime errors.
  • Use the hasFloat16 export to check for native support at runtime.
If you need exact NumPy float16 parity, target Node 23+ or use hasFloat16 to guard precision-sensitive code paths.

Type promotion hierarchy

When you combine arrays with different dtypes in an operation, numpy-ts promotes to a common type that can represent both without data loss. The promotion follows NumPy’s rules:
Some key promotion rules:

Integer overflow and wrapping

Integer dtypes wrap on overflow, just like NumPy and C integer types. There is no error or automatic promotion:
If you need overflow protection, use a wider dtype (int16 instead of int8) or promote to float64 first.

Converting dtypes with astype()

Use astype() to create a new array with a different dtype:
By default, astype() always returns a copy. Pass copy: false to return the same array when the dtype already matches:

Cast semantics: float → integer

For in-range finite floats, astype() truncates toward zero (matching NumPy). For out-of-range floats, the behavior depends on the target width: The two-step rule for narrow targets (8/16-bit) matches NumPy 2.x: saturate to int32 range, then bit-truncate via the target’s ToIntN. Earlier numpy-ts versions saturated against the final target width directly and could return 0 where NumPy returns -1 / 255 / 65535.

Cast semantics: int64 / uint64 → narrower integer

Casting a 64-bit integer to a narrower width preserves the low N bits of the source, matching NumPy:
This requires care internally — naive Number(bigint) rounds 2^63-class values to a multiple of 2^11 before any truncation can happen. numpy-ts masks in BigInt-space (v & 0xFFFFFFFFn) before converting, so the low bits survive into the TypedArray store.

Caveat: out-of-range float → integer is platform-dependent in NumPy

NumPy 2.x routes float-to-integer conversion through the CPU’s native fp-to-int intrinsic, and the two major intrinsics disagree:
  • x86 cvttsd2si wraps modularly and returns INT_MIN for invalid (NaN, Inf, out-of-range).
  • arm64 fcvtzs saturates and returns 0 for NaN.
The same NumPy version produces different results on different CPUs for the same input. This covers three pattern classes:
  1. NaN / ±Inf → any integer.
  2. Negative float → unsigned integer (e.g. -1.7uint32: arm64 gives 0, x86 gives 4294967295).
  3. Finite float whose truncated value falls outside the target’s range (e.g. 2**31int32: arm64 saturates to 2147483647, x86 wraps to -2147483648).
numpy-ts picks the arm64 convention as a stable, predictable rule across all runtimes: In-range finite casts (1.7 → int32, etc.) match every platform.
If you cast pathological float values to an integer dtype in code compared against NumPy, expect divergence on x86 NumPy. Filter, clamp, or replace these values explicitly before astype() if cross-platform parity matters.

Special cases

Comparisons always return bool

Regardless of input dtypes, comparison operations always produce bool arrays:

mean() promotes integers to float64

Just like NumPy, mean() converts integer arrays to float64 to avoid truncation:

Boolean arrays

Boolean arrays store 0 (false) and 1 (true) as uint8 values. They participate in arithmetic as integers:

Next steps

Array Basics

NDArray properties, creation, and conversion.

Broadcasting

How arrays with different shapes combine in operations.