dpnp.can_cast

dpnp.can_cast(from_, to, casting='safe')[source]

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

For full documentation refer to numpy.can_cast.

Parameters:
  • from ({dpnp.ndarray, usm_ndarray, dtype, dtype specifier}) -- Source data type.

  • to (dtype) -- Target data type.

  • casting ({'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional) -- Controls what kind of data casting may occur.

Returns:

out -- True if cast can occur according to the casting rule, False otherwise.

Return type:

bool

See also

dpnp.result_type

Returns the type that results from applying the NumPy type promotion rules to the arguments.

Examples

Basic examples

>>> import dpnp as np
>>> np.can_cast(np.int32, np.int64)
True
>>> np.can_cast(np.float64, complex)
True
>>> np.can_cast(complex, float)
False
>>> np.can_cast('i8', 'f8')
True
>>> np.can_cast('i8', 'f4')
False

Array scalar checks the value, array does not

>>> np.can_cast(np.array(1000.0), np.float32)
True
>>> np.can_cast(np.array([1000.0]), np.float32)
False

Using the casting rules

>>> np.can_cast('i8', 'i8', 'no')
True
>>> np.can_cast('<i8', '>i8', 'no')
False
>>> np.can_cast('<i8', '>i8', 'equiv')
True
>>> np.can_cast('<i4', '>i8', 'equiv')
False
>>> np.can_cast('<i4', '>i8', 'safe')
True
>>> np.can_cast('<i8', '>i4', 'safe')
False
>>> np.can_cast('<i8', '>i4', 'same_kind')
True
>>> np.can_cast('<i8', '>u4', 'same_kind')
False
>>> np.can_cast('<i8', '>u4', 'unsafe')
True