Cast
更多信息
- Cast 仅在编译代码中可用.
- Cast[val,type,method] 中 method 的可能值包括:
-
"BitCast" 将代表 val 的位重新解释为具有类型 type "CCast" 模仿 C 语言中的转换行为 "ConversionCast" 将 val 转换为类型 type - 默认的转换方法为 "ConversionCast".
- Cast[val,type,"BitCast"] 的表现就像 val 被写入内存并作为 type 读回. bitcast 的输入类型必须与输出类型的内存大小相同.
- Cast[val,type,"CCast"] 只支持数字类型.
- Cast[val,type,"ConversionCast"] 有可能失败并生成出错消息.
范例
打开所有单元 关闭所有单元基本范例 (2)
在编译函数中将 "Integer64" 转换为 "Real64":
cf = FunctionCompile[Function[Typed[arg, "Integer64"],
Cast[arg, "Real64"]
]]cf[2]cf = FunctionCompile[Function[Typed[arg, "InertExpression"],
Reverse[Cast[arg, "PackedArray"::["Real64", 1]]]
]]cf[{1, 2, 3}]cf["test"]范围 (2)
cf = FunctionCompile[Function[{},
Module[{ptr, res},
ptr = LibraryFunction["malloc"][Typed[1, "UnsignedInteger64"]];
res = Cast[ptr, "UnsignedInteger64", "BitCast"];
LibraryFunction["free"][ptr];
res
]
]]cf[]cf = FunctionCompile[Function[Typed[arg, "Integer64"], Cast[arg, "Integer32", "CCast"]]]cf[100]cf[2 ^ 32]可能存在的问题 (1)
相关指南
-
▪
- 编译类型 ▪
- Code Compilation
Wolfram Research (2022),Cast,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Cast.html.
文本
Wolfram Research (2022),Cast,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Cast.html.
CMS
Wolfram 语言. 2022. "Cast." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/Cast.html.
APA
Wolfram 语言. (2022). Cast. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/Cast.html 年
BibTeX
@misc{reference.wolfram_2026_cast, author="Wolfram Research", title="{Cast}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/Cast.html}", note=[Accessed: 08-August-2026]}
BibLaTeX
@online{reference.wolfram_2026_cast, organization={Wolfram Research}, title={Cast}, year={2022}, url={https://reference.wolfram.com/language/ref/Cast.html}, note=[Accessed: 08-August-2026]}