Skip to main content

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

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:

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:

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.