setfimath

Attach fimath object to fi object

Description

example

y = setfimath(x,f) returns a fi object, y, with x’s numerictype and value, and attached fimath object, f. This function and the related removefimath function are useful for preventing errors about embedded.fimath of both operands needing to be equal.

The y = setfimath(x,f) syntax does not modify the input, x. To modify x, use x = setfimath(x,f). If you use setfimath in an expression, such as, a*setfimath(b,f), the fimath object is used in the temporary variable, but b is not modified.

Examples

collapse all

Define a fi object, define a fimath object, and use setfimath to attach the fimath object to the fi object.

Create a fi object without a fimath object.

a = fi(pi)
a = 
    3.1416

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

Create a fimath object and attach it to the fi object.

f = fimath('OverflowAction','Wrap','RoundingMethod','Floor');
b = setfimath(a,f)
b = 
    3.1416

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

        RoundingMethod: Floor
        OverflowAction: Wrap
           ProductMode: FullPrecision
               SumMode: FullPrecision

Use the pattern x = setfimath(x,f) and y = removefimath(y) to insulate variables from fimath settings outside the function. This pattern does not create copies of the data in generated code.

function y = fixed_point_32bit_KeepLSB_plus_example(a,b)
   f = fimath('OverflowAction','Wrap',...
      'RoundingMethod','Floor',...
      'SumMode','KeepLSB',...
      'SumWordLength',32);
   a = setfimath(a,f);
   b = setfimath(b,f);
   y = a + b;
   y = removefimath(y);
end
 

If you have the MATLAB® Coder™ product, you can generate C code. This example generates C code on a computer with 32-bit, native integer type.

a = fi(0,1,16,15);
b = fi(0,1,16,15);
codegen -config:lib  fixed_point_32bit_KeepLSB_plus_example...
       -args {a,b} -launchreport
     
int fixed_point_32bit_KeepLSB_plus_example(short a, short b)
{
  return a + b;
}

Input Arguments

collapse all

Input data, specified as a fi object or built-in integer value, from which to copy the data type and value to the output. x must be a fi object or an integer data type (int8, int16, int32, int64, uint8, uint16, uint32, or uint64). Otherwise, the fimath object is not applied. If x is not a fi object or integer data type, y = x.

Input fimath object, specified as an existing fimath object to attach to the output. An error occurs if f is not a fimath object.

Output Arguments

collapse all

Output fi object, returned as a fi object with the same data type and value as the x input. y also has attached fimath object, f. If the input, x, is not a fi object or integer data type, then y = x.

Extended Capabilities

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

See Also

| |

Introduced in R2012b