Skip to content

Commit

Permalink
Update docs re: NumPy scalar types
Browse files Browse the repository at this point in the history
  • Loading branch information
aldanor committed Jan 8, 2020
1 parent a1bfbd1 commit eed0775
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/advanced/pycpp/numpy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,35 @@ prevent many types of unsupported structures, it is still the user's
responsibility to use only "plain" structures that can be safely manipulated as
raw memory without violating invariants.

Scalar types
============

In some cases we may want to accept or return NumPy scalar values such as
``np.float32`` or ``np.uint16``. It is especially important in case of
C-side single-precision floats which by default will be bound to Python's
double-precision builtin floats, causing mismatch in float precision.

Luckily, there's a helper type for this occasion - ``py::numpy_scalar``:

.. code-block:: cpp
m.def("square_float32", [](py::numpy_scalar<float> value) {
float v = value;
return py::make_scalar(v * v);
});
This type is trivially convertible to and from the type it wraps; currently
supported scalar types are NumPy arithmetic types: ``bool_``, ``int8``,
``int8``, ``int16``, ``int32``, ``int64``, ``uint8``, ``uint16``, ``uint32``,
``uint64``, ``float32``, ``float64``, all of them mapping to respective C++
equivalents.

.. note::

This is a strict type, it will only allow input arguments of the specified
NumPY type and nothing else (e.g., ``py::numpy_scalar<int64_t>`` will not
accept built-in ``int`` or any other type for that matter).

Vectorizing functions
=====================

Expand Down

0 comments on commit eed0775

Please sign in to comment.