[Numpy-discussion] Mapping of dtype to C types

Keith Goodman kwgoodman at gmail.com
Mon May 9 09:58:55 EDT 2011


On Mon, May 9, 2011 at 1:46 AM, Pauli Virtanen <pav at iki.fi> wrote:
> Sun, 08 May 2011 14:45:45 -0700, Keith Goodman wrote:
>> I'm writing a function that accepts four possible dtypes: int32, int64,
>> float32, float64. The function will call a C extension (wrapped in
>> Cython). What are the equivalent C types? int, long, float, double,
>> respectively? Will that work on all systems?
>
> Long can be 32-bit or 64-bit, depending on the platform.
>
> The types available in Numpy are listed here, including
> the information which of them are compatible with which C types:
>
> http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html
>
> The C long seems not to be listed -- but it's the same as
> "Python int", i.e., np.int_ will work.
>
> IIRC, the Numpy type codes, dtype.kind, map directly to C types.

Does this mapping look right?

        np.float32 --> float
        np.float64 --> double
        if np.int_ == np.int32:
            # np.int32 --> long
            # np.int64 --> long long
        elif np.int_ == np.int64:
            # np.int32 --> int
            # np.int64 --> long



More information about the NumPy-Discussion mailing list