Stop Bit growth for power computation

조회 수: 2 (최근 30일)
Life is Wonderful
Life is Wonderful 2021년 9월 30일
댓글: Life is Wonderful 2021년 10월 14일
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!!

답변 (2개)

John D'Errico
John D'Errico 2021년 9월 30일
편집: John D'Errico 2021년 9월 30일
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.

Steven Lord
Steven Lord 2021년 9월 30일
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
Life is Wonderful
Life is Wonderful 2021년 10월 1일
편집: Life is Wonderful 2021년 10월 1일
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 2021년 10월 14일
Please comment on my latest implementation . Please refer below link
Thank you!!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by