Numerictype for fimath output

2 visualizzazioni (ultimi 30 giorni)
Megan
Megan il 11 Mar 2014
Modificato: Shivansh il 10 Set 2024
When adding two fixed point numbers, I am not understanding the output I am getting. I think it may have to do with a subtlety I am missing with fimath properties. Here is an example:
FMval = fimath;
FMval.SumMode = 'KeepMSB';
FMval.SumWordLength = bitDepthd;
FMval.ProductMode = 'KeepMSB';
FMval.ProductWordLength = bitDepthd;
FMval.OverflowAction = 'Saturate';
FMval.RoundingMethod = 'Nearest';
A = fi(3, 0, 4, 0, FMval);
B = fi(5, 0, 4, 0, FMval);
I would think that A+B = 8, but when I add them together:
>> C = A+B
C =
10
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 4
FractionLength: -1
Why is the answer not 8? Why did the FractionLength go to -1? There is no overflow in this addition, and both variables have a fractionlength of 0.
I understand that adding two four bit numbers requires 5 bits, and then I could maybe understand an answer of, say, sixteen (moving the fractionlength to -1), but 5(0011 bin) + 3(0011 bin) should not equal 10 any way I can figure.
Another example, this time with a 4-bit overflow:
D = fi(12, 0, 4, 0, FMval); E = fi(7, 0, 4, 0, FMval);
>> F = E+D
F =
20
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 4
FractionLength: -1
What am I missing here? Is there a way to set the fimath properties to get the answers I'm expecting?

Risposte (1)

Shivansh
Shivansh il 10 Set 2024
Modificato: Shivansh il 10 Set 2024
Hello Megan,
As you mentioned above, adding two 4-bits number will need 5 bits, but you have set the "fimath" to work with 4-bits and as a result, you are getting unexpected results. A possible approach to get the desired result can be to change the "SumMode" to "FullPrecision".
bitDepthd = 4;
FMval = fimath;
FMval.SumMode = 'FullPrecision';
FMval.SumWordLength = bitDepthd;
FMval.ProductMode = 'KeepMSB';
FMval.ProductWordLength = bitDepthd;
FMval.OverflowAction = 'Saturate';
FMval.RoundingMethod = 'Nearest';
A = fi(3, 0, 4, 0, FMval);
B = fi(5, 0, 4, 0, FMval);
C=A+B
C =
8 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 5 FractionLength: 0 RoundingMethod: Nearest OverflowAction: Saturate ProductMode: FullPrecision SumMode: FullPrecision
C.storedInteger
ans = uint8 8
I hope it clarifies the issue!

Community Treasure Hunt

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

Start Hunting!

Translated by