copapy.cast#
- copapy.cast(x, dtype)#
Convert a number, a copapy value or the elements of a vector, tensor or array to another type.
- Overloads:
x (
float|int), dtype (Literal['bool'] |type[bool]) →boolx (
float|int), dtype (Literal['int'] |type[int]) →intx (
float|int), dtype (Literal['float'] |type[float]) →floatx (
value[Any]), dtype (Literal['bool'] |type[bool]) →value[int]x (
value[Any]), dtype (Literal['int'] |type[int]) →value[int]x (
value[Any]), dtype (Literal['float'] |type[float]) →value[float]x (
vector[Any]), dtype (Literal['bool'] |type[bool]) →vector[int]x (
vector[Any]), dtype (Literal['int'] |type[int]) →vector[int]x (
vector[Any]), dtype (Literal['float'] |type[float]) →vector[float]x (
tensor[Any]), dtype (Literal['bool'] |type[bool]) →tensor[int]x (
tensor[Any]), dtype (Literal['int'] |type[int]) →tensor[int]x (
tensor[Any]), dtype (Literal['float'] |type[float]) →tensor[float]x (
array[Any]), dtype (Literal['bool'] |type[bool]) →array[int]x (
array[Any]), dtype (Literal['int'] |type[int]) →array[int]x (
array[Any]), dtype (Literal['float'] |type[float]) →array[float]x (
ArrayType[Any]), dtype (Literal['bool'] |type[bool]) →ArrayType[int]x (
ArrayType[Any]), dtype (Literal['int'] |type[int]) →ArrayType[int]x (
ArrayType[Any]), dtype (Literal['float'] |type[float]) →ArrayType[float]x (
Any), dtype (str|type) →Any
Numbers and constants are converted at trace time, copapy values by a stencil. Float to int truncates towards zero (like Python int()), a bool is True for all values other than zero. Bool values are stored as int, a conversion from bool to int generates no code.
- Parameters:
x (
Any) – Number, copapy value, vector, tensor or arraydtype (
str|type) – Target type: ‘float’, ‘int’ or ‘bool’ or the Python types float, int or bool
- Returns:
x converted to dtype, x itself if it already has the type