fi
Construct fixed-point numeric object
Description
To assign a fixed-point data type to a number or variable, create afi
object using thefi
构造函数。You can specify numeric attributes and math rules in the constructor or by using thenumerictype
andfimath
objects.
克雷亚tion
Syntax
Description
返回一个signeda
= fifi
object with no value, a 16-bit word length, and a 15-bit fraction length.
返回一个signeda
= fi(v
)fi
object with valuev
, a 16-bit word length, and best-precision fraction length.
返回一个a
= fi(v
,s
,w
,slopeadjustmentfactor
,fixedexponent
,bias
)fi
object with valuev
, signednesss
,slopeadjustmentfactor
,fixedexponent
, andbias
.
返回一个a
= fi(v
,T
)fi
object with valuev
andnumerictype
T
.
返回一个a
= fi(___,Name,Value
)fi
object with property values specified by one or more name-value pair arguments.
Input Arguments
v
—Value
scalar|vector|matrix|multidimensional array
Value of thefi
object, specified as a scalar, vector, matrix, or multidimensional array.
The value of the returnedfi
object is the value of the inputv
quantized to the data type specified in thefi
构造函数。When the inputv
is a non-double and you do not specify the word length or fraction length, the returnedfi
object retains the numerictype of the input. For an example, see克雷亚te fi Object From Non-double Value.
You can specify the non-finite values-Inf
,Inf
, andNaN
as the value only if you fully specify the numerictype of thefi
object. Whenfi
is specified as a fixed-point numerictype,
NaN
maps to0
.When the
'OverflowAction'
property of thefi
object is set to'Wrap'
,-Inf
, andInf
map to0
.When the
'OverflowAction'
property of thefi
object is set to'Saturate'
,Inf
maps to the largest representable value, and-Inf
maps to the smallest representable value.
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|logical
|fi
s
—Signedness
true
or1
(default) |false
or0
Signedness of thefi
object, specified as a numeric or logical1
(true
) or0
(false
). A value of1
(true
) indicates a signed data type. A value of0
(false
) indicates an unsigned data type.
Data Types:logical
w
—Word length in bits
16(default) |positive scalar integer
Word length in bits of thefi
object, specified as a positive scalar integer.
Thefi
object has a word length limit of 65535 bits.
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|logical
f
—Fraction length in bits
15(default) |scalar integer
Fraction length in bits of the stored integer value of thefi
object, specified as a scalar integer.
If you do not specify a fraction length, thefi
object automatically uses the fraction length that gives the best precision while avoiding overflow for the specified value, word length, and signedness.
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|logical
slope
—Slope
positive scalar
Slope of the scaling of thefi
object, specified as a positive scalar.
This equation represents the real-world value of a slope bias scaled number.
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|logical
bias
—Bias
scalar
Bias of the scaling of thefi
object, specified as a scalar.
This equation represents the real-world value of a slope bias scaled number.
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|logical
slopeadjustmentfactor
—Slope adjustment factor
scalar greater than or equal to1
and less than2
Slope adjustment factor of thefi
object, specified as a scalar greater than or equal to1
and less than2
.
The following equation demonstrates the relationship between the slope, fixed exponent, and slope adjustment factor.
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|logical
fixedexponent
—Fixed exponent
scalar
Fixed exponent of thefi
object, specified as a scalar.
The following equation demonstrates the relationship between the slope, fixed exponent, and slope adjustment factor.
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|logical
T
—Numeric type properties
numerictype
object
Numeric type properties of thefi
object, specified as anumerictype
object.
F
—Fixed-point math properties
fimath
object
Fixed-point math properties of thefi
object, specified as afimath
object.
Properties
Thefi
object has three types of properties:
You can set these properties when you create afi
object. Use the data properties to access data in afi
object. Thefimath
properties andnumerictype
properties are, by transitivity, also properties of thefi
object.fimath
properties determine the rules for performing fixed-point arithmetic operations onfi
objects. Thenumerictype
object contains all the data type and scaling attributes of a fixed-point object.
Examples
克雷亚tefi
Object
克雷亚te afi
object using the default constructor. The constructor returns a signedfi
object with no value, a 16-bit word length, and a 15-bit fraction length.
a = fi
a = [] DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 15
创建一个签名fi
object with a value ofpi
, a 16-bit word length, and best-precision fraction length. The fraction length is automatically set to achieve the best precision possible without overflow.
a = fi(pi)
a = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13
克雷亚te an unsignedfi
object with a value ofpi
. When you specify only the value and the signedness of thefi
object, the word length defaults to 16 bits with best-precision fraction length.
a = fi(pi,0)
a = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 16 FractionLength: 14
创建一个签名fi
object with a word length of 8 bits and best-precision fraction length. In this example, the fraction length ofa
is5
because three bits are required to represent the integer portion of the value when the data type is signed.
a = fi(pi,1,8)
a = 3.1562 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 5
If thefi
object is unsigned, only two bits are needed to represent the integer portion, leaving six fractional bits.
b = fi(pi,0,8)
b = 3.1406 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 8 FractionLength: 6
创建一个签名fi
object with a value ofpi
, a word length of 8 bits, and a fraction length of 3 bits.
a = fi(pi,1,8,3)
a = 3.1250 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 3
克雷亚te an array offi
objects with 16-bit word length and 12-bit fraction length.
a = fi((magic(3)/10),1,16,12)
一个= 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
克雷亚tefi
Object with Slope and Bias Scaling
The real-world value of a slope and bias scaled number is represented by
.
To create afi
object that uses slope and bias scaling, include theslope
andbias
arguments after the word length in the constructor. For example, create afi
object with a slope of3
and a bias of2
.
a = fi(pi,1,16,3,2)
a = 2 DataTypeMode: Fixed-point: slope and bias scaling Signedness: Signed WordLength: 16 Slope: 3 Bias: 2
TheDataTypeMode
property of thefi
objecta
isFixed-point: slope and bias scaling
.
Alternatively, you can specify the slope adjustment factor and fixed exponent where
.
For example, create afi
object with a slope adjustment factor of1.5
, a fixed exponent of1
, and a bias of2
.
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
克雷亚tefi
Object Fromnumerictype
Object
Anumerictype
object contains all of the data type information of afi
object.numerictype
properties are also properties offi
objects.
You can create afi
object that uses all of the properties of an existingnumerictype
object by specifying thenumerictype
object in thefi
构造函数。
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
克雷亚tefi
Object With Associatedfimath
The arithmetic attributes of afi
object are defined by afimath
object which is attached to thatfi
object.
克雷亚te afimath
object and specify theOverflowAction
,RoundingMethod
, andProductMode
properties.
F = fimath('OverflowAction','Wrap',...“RoundingMethod”,'Floor',...'ProductMode','KeepMSB')
F = RoundingMethod: Floor OverflowAction: Wrap ProductMode: KeepMSB ProductWordLength: 32 SumMode: FullPrecision
克雷亚te afi
object and specify thefimath
objectF
in the constructor.
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
Use theremovefimath
function to remove the associatedfimath
object and restore the math settings to their default values.
a = removefimath(a)
a = 3.1415 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13
克雷亚tefi
Object From Non-double Value
When the input argumentv
of afi
object is not a double and you do not specify the word length or fraction length properties, the returnedfi
object retains the numeric type of the input.
克雷亚tefi
object from built-in integer
When the input is a built-in integer, the fixed-point attributes match the attributes of the integer type.
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
克雷亚tefi
object fromfi
object
When the input value is afi
object, the output uses the same word length, fraction length, and signedness as the inputfi
object.
v = fi(pi,1,24,12); a = fi(v)
a = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 24 FractionLength: 12
克雷亚tefi
object from logical
When the input value is a logical, theDataTypeMode
property of the outputfi
object isBoolean
.
v = true; a = fi(v)
a = 1 DataTypeMode: Boolean
克雷亚tefi
object from single
When the input value is single, theDataTypeMode
property of the output isSingle
.
v = single(pi); a = fi(v)
a = 3.1416 DataTypeMode: Single
Specify Rounding and Overflow Modes infi
Object Constructor
You can setfimath
properties, such as rounding and overflow modes during the creation of thefi
object.
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
TheRoundingMethod
andOverflowAction
properties are properties of thefimath
object. Specifying these properties in thefi
constructor associates a localfimath
object with thefi
object.
Use theremovefimath
function to remove the localfimath
and set the math properties back to their default values.
a = removefimath(a)
a = 3.1415 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13
Set Data Type Override onfi
Object
This examples shows how to use theDataTypeOverride
setting of thefipref
object to overridefi
objects with doubles, singles, or scaled doubles. Thefipref
object defines the display and logging attributes for allfi
objects.
Save the currentfipref
settings to restore later.
fp = fipref; initialDTO = fp.DataTypeOverride;
克雷亚te afi
object with the default settings and originalfipref
settings.
a = fi(pi)
a = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13
Use thefipref
对象数据类型覆盖doubles.
fipref('DataTypeOVerride','TrueDoubles')
ans = NumberDisplay: 'RealWorldValue' NumericTypeDisplay: 'full' FimathDisplay: 'full' LoggingMode: 'Off' DataTypeOverride: 'TrueDoubles' DataTypeOverrideAppliesTo: 'AllNumericTypes'
克雷亚te a newfi
object without specifying itsDataTypeOverride
property so that it uses the data type override settings specified usingfipref
.
a = fi(pi)
a = 3.1416 DataTypeMode: Double
克雷亚te anotherfi
object and set itsDataTypeOverride
setting tooff
so that it ignores the data type override settings of thefipref
object.
b = fi(pi,'DataTypeOverride','Off')
b = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13
Restore thefipref
settings saved at the start of the example.
fp.DataTypeOverride = initialDTO;
fi
Behavior for-Inf
,Inf
, andNaN
To use the non-numeric values-Inf
,Inf
, andNaN
as fixed-point values withfi
, you must fully specify the numeric type of the fixed-point object. Automatic best-precision scaling is not supported for these values.
Saturate on Overflow
When the numeric type of thefi
object is specified to saturate on overflow, thenInf
maps to the largest representable value of the specified numeric type, and-Inf
maps to the smallest representable value.NaN
maps to zero.
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
Wrap on Overflow
When the numeric type of thefi
object is specified to wrap on overflow, then-Inf
,Inf
, andNaN
map to zero.
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
Tips
Use the
fipref
object to control the display, logging, and data type override preferences forfi
objects.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
The default constructor syntax without any input arguments is not supported.
If the
numerictype
is not fully specified, the input tofi
must be a constant, afi
, a single, or a built-in integer value. If the input is a built-in double value, it must be a constant. This limitation allowsfi
to autoscale its fraction length based on the known data type of the input.All properties related to data type must be constant for code generation.
numerictype
object information must be available for nonfixed-point Simulink®inputs.
HDL Code Generation
Generate Verilog and VHDL code for FPGA and ASIC designs using HDL Coder™.
Version History
Introduced before R2006aR2021a:Inexact property names forfi
,fimath
, andnumerictype
objects not supported
In previous releases, inexact property names forfi
,fimath
, andnumerictype
objects would result in a warning. In R2021a, support for inexact property names was removed. Use exact property names instead.
R2020b:Change in default behavior offi
for-Inf
,Inf
, andNaN
In previous releases,fi
would return an error when passed the non-finite input values-Inf
,Inf
, orNaN
.fi
now treats these inputs in the same way that MATLAB®and Simulink handle-Inf
,Inf
, andNaN
for integer data types.
Whenfi
is specified as a fixed-point numeric type,
NaN
maps to0
.When the
'OverflowAction'
property of thefi
object is set to'Wrap'
,-Inf
, andInf
map to0
.When the
'OverflowAction'
property of thefi
object is set to'Saturate'
,Inf
maps to the largest representable value, and-Inf
maps to the smallest representable value.
For an example of this behavior, seefi Behavior for -Inf, Inf, and NaN.
Note
Best-precision scaling is not supported for input values of-Inf
,Inf
, orNaN
.
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- 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)