Stop Bit growth for power computation
Afficher commentaires plus anciens
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)
if (C >= (2^32-1))
C = min(C,2^31);
end
if (C >= -2^31)
C = -2^31; %min(C,-2^31)
end
C
% Case -2
C = power(-97,29)
if (C >= (2^32-1))
C = min(C,2^31)
end
if (C >= -2^31)
C = -2^31; %min(C,-2^31)
end
C
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
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
And that means when you execute this:
power(-97,28)
you should expect pure garbage if you expect the result to have correct digits.
sym(-97)^28 % correct
power(-97,28) % mostly garbage
sprintf('%55f',power(-97,28)) % note the divergence in the lower digits
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
(-2)^30
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)
C = power(b, 28)
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 > intmax('int32') % true
q > intmax('int64') % false
But the points John D'Errico raised are also things you should consider when performing your calculations.
4 commentaires
Life is Wonderful
le 30 Sep 2021
Modifié(e) : Life is Wonderful
le 30 Sep 2021
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
y = (3+4i)^(-exp(1))
z = 2^(5+12i)
w = (-3)^7i
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
le 1 Oct 2021
Modifié(e) : Life is Wonderful
le 1 Oct 2021
Life is Wonderful
le 14 Oct 2021
Catégories
En savoir plus sur Number Theory dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!