It is asked for "signed integer". Then uint32 is not an option, because it is unsigned.
The operator is missing in:
elseif maxA<= ((2^32)-1) maxA > ((2^16)-1) ;
^^
In addition this does not restrict the minA part.
You do not have to exclude e.g. the int8 range, if it was excluded already:
if maxA<=((2^7)-1) && minA>=-(2^7);
x = 'int8';
elseif maxA<=((2^15)-1) && maxA>((2^7)-1) && minA>=-(2^15) && minA< -(2^7);
x='int16';
Easier:
if maxA <= 2^7 - 1 && minA >= -2^7
x = 'int8';
elseif maxA <= 2^15 - 1 && minA >= -2^15
x = 'int16';
I'd prefer a loop:
TypeList = {'int8', 'uint8', 'int16', 'uint16', ...
'int32', 'uint32', 'int64', 'uint64'};
x = 'NONE';
for k = 1:numel(TypeList)
if isequal(double(A), double(cast(A, TypeList{k})))
x = TypeList{k};
break;
end
end
Or:
minA = min(A(:));
maxA = max(A(:));
x = 'NONE';
for aType = {'int8', 'int16', 'int32', 'int64'}
aType = TypeList{k}
if minA >= intmin(aType) && maxA <= intmax(aType)
x = aType;
break;
end
end
Or
minA = min(A(:));
maxA = max(A(:));
x = 'NONE';
for k = [8,16,32,64]
if minA >= -2^(k-1) && maxA <= 2^(k-1)
x = sprintf('int%d', k);
break;
end
end
Best Answer