Main Content

fi

Construct fixed-point numeric object

Description

To assign a fixed-point data type to a number or variable, create afiobject using thefi构造函数。You can specify numeric attributes and math rules in the constructor or by using thenumerictypeandfimathobjects.

克雷亚tion

Description

example

a= fi返回一个signedfiobject with no value, a 16-bit word length, and a 15-bit fraction length.

example

a= fi(v)返回一个signedfiobject with valuev, a 16-bit word length, and best-precision fraction length.

example

a= fi(v,s)返回一个fiobject with valuev, signednesss, a 16-bit word length, and best-precision fraction length.

example

a= fi(v,s,w)返回一个fiobject with valuev, signednesss, and word lengthw.

example

a= fi(v,s,w,f)返回一个fiobject with valuev, signednesss, word lengthw, and fraction lengthf.

example

a= fi(v,s,w,slope,bias)返回一个fiobject with valuev, signednesss,slope, andbias.

example

a= fi(v,s,w,slopeadjustmentfactor,fixedexponent,bias)返回一个fiobject with valuev, signednesss,slopeadjustmentfactor,fixedexponent, andbias.

example

a= fi(v,T)返回一个fiobject with valuevandnumerictypeT.

example

a= fi(___,F)返回一个fiobject withfimathF.

example

a= fi(___,Name,Value)返回一个fiobject with property values specified by one or more name-value pair arguments.

Input Arguments

expand all

Value of thefiobject, specified as a scalar, vector, matrix, or multidimensional array.

The value of the returnedfiobject is the value of the inputvquantized to the data type specified in thefi构造函数。When the inputvis a non-double and you do not specify the word length or fraction length, the returnedfiobject 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, andNaNas the value only if you fully specify the numerictype of thefiobject. Whenfiis specified as a fixed-point numerictype,

  • NaNmaps to0.

  • When the'OverflowAction'property of thefiobject is set to'Wrap',-Inf, andInfmap to0.

  • When the'OverflowAction'property of thefiobject is set to'Saturate',Infmaps to the largest representable value, and-Infmaps to the smallest representable value.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|fi

Signedness of thefiobject, 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

Word length in bits of thefiobject, specified as a positive scalar integer.

Thefiobject has a word length limit of 65535 bits.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical

Fraction length in bits of the stored integer value of thefiobject, specified as a scalar integer.

If you do not specify a fraction length, thefiobject 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 of the scaling of thefiobject, specified as a positive scalar.

This equation represents the real-world value of a slope bias scaled number.

r e a l - w o r l d v a l u e = ( s l o p e × i n t e g e r ) + b i a s

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical

Bias of the scaling of thefiobject, specified as a scalar.

This equation represents the real-world value of a slope bias scaled number.

r e a l - w o r l d v a l u e = ( s l o p e × i n t e g e r ) + b i a s

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical

Slope adjustment factor of thefiobject, specified as a scalar greater than or equal to1and less than2.

The following equation demonstrates the relationship between the slope, fixed exponent, and slope adjustment factor.

s l o p e = s l o p e a d j u s t m e n t f a c t o r × 2 f i x e d e x p o n e n t

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical

Fixed exponent of thefiobject, specified as a scalar.

The following equation demonstrates the relationship between the slope, fixed exponent, and slope adjustment factor.

s l o p e = s l o p e a d j u s t m e n t f a c t o r × 2 f i x e d e x p o n e n t

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical

Numeric type properties of thefiobject, specified as anumerictypeobject.

Fixed-point math properties of thefiobject, specified as afimathobject.

Properties

Thefiobject has three types of properties:

You can set these properties when you create afiobject. Use the data properties to access data in afiobject. Thefimathproperties andnumerictypeproperties are, by transitivity, also properties of thefiobject.fimathproperties determine the rules for performing fixed-point arithmetic operations onfiobjects. Thenumerictypeobject contains all the data type and scaling attributes of a fixed-point object.

Examples

collapse all

克雷亚te afiobject using the default constructor. The constructor returns a signedfiobject 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

