Why MatLab dont use the globalfimath properties

2 views (last 30 days)
I set the globalfimath properties with this code:
resetglobalfimath;
Fm = fimath(...
'ProductMode', 'SpecifyPrecision', ...
'MaxProductWordLength', WordLength, ...
'ProductWordLength', WordLength, ...
'ProductFractionLength', FractionalLength, ...
'SumMode', 'SpecifyPrecision', ...
'MaxSumWordLength', WordLength,...
'SumWordLength', WordLength, ...
'SumFractionLength', FractionalLength, ...
'OverflowAction', 'Wrap', ...
'RoundingMethod', 'Floor');
globalfimath(Fm)
and i get the right properties returned...for example:
>>fimath
ans =
RoundingMethod: Floor
OverflowAction: Wrap
ProductMode: SpecifyPrecision
ProductWordLength: 64
ProductFractionLength: 53
SumMode: SpecifyPrecision
SumWordLength: 64
SumFractionLength: 53
CastBeforeSum: true
if i now try to use the fi() constructor it seems he always create a local fi-object. for example i get this:
>> A = fi(pi)
A =
3.1416
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 13
What can i do to get the same word- and franctionlength for every fi-object?

Answers (1)

Jessica Clayton
Jessica Clayton on 2 Dec 2020
I understand that you are trying to use globalfimath to set the word length and fraction length to have the same values for every fi object. fimath controls the rules for performing arithmetic operations on fi object, however it does not control the word length or fraction length of the fi object. You can check that the fimath properties you defined were applied at construction of the fi object by using A.fimath and confirming that the output is the same as the specified globalfimath.
WordLength = 64;
FractionalLength = 53;
resetglobalfimath;
Fm = fimath(...
'ProductMode', 'SpecifyPrecision', ...
'MaxProductWordLength', WordLength, ...
'ProductWordLength', WordLength, ...
'ProductFractionLength', FractionalLength, ...
'SumMode', 'SpecifyPrecision', ...
'MaxSumWordLength', WordLength,...
'SumWordLength', WordLength, ...
'SumFractionLength', FractionalLength, ...
'OverflowAction', 'Wrap', ...
'RoundingMethod', 'Floor');
globalfimath(Fm);
A = fi(pi);
A.fimath
ans =
RoundingMethod: Floor
OverflowAction: Wrap
ProductMode: SpecifyPrecision
ProductWordLength: 64
ProductFractionLength: 53
SumMode: SpecifyPrecision
SumWordLength: 64
SumFractionLength: 53
CastBeforeSum: true
ans =
RoundingMethod: Floor
OverflowAction: Wrap
ProductMode: SpecifyPrecision
ProductWordLength: 64
ProductFractionLength: 53
SumMode: SpecifyPrecision
SumWordLength: 64
SumFractionLength: 53
CastBeforeSum: true
One way to assign the same word length and fraction length to every fi object is to use the “cast like” functionality as documented here. For example,
a = pi;
p = fi([],1,64,53);
b = cast(a,'like',p)
b.fimath
b =
3.1416
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 64
FractionLength: 53
ans =
RoundingMethod: Floor
OverflowAction: Wrap
ProductMode: SpecifyPrecision
ProductWordLength: 64
ProductFractionLength: 53
SumMode: SpecifyPrecision
SumWordLength: 64
SumFractionLength: 53
CastBeforeSum: true

Community Treasure Hunt

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

Start Hunting!