Integer conversion without precision loss for literal function inputs

조회 수: 27 (최근 30일)
AB
AB 2025년 11월 20일
댓글: Matt J 2025년 12월 19일 15:15
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
Dyuman Joshi
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)
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
Paul
Paul 2025년 12월 19일 4:02
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
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.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by