Overview
numpy-ts supports 13 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
| dtype | TypedArray backing | Byte size | Range |
|---|---|---|---|
float64 | Float64Array | 8 | ~-1.8e308 to ~1.8e308 (64-bit IEEE 754) |
float32 | Float32Array | 4 | ~-3.4e38 to ~3.4e38 (32-bit IEEE 754) |
int64 | BigInt64Array | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
int32 | Int32Array | 4 | -2,147,483,648 to 2,147,483,647 |
int16 | Int16Array | 2 | -32,768 to 32,767 |
int8 | Int8Array | 1 | -128 to 127 |
uint64 | BigUint64Array | 8 | 0 to 18,446,744,073,709,551,615 |
uint32 | Uint32Array | 4 | 0 to 4,294,967,295 |
uint16 | Uint16Array | 2 | 0 to 65,535 |
uint8 | Uint8Array | 1 | 0 to 255 |
bool | Uint8Array | 1 | 0 or 1 |
complex128 | Float64Array (interleaved) | 16 | Two float64 values (real + imaginary) |
complex64 | Float32Array (interleaved) | 8 | Two float32 values (real + imaginary) |
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:
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:| Operation | Result dtype | Reason |
|---|---|---|
float64 + int32 | float64 | float always wins |
float32 + int8 | float32 | float32 can hold int8 values |
float32 + int32 | float64 | float32 lacks precision for all int32 values |
int32 + uint32 | int64 | need signed type larger than both |
int64 + uint64 | float64 | no integer type can hold both ranges |
bool + int32 | int32 | bool promotes to the other type |
complex128 + float64 | complex128 | complex always wins |
complex64 + float64 | complex128 | float64 requires complex128 precision |
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:
astype() always returns a copy. Pass copy: false to return the same array when the dtype already matches:
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.