fn

can_cast

bool
can_cast(from_dtype: dtype, to_dtype: dtype)
source

Predicate: can from_dtype be safely cast to to_dtype?

Returns True iff every value representable in from_dtype is also representable in to_dtype without loss of range or precision. This is the "safe" casting policy of NumPy: it admits widening conversions (e.g. int8 → int32, float32 → float64) and rejects narrowing or sign-changing conversions.

Parameters

from_dtypedtype-like
Source dtype.
to_dtypedtype-like
Destination dtype.

Returns

bool

True if the cast is safe, False otherwise.

Notes

Implemented in terms of promote_types: the cast is safe when

promote_types(from,  to)  =  to,\operatorname{promote\_types}(\text{from},\;\text{to}) \;=\; \text{to},

i.e. to_dtype already dominates from_dtype in the promotion lattice. Contrast with result_type, which returns the target dtype for a mixed-type expression rather than a boolean — use can_cast for pre-flight checks, result_type for selecting an output dtype.

Examples

>>> import lucid
>>> lucid.can_cast(lucid.int8, lucid.int32)
True
>>> lucid.can_cast(lucid.float32, lucid.int32)
False
>>> lucid.can_cast(lucid.float32, lucid.float64)
True