# Integers can only be raised to positive integral powers in db2mag computation

9 views (last 30 days)
Life is Wonderful on 8 Jan 2021
Commented: Life is Wonderful on 29 Sep 2021
I am converting matlab code to c using fixed point coder app
I am getting the below error
Input to function db2mag_conv
decibelTomag.u1 = 10
decibelTomag.u2 = [-90:0.1:90]
function [y] = db2mag_conv(decibelTomag)
y = power(decibelTomag.u1,double(decibelTomag.u2/20))
end
Error signature
### Begin Fixed Point Simulation using Scaled Doubles : db2mag_conv
Error using >intpower', 'C:/Program Files/MATLAB/R2020b/toolbox/eml/lib/matlab/ops/power.m', 187)">power>>intpower (line 187)
Integers can only be raised to positive integral powers
I need help in resolving the error

Chidvi Modala on 21 Sep 2021
The reason for the error is using negative powers as input to test the generated code. In general, power function can be used in MATLAB for negative powers by typecasting the input to "double". But power function for fixed point data ‘fi’ type has the following restriction.
Exponent must be a non-negative, real, and integer-valued scalar.
power(fi(1.5),fi(2.5))
throws error in MATLAB. Because of this restriction, if you generate code for
function [C] = power_fxpt(A,B)
C = power(double(A),double(B));
the arguments are always converted to integers in the generated code as shown below.
C = fi(power(fi_toint(double(A)),fi_toint(double(B))), 0, 16, 5, fm);
I have brought this issue to the notice of the concerned people, and it might be considered in the future releases.
As a workaround, you can keep the original types as double or single and you can use function replacement to achieve this.
Example: Write original code in power_fxpt.m as
function [C] = power_fxpt(A,B)
C = localpower(double(A),double(B));
function C = localpower(A, B)
C = power(A, B);
Write a replacement function mypower.m as
function C=mypower(A,B)
C = power(double(A),double(B));
Convert to fixed-point using:
cfg = coder.config('fixpt');
cfg.TestBenchName = 'mp_tb';
codegen -config cfg power_fxpt
Another option, if the input ranges are small is to create lookup table manually for the limited set of input values.
Life is Wonderful on 29 Sep 2021
Thanks a lot for detailed write up and steps !!

Nitin Kapgate on 13 Jan 2021
You will need to cast both "decibelTomag.u1" and "decibelTomag.u2" as single type (Fixed Point Coder App doesn't support doubles) as follows:
y = power(single(decibelTomag.u1),single(decibelTomag.u2/20))
This should help in getting your problem resolved.
Steven Lord on 15 Jan 2021
Raising an integer value to a non-integer power does not necessarily result in an integer value.
y = 2^(1/2)
y = 1.4142
Nor does raising that integer to a non-positive integer power always result in an integer value.
w = 2^(-1)
w = 0.5000
Here's what I receive when I replace the double precision integer value 2 with a 2 stored in an integer type.
try, x = uint32(2)^(1/2), catch ME, disp("Error was:" + newline + ME.message), end
Error was: Integers can only be raised to positive integral powers.
try, z = uint32(2)^(-1), catch ME, disp("Error was:" + newline + ME.message), end
Error was: Integers can only be raised to positive integral powers.
You could try:
%{
function [y] = db2mag_conv(decibelTomag)
y = power(double(decibelTomag.u1),double(decibelTomag.u2/20))
end
%}
but I'm not sure if the two casts to double will cause problems in code generation.
Life is Wonderful on 18 Jan 2021
Thanks steven, But stil the issue did not go away, can you please suggest another approach ?