Integer conversion without precision loss for literal function inputs
58 次查看(过去 30 天)
显示 更早的评论
The function uint64 can take a literal input that is not representable by double and convert without precision loss, like
uint64(7725400999518902274)
Unfortunately, this functionality does not seem to extend to a function with an argument block and type validation.
function test(a)
arguments
a (1,1) uint64
end
disp(a)
end
test(7725400999518902274)
I would have to do
test(uint64(7725400999518902274))
Does anyone know if there is a trick to get this functionality or I am otherwise missing something?
4 个评论
Dyuman Joshi
2025-11-20,15:16
"The function uint64 can take a literal input that is not representable by double and convert without precision loss"
That is not true, it can do that within [0, 2^64-1]
uint64(123456789012345678901)
uint64(2^64-1)
uint64(2^64+1)
采纳的回答
Matt J
2025-11-20,2:28
编辑:Matt J
2025-11-20,13:51
test 7725400999518902274
function test(a)
arguments
a (1,:) string
end
a=eval("uint64("+ a + ")"),
end
5 个评论
Dyuman Joshi
2025-11-20,16:37
@AB, using sscanf is what is suggested in the documentation as well - https://in.mathworks.com/help/matlab/ref/uint64.html#mw_73911831-6f61-4aae-89d5-5afb9ff9a902.
更多回答(1 个)
Walter Roberson
2025-11-21,7:55
Does anyone know if there is a trick to get this functionality
There is no way of doing that.
Any way of doing that would have to affect the inputs at parse time. However, arguement blocks do not affect parse time. Arguement blocks apply conversions to whatever input was passed in. By the time the arguement block processing is applied, the parameter has already been parsed as double precision.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!