Stop Bit growth for power computation

Hi
I am in need a logic to check bit growth in case of word length is 32 /16/8-bit
I am using following code to check bit growth and cap the max and min 32 bit value But this seems not working for case -2 !!
% Case -1
C = power(-97,28)
C = 4.2620e+55
if (C >= (2^32-1))
C = min(C,2^31);
end
if (C >= -2^31)
C = -2^31; %min(C,-2^31)
end
C
C = -2.1475e+09
% Case -2
C = power(-97,29)
C = -4.1341e+57
if (C >= (2^32-1))
C = min(C,2^31)
end
if (C >= -2^31)
C = -2^31; %min(C,-2^31)
end
C
C = -4.1341e+57
I need logic for both positive and nagative mantissa and exponent value . Result C must not exceed 32 bit word size
Thank you!!

Réponses (2)

John D'Errico
John D'Errico le 30 Sep 2021
Modifié(e) : John D'Errico le 30 Sep 2021
Case 1: You CANNOT raise a double precision number to a power such that it exceeds flintmax (2^53 - 1) and expect the result to be correct.
flintmax
ans = 9.0072e+15
And that means when you execute this:
power(-97,28)
ans = 4.2620e+55
you should expect pure garbage if you expect the result to have correct digits.
sym(-97)^28 % correct
ans = 
42619520516862344959898006540158454742326868747150111361
power(-97,28) % mostly garbage
ans = 4.2620e+55
sprintf('%55f',power(-97,28)) % note the divergence in the lower digits
ans = '42619520516862345006904392299734156132045387227709046784.000000'
Case 2: While you MAY think that -2^31 raises the number -2 to a negative power, in fact, it forms 2^31, and then negates that result. If the power is odd, then this does not matter, because the negative sign works then. But if the power is even, then it does matter.
Raising a number to a power has a higher order of precedence than does unary minus. So these two operations are not the same:
-2^30
ans = -1.0737e+09
(-2)^30
ans = 1.0737e+09
I used an even power to show they are distinct there.
If you want to saturate one way you can do this is to use integer arithmetic.
b = int32(-97)
b = int32 -97
C = power(b, 28)
C = int32 2147483647
Alternately you could use intmin and intmax as your limits. These functions can return the limits of any of the eight integer types (signed and unsigned 8, 16, 32, and 64 bit integers.)
q = 2^33
q = 8.5899e+09
q > intmax('int32') % true
ans = logical
1
q > intmax('int64') % false
ans = logical
0
But the points John D'Errico raised are also things you should consider when performing your calculations.

4 commentaires

Life is Wonderful
Life is Wonderful le 30 Sep 2021
Modifié(e) : Life is Wonderful le 30 Sep 2021
Thanks , @John D'Errico @Steven Lord, How to make the check for complex if mantissa is complex number instead of integer ?
Since mantissa is a negative number (-97) and raising power to negative won't work for power .
So mantissa is complex number instead of integer
I think this is true for intmax and flintmax ( IEEE-single (2^24) and double (2^53)case), so my question is how I am going to make a range check for min and max value ?
I'm no longer sure I understand what you're trying to do. You can raise a complex number to a power (positive or negative) or vice versa.
x = (3+4i)^pi
x = -1.5292e+02 + 3.5547e+01i
y = (3+4i)^(-exp(1))
y = -0.0102 - 0.0073i
z = 2^(5+12i)
z = -14.3148 + 28.6197i
w = (-3)^7i
w = 4.5863e-11 + 2.7766e-10i
So perhaps you can start at the beginning. What task are you trying to perform that requires you to check or limit the results of this calculation to within a certain bound? Are you planning to do this in MATLAB or do you eventually want / hope to deploy this using MATLAB Compiler or MATLAB Coder or something similar?
Life is Wonderful
Life is Wonderful le 1 Oct 2021
Modifié(e) : Life is Wonderful le 1 Oct 2021
Hi Steven/John,
limit the results of this calculation to within a certain bound
[Yes]
do you eventually want / hope to deploy this using MATLAB Compiler or MATLAB Coder or something similar?
Yes
Note - Bit growth occurs when fractionallength/Wordlength is too low/high > 32 size.
Concept power fcn in 32 bit domain
output = power(matissa,exp);
mantissa = int32('man_val')/uint32('man_val')
exponent = int32('exp_val')/uint32('exp_val')
Mantissa | Exponent | output |
+value | +value | +value |
+value | -value | +value |
-value | +value |-/+value | ==> exponent value ( Odd / Even )
-value |-value |-/+value | ==> exponent value ( Odd / Even )
0 | +value | 0 |
0 | -value | inf |
0 | 0 | 1 |
+value | 0 | 1 |
-value | 0 | 1 |
The issue is what ever might the power function doing - I would like to cap the bit growth/output within 32 bit word size.
output = power(mantissa,exp);
Below implementation does not work correctly !! This is already explained well by @John D'Errico
if (output > intmax('int32')/[flintmax('double')/flintmax('single')])
outpout = intmax('int32');
elseif (output < intmin('int32'))
output = intmin('int32');
.
.
.
.
.
end
I want the output to have limit range of 32 bit .
x word length
y fraction lenght
1 /0 signed bit
outout_32 = [value, signedbit,wordlenght,fracitonal_length]. You have already captured the reason and tool for bit growth condition above,
Thank you!!
Life is Wonderful
Life is Wonderful le 14 Oct 2021
Please comment on my latest implementation . Please refer below link
Thank you!!

Connectez-vous pour commenter.

Produits

Version

R2021a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by