首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Matlab :如何在不扩展底层数据类型的情况下执行定点算法?

Matlab :如何在不扩展底层数据类型的情况下执行定点算法?
EN

Stack Overflow用户
提问于 2013-09-25 10:40:06
回答 1查看 2.6K关注 0票数 7

谷歌在这个问题上保持沉默。我目前正在实现一个数字计算器上只有16位符号不动点在Matlab中。但是,对16位定点的算术操作会导致数据类型扩展到以下几个方面

代码语言:javascript
复制
>> 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会导致错误。为什么会发生此错误?

代码语言:javascript
复制
>> 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};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-25 11:44:10

本地解决方案:您可以使用fimath对象指定产品结果的精度

代码语言:javascript
复制
F = fimath('OverflowMode','Wrap', 'RoundMode', 'Floor', ...
           'ProductMode', 'SpecifyPrecision', ...
           'ProductWordLength', 16, 'ProductFractionLength', 2);
dis.fimath = F;

结果是:

代码语言:javascript
复制
>> 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

全局解决方案:,或者,如果您希望将其应用于所有可以使用的不动点变量

代码语言:javascript
复制
globalfimath('OverflowMode','Wrap', 'RoundMode', 'Floor', ...
             'ProductMode', 'SpecifyPrecision', ...
             'ProductWordLength', 16, 'ProductFractionLength', 2);

注意到,要限制在16位以内,还需要指定sum的精度(使用fimathSumModeSumWordLength属性),否则和的字长为17:

代码语言:javascript
复制
>> dis+dis
ans =
     3

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 17
        FractionLength: 2
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19002666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档