function handle uint64 bug?

In R2014a I find,
uint64(2025027714713048816) %no problem, returns input
@(x) uint64(2025027714713048816) %complains about Unexpected MATLAB expression
@(x) uint64(20250277147130) %works
The implication would be that you cannot create a function handle that uses a uint64 constant that is larger than 2^53-1 .
Could someone check this in later versions?

2 Comments

Hi,
Is there any specific reason for creating a function handle with an input which is never used?
A better way to create a handle to a uint64 would be to use,
>> ah = @(x) uint64(x)
and then pass that value to ah as follows
>> ah(2025027714713048816)
This works as expected.
On a side note in MATLAB R2016a the following output is generated:
>> @(x) uint64(2025027714713048816)
ans =
@(x)uint64(2025000000000000000)uint64(27714713048816)
Whereas,
>> a = @(x) uint64(2025027714713048816)
gives the Error message:
Unexpected MATLAB expression.
As per the above outputs it makes sense as to why the assignment to an handle does not work out.
The reason for the first output being represented as two uint64's lies in the precision losses and mantissa size (52 bits for floating points). You can check the following documentation link for more information:
Hope this helps
regards
Rakesh
Rakesh, you can do
@(x) uint64(2025027714713048816) + x
and you will still get "Unexpected MATLAB expression"
The fact that @(x) uint64(20250277147130) works but @(x) uint64(2025027714713048816) does not (in R2014a) shows that it is not a syntax error and not a problem with not using the argument: it is a problem with 2025027714713048816 being above 2^53-1. The same problem happens with
@(varargin) uint64(2025027714713048816)
There are good reasons to write functions that have inputs but ignore them. For example,
f = @(x) uint64(2025027714713048816)
is a implementation of
y = uint64(2025027714713048816)
The problem occurs if the uint64 or int64 of a large constant occurs anywhere in the anonymous function:
a = @(x) double(x + uint64(2025027714713048816))
is surely a valid meaningful function, but it is complained about as being a syntax error.
In R2014a,
ah = @(x) uint64(x);
ah(2025027714713048816)
gives 2025027714713048832 as the output, because the keeping of 2025027714713048816 as a uint64 is done at parse time for uint64(2025027714713048816) rather than at execution time.

Sign in to comment.

 Accepted Answer

Walter Roberson
Walter Roberson on 21 Apr 2016
This appears to be fixed in R2016a

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!