Error in fi divide for embedded fi object

7 次查看(过去 30 天)
Hi
I am trying to make a fixed point calculation with below code. But I run into error , I don't understand what is wrong in my code.
Note : Error results
clearvars;clc;
% define ranage for input data
base = fi([-1:-1:-(2^3),1:1:(2^3)],1,32,27);
exp = fi([-(2^4-6):1:-1,1:1:(2^4-6)],1,32,27);
power = 1;
if (exp > 0)
while(exp~=0)
power = power * base;
exp = accumneg(exp, 1);
end
else
while(exp~=0)
power = power * 1/base;
exp = accumpos(exp , 1);
end
end
Error using embedded.fi/mrdivide>fidivide (line 56)
For fi objects, B must be a scalar in A/B.

Error in / (line 23)
c = fidivide(a,b);
Without fi object code, below code is fine
clearvars;clc;
% define ranage for input data
base = [-1:-1:-(2^3),1:1:(2^3)];
exp = [-(2^4-6):1:-1,1:1:(2^4-6)];
power = 1;
fprintf('%20s|%20s|%20s|%20s|%20s|%20s|\n','i','j','power','base','exponent','NBits')
for i = 1:length(base)
for j = 1:length(exp)
power(i,j) = power_flpt(base(i),exp(j));
Bits = power(i,j);
NBits = log2(Bits);
fprintf('%20d|%20d|%20f|%20f|%20f|%20f|\n',i,j,power(i,j),base(i),exp(j),NBits);
end
end
function power = power_flpt(base,exp)
power = 1;
if (exp > 0)
while(exp~=0)
power = power * base;
exp = accumneg(exp, 1);
end
else
while(exp~=0)
power = power * 1/base;
exp = accumpos(exp , 1);
end
end
end

采纳的回答

Tom Bryan
Tom Bryan 2021-10-14
The error is telling you that 1/base isn't supported in fi because base is a vector. The error is telling you that base must be a scalar for mrdivide (matrix right-divide). Element-wise division is 1./base, which would not have thrown an error, but the first code was logically incorrect anyway. The first code didn't give the right answer with double either. It was using things like "if (exp > 0)" where exp is a vector, so the if condition was never entered. Also the "while(exp~=0)" condition was never entered either.
The second code works with both fixed-point and floating-point becasue the power_fltpt function only uses scalars.
Also, both exp and power are builtin MATLAB functions, so you should think about renaming them. MATLAB is forgiving about overloading other functions, but it may interfere if you ever want to use the exp or power functions.
Enclosed below is your second code running with both fixed-point and floating point, and comparing the answers at the end.
clearvars;clc;
% define ranage for input data
base = fi([-1:-1:-(2^3),1:1:(2^3)],1,32,27);
exp = fi([-(2^4-6):1:-1,1:1:(2^4-6)],1,32,27);
fprintf('\n---------------------------------\n')
fprintf('Fixed Point\n')
power_fixedpoint = power_loop(base,exp);
fprintf('\n---------------------------------\n')
fprintf('Double\n')
power_float = power_loop(double(base),double(exp));
fprintf('\n---------------------------------\n')
fprintf('Difference\n')
difference = power_float - double(power_fixedpoint) %#ok<NOPTS>
max_difference = max(abs(difference(:))) %#ok<NOPTS>
function power = power_loop(base,exp)
power = ones(length(base),length(exp));
fprintf('%20s|%20s|%20s|%20s|%20s|%20s|\n','i','j','power','base','exponent','NBits')
for i = 1:length(base)
for j = 1:length(exp)
power(i,j) = power_scalar_function(base(i),exp(j));
Bits = power(i,j);
NBits = log2(Bits);
fprintf('%20d|%20d|%20f|%20f|%20f|%20f|\n',i,j,power(i,j),base(i),exp(j),NBits);
end
end
end
function power = power_scalar_function(base,exp)
power = 1;
if (exp > 0)
while(exp~=0)
power(:) = power * base;
exp = accumneg(exp, 1);
end
else
while(exp~=0)
power(:) = power * 1/base;
exp = accumpos(exp , 1);
end
end
end
Best wishes,
Tom Bryan
  15 个评论
Tom Bryan
Tom Bryan 2021-10-16
The error with the builtin fi power method is a limitation but not a bug. The error message is telling you how to set the fimath to meet the requirements to use with a fi object: "Exponent input to 'power' must be a real scalar and the value must be a non-negative integer." See the documentation for fimath for how to use it with a fi object.
After setting fimath, you will run into another limitation with the builtin fi power method. It only works with positive exponents, and your example uses positive and negative exponents. Again, this is a limitation but not a bug.
Life is Wonderful
Life is Wonderful 2021-10-17
Many thanks . Yes , I can understand limitation !!
best regards

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by