Integer conversion without precision loss for literal function inputs

The function uint64 can take a literal input that is not representable by double and convert without precision loss, like
uint64(7725400999518902274)
ans = 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)
7725400999518902272
I would have to do
test(uint64(7725400999518902274))
7725400999518902274
Does anyone know if there is a trick to get this functionality or I am otherwise missing something?

4 件のコメント

@AB. In the argument block, the variable a is a double number, it is converted to uint64.
uint64(double(7725400999518902274))
ans = uint64 7725400999518902272
uint64(7725400999518902274)
ans = uint64 7725400999518902274
Matt J
Matt J 2025 年 11 月 20 日
編集済み: Matt J 2025 年 11 月 20 日
Unfortunately, this functionality does not seem to extend to a function with an argument block and type validation.
The argument block isn't the issue. Here's the same problem without it:
test(7725400999518902274)
7725400999518902272
function test(a)
a=uint64(a);
disp(a)
end
"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)
ans = uint64 18446744073709551615
uint64(2^64-1)
ans = uint64 18446744073709551615
uint64(2^64+1)
ans = uint64 18446744073709551615
AB
AB 2025 年 11 月 20 日
Thanks everyone for the discussion, this might have been a bit of an unfair question without an answer beyond "no, there isn't".
To clarify, uint64 provides a special execution path that allows a literal that is not representable by a double but is representable by a uint64 to be created as a uint64 without precision loss, and I was hoping there might be a similar special handling that utilizes the arguments block.

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2025 年 11 月 20 日
編集済み: Matt J 2025 年 11 月 20 日
test 7725400999518902274
a = uint64 7725400999518902274
function test(a)
arguments
a (1,:) string
end
a=eval("uint64("+ a + ")"),
end

8 件のコメント

Matt - You are providing a string as an input, however OP is providing a double value.
The same problem resurfaces when providing the input as double -
test(7725400999518902274)
a = uint64 7725400999518902272
function test(a)
arguments
a (1,:) string
end
a=eval("uint64("+ a + ")"),
end
Matt J
Matt J 2025 年 11 月 20 日
編集済み: Matt J 2025 年 11 月 20 日
No, the OP is providing the number as a literal. The Matlab parser clearly does not interpret literal input to uint64 as a double, since,
A=uint64(7725400999518902274);
B=uint64(double(7725400999518902274));
isequal(A,B)
ans = logical
0
AB
AB 2025 年 11 月 20 日
This might actually be an ok solution for some use cases, just letting us specify a string with a literal. You could hit it with an sscanf though instead of the eval. I'll accept since this is probably the best we can do right now. Thanks!
Dyuman Joshi
Dyuman Joshi 2025 年 11 月 20 日
@Matt J - Yes, you are right. That was an oversight by me.
Dyuman Joshi
Dyuman Joshi 2025 年 11 月 20 日
I wondered whether uint64() of a string object could work, but unfortunately it does not.
ABC="7725400999518902274"
ABC = "7725400999518902274"
double(ABC)
ans = 7.7254e+18
uint64(ABC)
Error using uint64
Conversion to uint64 from string is not possible.
Is there any reason to prefer command syntax? Function syntax seems to work fine.
test 7725400999518902274
a = uint64 7725400999518902274
test("7725400999518902274")
a = uint64 7725400999518902274
function test(a)
arguments
a (1,:) string
end
a=eval("uint64("+ a + ")"),
end
Matt J
Matt J 2025 年 12 月 19 日
Fewer keystrokes?

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2025 年 11 月 21 日

2 投票

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.

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

製品

リリース

R2024b

質問済み:

AB
2025 年 11 月 20 日

コメント済み:

2025 年 12 月 19 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by