Main Content

abs

Absolute value of fi object

Description

y = abs(a) returns the absolute value of fi object a with the same numerictype object as a. Intermediate quantities are calculated using the fimath associated with a. The output fi object, y, has the same local fimath as a.

example

y = abs(a,T) returns a fi object with a value equal to the absolute value of a and numerictype object T. Intermediate quantities are calculated using the fimath associated with a and the output fi object y has the same local fimath as a. See Data Type Propagation Rules.

example

y = abs(a,F) returns a fi object with a value equal to the absolute value of a and the same numerictype object as a. Intermediate quantities are calculated using the fimath object F. The output fi object, y, has no local fimath.

example

y = abs(a,T,F) returns a fi object with a value equal to the absolute value of a and the numerictype object T. Intermediate quantities are calculated using the fimath object F. The output fi object, y, has no local fimath. See Data Type Propagation Rules.

example

Examples

collapse all

This example shows the difference between the absolute value results for the most negative value representable by a signed data type when the 'OverflowAction' property is set to 'Saturate' or 'Wrap'.

Calculate the absolute value when the 'OverflowAction' is set to the default value 'Saturate'.

P = fipref('NumericTypeDisplay','full',...
           'FimathDisplay','full');
a = fi(-128)
y = abs(a)
a = 

  -128

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 8

y = 

  127.9961

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 8

abs returns 127.9961, which is a result of saturation to the maximum positive value.

Calculate the absolute value when the 'OverflowAction' is set to 'Wrap'.

a.OverflowAction = 'Wrap'
y = abs(a)
a = 

  -128

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 8

        RoundingMethod: Nearest
        OverflowAction: Wrap
           ProductMode: FullPrecision
               SumMode: FullPrecision

y = 

  -128

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 8

        RoundingMethod: Nearest
        OverflowAction: Wrap
           ProductMode: FullPrecision
               SumMode: FullPrecision

abs returns 128, which is a result of wrapping back to the most negative value.

This example shows the difference between the absolute value results for complex and real fi inputs that have the most negative value representable by a signed data type when the 'OverflowAction' property is set to 'Wrap'.

Define a complex fi object.

re = fi(-1,1,16,15);
im = fi(0,1,16,15);
a = complex(re,im)
a = 

  -1.0000 + 0.0000i

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 15

a is complex, but numerically equal to the real part, re.

Calculate the absolute value of the complex fi object.

y = abs(a,re.numerictype,fimath('OverflowAction','Wrap'))
y = 

    1.0000

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 15

Calculate the absolute value of the real fi object.

y = abs(re,re.numerictype,fimath('OverflowAction','Wrap'))
y = 

    -1

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 15

This example shows how to specify numerictype and fimath objects as optional arguments to control the result of the abs function for real inputs. When you specify a fimath object as an argument, that fimath object is used to compute intermediate quantities, and the resulting fi object has no local fimath.

a = fi(-1,1,6,5,'OverflowAction','Wrap');
y = abs(a)
y = 

    -1

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 6
        FractionLength: 5

        RoundingMethod: Nearest
        OverflowAction: Wrap
           ProductMode: FullPrecision
               SumMode: FullPrecision

The returned output is identical to the input. This may be undesirable because the absolute value is expected to be positive.

F = fimath('OverflowAction','Saturate');
y = abs(a,F)
y = 

    0.9688

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 6
        FractionLength: 5

The returned fi object is saturated to a value of 0.9688 and has the same numerictype object as the input.

Because the output of abs is always expected to be positive, an unsigned numerictype may be specified for the output.

T = numerictype(a.numerictype, 'Signed', false);
y = abs(a,T,F)
y = 

     1

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Unsigned
            WordLength: 6
        FractionLength: 5

Specifying an unsigned numerictype enables better precision.

This example shows how to specify numerictype and fimath objects as optional arguments to control the result of the abs function for complex inputs.

Specify a numerictype input and calculate the absolute value of a.

a = fi(-1-i,1,16,15,'OverflowAction','Wrap');
T = numerictype(a.numerictype,'Signed',false);
y = abs(a,T)
y = 

    1.4142

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Unsigned
            WordLength: 16
        FractionLength: 15

        RoundingMethod: Nearest
        OverflowAction: Wrap
           ProductMode: FullPrecision
               SumMode: FullPrecision

A fi object is returned with a value of 1.4142 and the specified unsigned numerictype. The fimath used for intermediate calculation and the fimath of the output are the same as that of the input.

Now specify a fimath object different from that of a.

F = fimath('OverflowAction','Saturate','SumMode',...
        'KeepLSB','SumWordLength',a.WordLength,...
        'ProductMode','specifyprecision',...
        'ProductWordLength',a.WordLength,...
        'ProductFractionLength',a.FractionLength);
y = abs(a,T,F)
y = 

    1.4142

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Unsigned
            WordLength: 16
        FractionLength: 15

The specified fimath object is used for intermediate calculation. The fimath associated with the output is the default fimath.

Input Arguments

collapse all

Input fi array, specified as a scalar, vector, matrix, or multidimensional array.

abs only supports fi objects with trivial [Slope Bias] scaling, that is, when the bias is 0 and the fractional slope is 1.

abs uses a different algorithm for real and complex inputs. For more information, see Absolute Value.

Data Types: fi
Complex Number Support: Yes

numerictype of the output fi object y, specified as a numerictype object. For more information, see Data Type Propagation Rules.

Example: T = numerictype(0,24,12,'DataType','Fixed')

Fixed-point math settings to use for the calculation of absolute value, specified as a fimath object.

Example: F = fimath('OverflowAction','Saturate','RoundingMethod','Convergent')

Algorithms

collapse all

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced before R2006a

See Also

| |