flopscope.

flopscope.numpy.can_cast

fnp.can_cast(*args, **kwargs)[flopscope source]

Returns True if cast between data types can occur according to the casting rule.

Adapted from NumPy docs np.can_cast

Areacore
Typefree
NumPy Refnp.can_cast
Cost
0
Flopscope Context

Returns True if cast is safe.

Parameters

from_:dtype, dtype specifier, NumPy scalar, or array

Data type, NumPy scalar, or array to cast from.

to:dtype or dtype specifier

Data type to cast to.

casting:{'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional

Controls what kind of data casting may occur.

  • 'no' means the data types should not be cast at all.

  • 'equiv' means only byte-order changes are allowed.

  • 'safe' means only casts which can preserve values are allowed.

  • 'same_kind' means only safe casts or casts within a kind, like float64 to float32, are allowed.

  • 'unsafe' means any data conversions may be done.

Returns

out:bool

True if cast can occur according to the casting rule.

See also

Notes

Changed in version 2.0.

Examples

Basic examples

>>> import flopscope.numpy as fnp
>>> flops.can_cast(flops.int32, flops.int64)
True
>>> flops.can_cast(flops.float64, complex)
True
>>> flops.can_cast(complex, float)
False
>>> flops.can_cast('i8', 'f8')
True
>>> flops.can_cast('i8', 'f4')
False
>>> flops.can_cast('i4', 'S4')
False