Check range for power function

I am looking for a unit test vectors which checks the range of input values for power function so that I can implement equivalent of C type in fix point
A = fi([1:2^5/32:2^5],0,32); % A > 0, so it starting with lowest positive fraction value
B = fi([-2:1e-2:2],1,32);
% C = power(A,B) % seeking help here for C type power implementation but
% gettting error
% % Error using fixed.internal.power.validateK (line 13)
% % Exponent input to 'power' must be a real scalar and the value must be a non-negative integer.
% %
% % Error in fixed.internal.power.powImpl>validateInputs (line 39)
% % fixed.internal.power.validateK(k);
% %
% % Error in fixed.internal.power.powImpl (line 9)
% % validateInputs(a, k);
% %
% % Error in .^ (line 32)
% % y = fixed.internal.power.powImpl(a, k);
figure(100);
A = [1:32];
B = [-2:2];
hold on; grid on;
plot(power(A(1),B(1:end)),'r-.');
plot(power(A(2),B(1:end)),'g-.');
plot(power(A(3),B(1:end)),'b-.');
plot(power(A(4),B(1:end)),'m-.');
%.
%.
%.
%.
plot(power(A(32),B(1:end)),'k-.','linewidth', 2.0);

回答(1 个)

Walter Roberson
Walter Roberson 2021-9-14

0 个投票

When you use fi, the power must be a positive integer scalar.
Also, log(A) does not appear to be supported so you cannot use the usual exp(log(A) .* B)
You will need to implement your own fractional power, perhaps using a CORDIC method.

3 个评论

Thanks @Walter Roberson, fi was used for ensuring Q format but this can be deleted. Perhaps I need clue for implementing own fractional power i.e. [value^(1/2) == sqrt(value)]
81(3/4) = sqrt(81)*power3 which is 27
y = power(sqrt(sqrt(81)),3)
y = 27
C = power(1,-2)
C = 1
For me it is not clear , if you can confirm cordiccexp(log(A) .* B) , you mean ?? I am missing what
A = fi([1:2^5/32:2^5],0,32); % A > 0, so it starting with lowest positive fraction value
B = fi([-2:1e-2:2],1,32);
y = cordiccexp(log(A) .* B);
Check for incorrect argument data type or missing argument in call to function 'log'.
y = cordiccexp(log(1) .* -2);
y
y = 1.0000 - 0.0000i
y = power(2,cordiccexp(log(2) .* -2))
y = 0.8820 - 0.7153i
z = power(2 ,-2)
z = 0.2500
y = nthroot(1,-2);power(2,y) % IS THIS YOUR INTERPRETATION OWN FRACTIONAL POWER
ans = 2
I could see following case for computing the power with base and exponent condition
A is integer and B is exponent
if A < 0 && B < 0
C = power(A,B);
elseif A > 0 && B < 0
C = power(A,B);
elseif A == 0 && B < 0
C = power(A,B);
elseif A > 0 && B == 0
C = power(A,B);
elseif A > 0 && B > 0
C = power(A,B);
end

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 MATLAB 的更多信息

产品

版本

R2021a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by