fn
can_cast
→boolcan_cast(from_dtype: dtype, to_dtype: dtype)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-likeSource dtype.
to_dtypedtype-likeDestination dtype.
Returns
boolTrue if the cast is safe, False otherwise.
Notes
Implemented in terms of promote_types: the cast is safe when
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