创建一个签名fiobject 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 unsignedfiobject with a value ofpi. When you specify only the value and the signedness of thefiobject, 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

创建一个签名fiobject with a word length of 8 bits and best-precision fraction length. In this example, the fraction length ofais5because 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 thefiobject 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

创建一个签名fiobject 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 offiobjects 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

The real-world value of a slope and bias scaled number is represented by

real world value = ( slope × integer ) + bias .

To create afiobject that uses slope and bias scaling, include theslopeandbiasarguments after the word length in the constructor. For example, create afiobject with a slope of3and 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

TheDataTypeModeproperty of thefiobjectaisFixed-point: slope and bias scaling.

Alternatively, you can specify the slope adjustment factor and fixed exponent where

s lope = slopeadjustmentfactor × 2 fixedexponent .

For example, create afiobject 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

Anumerictypeobject contains all of the data type information of afiobject.numerictypeproperties are also properties offiobjects.

You can create afiobject that uses all of the properties of an existingnumerictypeobject by specifying thenumerictypeobject 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

The arithmetic attributes of afiobject are defined by afimathobject which is attached to thatfiobject.

克雷亚te afimathobject and specify theOverflowAction,RoundingMethod, andProductModeproperties.

F = fimath('OverflowAction','Wrap',...“RoundingMethod”,'Floor',...'ProductMode','KeepMSB')
F = RoundingMethod: Floor OverflowAction: Wrap ProductMode: KeepMSB ProductWordLength: 32 SumMode: FullPrecision

克雷亚te afiobject and specify thefimathobjectFin 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 theremovefimathfunction to remove the associatedfimathobject 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

When the input argumentvof afiobject is not a double and you do not specify the word length or fraction length properties, the returnedfiobject retains the numeric type of the input.

克雷亚tefiobject 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

克雷亚tefiobject fromfiobject

When the input value is afiobject, the output uses the same word length, fraction length, and signedness as the inputfiobject.

v = fi(pi,1,24,12); a = fi(v)
a = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 24 FractionLength: 12

克雷亚tefiobject from logical

When the input value is a logical, theDataTypeModeproperty of the outputfiobject isBoolean.

v = true; a = fi(v)
a = 1 DataTypeMode: Boolean

克雷亚tefiobject from single

When the input value is single, theDataTypeModeproperty of the output isSingle.

v = single(pi); a = fi(v)
a = 3.1416 DataTypeMode: Single

You can setfimathproperties, such as rounding and overflow modes during the creation of thefiobject.

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

TheRoundingMethodandOverflowActionproperties are properties of thefimathobject. Specifying these properties in theficonstructor associates a localfimathobject with thefiobject.

Use theremovefimathfunction to remove the localfimathand 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

This examples shows how to use theDataTypeOverridesetting of thefiprefobject to overridefiobjects with doubles, singles, or scaled doubles. Thefiprefobject defines the display and logging attributes for allfiobjects.

Save the currentfiprefsettings to restore later.

fp = fipref; initialDTO = fp.DataTypeOverride;

克雷亚te afiobject with the default settings and originalfiprefsettings.

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 newfiobject without specifying itsDataTypeOverrideproperty so that it uses the data type override settings specified usingfipref.

a = fi(pi)
a = 3.1416 DataTypeMode: Double

克雷亚te anotherfiobject and set itsDataTypeOverridesetting tooffso that it ignores the data type override settings of thefiprefobject.

b = fi(pi,'DataTypeOverride','Off')
b = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13

Restore thefiprefsettings saved at the start of the example.

fp.DataTypeOverride = initialDTO;

To use the non-numeric values-Inf,Inf, andNaNas 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 thefiobject is specified to saturate on overflow, thenInfmaps to the largest representable value of the specified numeric type, and-Infmaps to the smallest representable value.NaNmaps 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 thefiobject is specified to wrap on overflow, then-Inf,Inf, andNaNmap 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 thefiprefobject to control the display, logging, and data type override preferences forfiobjects.

Extended Capabilities

HDL Code Generation
Generate Verilog and VHDL code for FPGA and ASIC designs using HDL Coder™.

Version History

Introduced before R2006a

expand all