fi
构造定点数值对象
说明
要将定点数据类型赋给数字或变量,请使用 fi 构造函数创建一个 fi 对象。您可以在构造函数中或通过使用 numerictype 和 fimath 对象来指定数值属性和数学规则。
创建对象
语法
描述
函数返回一个不带值的有符号 a = fifi 对象,其字长为 16 位,小数长度为 15 位。
返回一个 a = fi(v,s,w,slopeadjustmentfactor,fixedexponent,bias)fi 对象,其值为 v,符号性为 s,字长为 w,斜率调整因子为 slopeadjustmentfactor,定点指数为 fixedexponent,偏置为 bias。
返回一个 a = fi(v,T)fi 对象,其值为 v,数据类型为 numerictype T。
返回一个 a = fi(___,Name,Value)fi 对象,其属性值由一个或多个名称-值对组参量指定。
输入参量
fi 对象的值,指定为标量、向量、矩阵或多维数组。
返回的 fi 对象的值是量化为 fi 构造函数中指定的数据类型的输入 v 的值。当输入 v 是非双精度并且您没有指定字长或小数长度时,返回的 fi 对象保留输入的 numerictype。有关示例,请参阅基于非双精度值创建 fi 对象。
仅当您完全指定 numerictype 类型的 fi 对象时,才能将非有限值 -Inf、Inf 和 NaN 指定为值。当 fi 指定为定点 numerictype 时,
NaN映射到0。当
fi对象的'OverflowAction'属性设置为'Wrap'时,-Inf和Inf映射到0。当
fi对象的'OverflowAction'属性设置为'Saturate'时,Inf映射到最大的可表示值,而-Inf映射到最小的可表示值。
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | fi
fi 对象的符号性,指定为数值或逻辑值 1 (true) 或 0 (false)。值 1 (true) 表示有符号数据类型。值 0 (false) 表示无符号数据类型。
数据类型: logical
fi 对象的字长,以位为单位,指定为正整数标量。
字长必须为 1 ≤ w ≤ 65535 范围内的整数。
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
fi 对象的存储整数值的小数长度,以位为单位,指定为整数标量。小数长度必须为 -65535 ≤ f ≤ 65535 范围内的整数。
如果不指定小数长度,则对于指定的值、字长和符号性,fi 对象会在避免出现溢出的情况下自动使用可提供最佳精度的小数长度。
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
fi 对象的定标的斜率,指定为正标量。
以下方程表示斜率偏置定标数的真实值。
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
fi 对象的定标的偏置,指定为标量。
以下方程表示斜率偏置定标数的真实值。
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
fi 对象的斜率调整因子,指定为大于或等于 1 且小于 2 的标量。
以下方程说明斜率、固定指数和斜率调整因子之间的关系。
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
fi 对象的固定指数,指定为标量。
以下方程说明斜率、固定指数和斜率调整因子之间的关系。
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
fi 对象的数值类型属性,指定为 numerictype 对象。
fi 对象的定点数学属性,指定为 fimath 对象。
如果未指定 fimath 属性,则无论 globalfimath 设置如何,fi 构造函数都会使用最接近舍入并在溢出时进行饱和处理以创建 fi 对象。有关此行为的示例,请参阅在 fi 对象构造函数中指定舍入和溢出模式。
属性
fi 对象有三种类型的属性:
您可以在创建 fi 对象时设置这些属性。使用数据属性可访问 fi 对象中的数据。根据传递性,fimath 属性和 numerictype 属性也是 fi 对象的属性。fimath 属性可确定对 fi 对象执行定点算术运算的规则。numerictype 对象包含定点对象的所有数据类型和定标属性。
示例
使用默认构造函数创建一个 fi 对象。该构造函数返回一个不带值的有符号 fi 对象,其字长为 16 位,小数长度为 15 位。
a = fi
a =
[]
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 15
创建一个有符号 fi 对象,其值为 pi,字长为 16 位,具有最佳精度小数长度。小数长度会自动设置为在不溢出的情况下尽可能获得最佳精度。
a = fi(pi)
a =
3.1416
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 13
创建一个值为 pi 的无符号 fi 对象。当您仅指定 fi 对象的值和符号性时,该对象具有最佳精度小数长度,其字长默认为 16 位。
a = fi(pi,0)
a =
3.1416
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 16
FractionLength: 14
创建一个有符号 fi 对象,其字长为 8 位,具有最佳精度小数长度。在此示例中,a 的小数长度为 5,因为当数据类型有符号时,值的整数部分需要三位来表示。
a = fi(pi,1,8)
a =
3.1562
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 5
如果 fi 对象无符号,则只需两位来表示整数部分,剩下六位小数位。
b = fi(pi,0,8)
b =
3.1406
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 8
FractionLength: 6
创建一个有符号 fi 对象,其值为 pi,字长为 8 位,小数长度为 3 位。
a = fi(pi,1,8,3)
a =
3.1250
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 3
创建一个 fi 对象数组,其字长为 16 位,小数长度为 12 位。
a = fi((magic(3)/10),1,16,12)
a =
0.8000 0.1001 0.6001
0.3000 0.5000 0.7000
0.3999 0.8999 0.2000
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 12
斜率和偏置定标数的真实值由下式表示:
.
要创建具有斜率和偏置定标的 fi 对象,请在构造函数中的字长后包含 slope 和 bias 参量。例如,创建一个斜率为 3、偏置为 2 的 fi 对象。
a = fi(pi,1,16,3,2)
a =
2
DataTypeMode: Fixed-point: slope and bias scaling
Signedness: Signed
WordLength: 16
Slope: 3
Bias: 2
fi 对象 a 的 DataTypeMode 属性是 Fixed-point: slope and bias scaling。
您也可以指定斜率调整因子和固定指数,其中
.
例如,创建一个 fi 对象,其斜率调整因子为 1.5,固定指数为 1,偏置为 2。
a = fi(pi,1,16,1.5,1,2)
a =
2
DataTypeMode: Fixed-point: slope and bias scaling
Signedness: Signed
WordLength: 16
Slope: 3
Bias: 2
numerictype 对象包含 fi 对象的所有数据类型信息。numerictype 属性也是 fi 对象的属性。
通过在 fi 构造函数中指定 numerictype 对象,可以创建使用现有 numerictype 对象的所有属性的 fi 对象。
T = numerictype(0,24,16)
T =
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 24
FractionLength: 16
a = fi(pi,T)
a =
3.1416
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 24
FractionLength: 16
fi 对象的算术属性由关联到该 fi 对象的 fimath 对象定义。
创建一个 fimath 对象,并指定 OverflowAction、RoundingMethod 和 ProductMode 属性。
F = fimath('OverflowAction','Wrap',... 'RoundingMethod','Floor',... 'ProductMode','KeepMSB')
F =
RoundingMethod: Floor
OverflowAction: Wrap
ProductMode: KeepMSB
ProductWordLength: 32
SumMode: FullPrecision
创建一个 fi 对象,并在构造函数中指定 fimath 对象 F。
a = fi(pi,F)
a =
3.1415
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 13
RoundingMethod: Floor
OverflowAction: Wrap
ProductMode: KeepMSB
ProductWordLength: 32
SumMode: FullPrecision
使用 removefimath 函数删除关联的 fimath 对象,并将数学设置还原为其默认值。
a = removefimath(a)
a =
3.1415
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 13
当 fi 对象的输入参量 v 不是双精度值并且您没有指定字长或小数长度属性时,返回的 fi 对象保留输入的数值类型。
基于内置整数创建 fi 对象
当输入是内置整数时,定点属性与整数类型的属性匹配。
v1 = uint32(5); a1 = fi(v1)
a1 =
5
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 32
FractionLength: 0
v2 = int8(5); a2 = fi(v2)
a2 =
5
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 0
基于 fi 对象创建 fi 对象
当输入值是 fi 对象时,输出使用与输入 fi 对象相同的字长、小数长度和符号性。
v = fi(pi,1,24,12); a = fi(v)
a =
3.1416
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 24
FractionLength: 12
基于逻辑值创建 fi 对象
当输入值为逻辑值时,输出 fi 对象的 DataTypeMode 属性为 Boolean。
v = true; a = fi(v)
a =
1
DataTypeMode: Boolean
基于单精度值创建 fi 对象
当输入值为单精度值时,输出的 DataTypeMode 属性为 Single。
v = single(pi); a = fi(v)
a =
3.1416
DataTypeMode: Single
您可以在创建 fi 对象的过程中设置 fimath 属性,如舍入和溢出模式。
a = fi(pi,'RoundingMethod','Floor',... 'OverflowAction','Wrap')
a =
3.1415
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 13
RoundingMethod: Floor
OverflowAction: Wrap
ProductMode: FullPrecision
SumMode: FullPrecision
RoundingMethod 和 OverflowAction 属性是 fimath 对象的属性。在 fi 构造函数中指定这些属性会将局部 fimath 对象与 fi 对象相关联。
使用 removefimath 函数删除局部 fimath,并将数学属性设置回其默认值。
a = removefimath(a)
a =
3.1415
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 13
创建未设置属性的 fi 对象将使用默认 RoundingMethod 和 OverflowAction,而不考虑任何 globalfimath 设置。
要观察此行为,请指定 globalfimath。
resetglobalfimath globalfimath('RoundingMethod','floor','OverflowAction','wrap')
ans =
RoundingMethod: Floor
OverflowAction: Wrap
ProductMode: FullPrecision
SumMode: FullPrecision
在构造函数中构造没有 fimath 设置的 fi 对象。
b = fi([3.6 128],1,8,0)
b =
4 127
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 0
生成的 b 值使用 Nearest 舍入和 Saturate 作为溢出操作。如果此行为不适合您的应用,请参阅 fi Constructor Does Not Follow globalfimath Rules寻找解决方法。
重置 globalfimath 以还原默认值。
resetglobalfimath;
此示例说明如何使用 fipref 对象的 DataTypeOverride 设置用双精度、单精度或定标双精度来覆盖 fi 对象。fipref 对象定义所有 fi 对象的显示和记录属性。
保存当前 fipref 设置,以便以后还原。
fp = fipref; initialDTO = fp.DataTypeOverride;
使用默认设置和原始 fipref 设置创建一个 fi 对象。
a = fi(pi)
a =
3.1416
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 13
使用 fipref 对象将数据类型覆盖设置为双精度。
fipref('DataTypeOverride','TrueDoubles')
ans =
NumberDisplay: 'RealWorldValue'
NumericTypeDisplay: 'full'
FimathDisplay: 'full'
LoggingMode: 'Off'
DataTypeOverride: 'TrueDoubles'
DataTypeOverrideAppliesTo: 'AllNumericTypes'
创建一个新的 fi 对象,而不指定其 DataTypeOverride 属性,以便它使用通过 fipref 指定的数据类型覆盖设置。
a = fi(pi)
a =
3.1416
DataTypeMode: Double
创建另一个 fi 对象,并将其 DataTypeOverride 设置设为 off,以便它忽略 fipref 对象的数据类型覆盖设置。
b = fi(pi,'DataTypeOverride','Off')
b =
3.1416
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 13
还原示例开始时保存的 fipref 设置。
fp.DataTypeOverride = initialDTO;
要将非数值 -Inf、Inf 和 NaN 用作 fi 表示的定点值,必须完全指定定点对象的数值类型。这些值不支持自动最佳精度定标。
溢出时饱和
当 fi 对象的数值类型指定为溢出时饱和时,则 Inf 映射到指定数值类型的最大可表示值,而 -Inf 映射到最小可表示值。NaN 映射到零。
x = [-inf nan inf]; a = fi(x,1,8,0,'OverflowAction','Saturate') b = fi(x,0,8,0,'OverflowAction','Saturate')
a =
-128 0 127
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 0
RoundingMethod: Nearest
OverflowAction: Saturate
ProductMode: FullPrecision
SumMode: FullPrecision
b =
0 0 255
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 8
FractionLength: 0
RoundingMethod: Nearest
OverflowAction: Saturate
ProductMode: FullPrecision
SumMode: FullPrecision
溢出时绕回
当 fi 对象的数值类型指定为溢出时绕回时,则 -Inf、Inf 和 NaN 映射到零。
x = [-inf nan inf]; a = fi(x,1,8,0,'OverflowAction','Wrap') b = fi(x,0,8,0,'OverflowAction','Wrap')
a =
0 0 0
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 0
RoundingMethod: Nearest
OverflowAction: Wrap
ProductMode: FullPrecision
SumMode: FullPrecision
b =
0 0 0
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 8
FractionLength: 0
RoundingMethod: Nearest
OverflowAction: Wrap
ProductMode: FullPrecision
SumMode: FullPrecision
提示
使用
fipref对象控制fi对象的显示、记录和数据类型覆盖预设项。
扩展功能
使用说明和限制:
不支持没有任何输入参量的默认构造函数语法。
如果没有完全指定
numerictype,则fi的输入必须为常量、fi、单精度值或内置整数值。如果输入是内置双精度值,它必须为常量。这种限制允许fi根据输入的已知数据类型自动定标其小数长度。对于代码生成,所有与数据类型相关的属性都必须为常量。
非定点 Simulink® 必须有输入
numerictype对象信息。
HDL 代码生成
使用 HDL Coder™ 为 FPGA 和 ASIC 设计生成 VHDL、Verilog 和 SystemVerilog 代码。
版本历史记录
在 R2006a 之前推出在以前的版本中,对象 fi、fimath 和 numerictype 的不精确的属性名称会导致警告。在 R2021a 中,不再支持不精确的属性名称。请改用精确的属性名称。
在以前的版本中,当传递非有限输入值 -Inf、Inf 或 NaN 时,fi 会返回错误。fi 现在处理这些输入的方式与 MATLAB® 和 Simulink 处理整数数据类型的 -Inf、Inf 和 NaN 的方法相同。
当 fi 指定为定点数值类型时,
NaN映射到0。当
fi对象的'OverflowAction'属性设置为'Wrap'时,-Inf和Inf映射到0。当
fi对象的'OverflowAction'属性设置为'Saturate'时,Inf映射到最大的可表示值,而-Inf映射到最小的可表示值。
有关此行为的示例,请参阅fi 针对 -Inf、Inf 和 NaN 的行为。
注意
-Inf、Inf 或 NaN 的输入值不支持最佳精度定标。
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)