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 usesfloat64, matching NumPy:
Creating arrays with specific dtypes
Pass thedtype option to any creation function:
BigInt for int64 and uint64
JavaScriptnumber 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.
Complex numbers
numpy-ts provides aComplex class for complex number support, with two complex dtypes:
complex128— each element is a pair offloat64values (real, imaginary)complex64— each element is a pair offloat32values (real, imaginary)
Complex class supports arithmetic:
Float16 support
numpy-ts includes afloat16 dtype with automatic fallback for broad runtime compatibility.
- Uses native
Float16Arrayon modern runtimes (Node 23+, Chrome 127+, Firefox 129+, Safari 18.2+). - Falls back to
Float32Arrayon older runtimes —float16always works, no runtime errors. - Use the
hasFloat16export to check for native support at runtime.
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:Integer overflow and wrapping
Integer dtypes wrap on overflow, just like NumPy and C integer types. There is no error or automatic promotion:Converting dtypes with astype()
Use astype() to create a new array with a different dtype:
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: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
cvttsd2siwraps modularly and returnsINT_MINfor invalid (NaN, Inf, out-of-range). - arm64
fcvtzssaturates and returns0for NaN.
NaN/±Inf→ any integer.- Negative float → unsigned integer (e.g.
-1.7→uint32: arm64 gives0, x86 gives4294967295). - Finite float whose truncated value falls outside the target’s range (e.g.
2**31→int32: arm64 saturates to2147483647, x86 wraps to-2147483648).
In-range finite casts (
1.7 → int32, etc.) match every platform.
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 store0 (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.