Google在此问题上保持沉默。我目前在Matlab中仅在16位有符号固定点上实现数字计算器。但是对16位定点进行算术运算会导致将数据类型扩展到以下类型
>> a = int16(1.5 * 4)
a = 6
>> T = numerictype(1, 16, 2)
T = DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 2
>> dis = reinterpretcast(a, T)
dis = 1.5000
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 2
>> c = dis * dis
c = 2.2500
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 32
FractionLength: 4
我希望变量c
保持在WordLength 16,FractionLength 2中。是否有可能在不扩展基础数据类型的情况下完成16位固定点的算术运算?我要冒任何上溢和下溢的风险。任何帮助都是极好的。
编辑:键入fimath
命令窗口导致错误。为什么会发生此错误?
>> F = fimath('OverflowAction','Wrap', 'RoundingMethod', 'Floor', ...
'ProductWordLength', 16, 'ProductFractionLength', 2);
No public field OverflowAction exists for class embedded.fimath.
Error in fimath (line 72)
this.(varargin{k}) = varargin{k+1};
本地解决方案:您可以使用一个fimath
对象来指定乘积结果的精度:
F = fimath('OverflowMode','Wrap', 'RoundMode', 'Floor', ...
'ProductMode', 'SpecifyPrecision', ...
'ProductWordLength', 16, 'ProductFractionLength', 2);
dis.fimath = F;
那么结果将是:
>> dis*dis
ans =
2.25
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 2
RoundMode: floor
OverflowMode: wrap
ProductMode: SpecifyPrecision
ProductWordLength: 16
ProductFractionLength: 2
SumMode: FullPrecision
MaxSumWordLength: 128
全局解决方案:或者,如果您希望将此方法应用于所有定点变量,则可以使用
globalfimath('OverflowMode','Wrap', 'RoundMode', 'Floor', ...
'ProductMode', 'SpecifyPrecision', ...
'ProductWordLength', 16, 'ProductFractionLength', 2);
请注意,要限制为16位,您还需要指定sum的精度(的使用SumMode
和SumWordLength
属性fimath
),否则sum的字长为17:
>> dis+dis
ans =
3
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 17
FractionLength: 2
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句