Cast

Cast[val,type]

converts val to the type type, for use in compiled code.

Cast[val,type,method]

uses the specified casting method.

Details

  • Cast[value,type] reinterprets the bits representing val as having the type type.
  • Cast is only available in compiled code.
  • Possible values of method in Cast[val,type,method] include:
  • "BitCast"reinterprets the bits representing val as having the type type
    "CCast"emulates the behavior of casting in C
  • Cast[val,type,"BitCast"] behaves as though val is written to memory and read back as type.
  • Cast[val,type,"CCast"] only supports numeric types.
  • The input type to a bitcast must be the same length as the output type.

Examples

open allclose all

Basic Examples  (2)

Cast an "Integer64" to a "Real64" in a compiled function:

Cast an expression to a packed array, suitable for use with other compiled functions:

Some expressions cannot be converted into packed arrays:

Scope  (2)

Bitcast a pointer to an integer:

Cast from a larger type to a smaller one with a C-style cast:

As in C, the "CCast" method does not check for numerical overflows:

Possible Issues  (1)

The default casting method "ConversionCast" does not currently support casting from larger types to smaller types:

Instead, "CCast" can be used:

However, "CCast" does not check for numerical overflows:

Wolfram Research (2022), Cast, Wolfram Language function, https://reference.wolfram.com/language/ref/Cast.html.

Text

Wolfram Research (2022), Cast, Wolfram Language function, https://reference.wolfram.com/language/ref/Cast.html.

CMS

Wolfram Language. 2022. "Cast." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Cast.html.

APA

Wolfram Language. (2022). Cast. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Cast.html

BibTeX

@misc{reference.wolfram_2024_cast, author="Wolfram Research", title="{Cast}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/Cast.html}", note=[Accessed: 21-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_cast, organization={Wolfram Research}, title={Cast}, year={2022}, url={https://reference.wolfram.com/language/ref/Cast.html}, note=[Accessed: 21-December-2024 ]}