Integer conversion without precision loss for literal function inputs
25 ビュー (過去 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 日
"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 日
編集済み: Matt J
2025 年 11 月 20 日
test 7725400999518902274
function test(a)
arguments
a (1,:) string
end
a=eval("uint64("+ a + ")"),
end
8 件のコメント
Paul
2025 年 12 月 19 日 4:02
Is there any reason to prefer command syntax? Function syntax seems to work fine.
test 7725400999518902274
test("7725400999518902274")
function test(a)
arguments
a (1,:) string
end
a=eval("uint64("+ a + ")"),
end
その他の回答 (1 件)
Walter Roberson
2025 年 11 月 21 日
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!