qunits logo qunits logo

A performant, verbose and generic physical unit system.

Static Badge Static Badge

© 2026 Patrick Müller. All rights reserved.

qunits. Quantity  (  value ,  unit=None  )[source]

A value with a Unit. The value is a float or a numpy array of floats. Quantities are usually created by multiplying a number or an array with a unit, e.g. 3 * u.m, and support:

  • + and -, also in place, with quantities of the same dimension and context. The result is in the unit of the left operand. Numbers and arrays can only be added to and subtracted from dimensionless quantities without context, e.g. 0.5 * u.m / u.km + 1.
  • * and /, also in place, with numbers, arrays, units and quantities.
  • np.add, np.subtract, np.multiply and np.divide, and np.sin, np.cos and np.tan of angles, i.e. dimensionless quantities with the context Context.ANGLE such as u.rad.

Other numpy functions and the methods reduce, accumulate, outer and at of the numpy functions, e.g. np.sum, raise a NotImplementedError.

Printed quantities show scalar values rounded to 15 significant digits, e.g. 5.225 km instead of 5.2250000000000005 km, which hides the rounding errors of the binary floats. The values themselves are not rounded. Arrays are printed with the precision of numpy.

Parameters:
valuearray_like | Quantity

The value, a number, an array-like or a quantity. Numbers are converted to float and array-likes to numpy arrays of float64.

unitUnit | None

The unit. A quantity value is converted to it. Defaults to the unit of a quantity value, and to the dimensionless unit u.I otherwise.

Attributes:
TQuantity

The quantity with the transposed array value. A quantity with a scalar value is returned unchanged.

ctypes_ctypes[int]

The ctypes interface of the array value. Raises a NotImplementedError for a scalar value.

datamemoryview

A memoryview of the array value. Raises a NotImplementedError for a scalar value.

dtypenp.dtype[np.float64]

The data type of the value, float64.

flagsflagsobj

The memory layout flags of the array value. Raises a NotImplementedError for a scalar value.

itemsizeint

The size of one element of the value in bytes.

mfloat | NDArray[np.float64]

The value of the quantity in its unit, same as value.

magnitudefloat | NDArray[np.float64]

The value of the quantity in its unit, same as value.

nbytesint

The size of all elements of the value in bytes.

ndimint

The number of array dimensions of the value, 0 for a scalar value.

shapetuple[int, ...]

The shape of the value, () for a scalar value.

sizeint

The number of elements of the value, 1 for a scalar value.

stridestuple[int, ...]

The strides of the array value, () for a scalar value.

unitUnit

The Unit of the quantity.

valuefloat | NDArray[np.float64]

The value of the quantity in its unit, a float or a numpy array of floats.

Raises:
ValueError

If value is a quantity with another dimension or context than unit.