Documentation

# Math Function

Perform mathematical function

• Library:

HDL Coder / Math Operations

## Description

The Math Function block performs numerous common mathematical functions.

### Tip

To perform square root calculations, use the Sqrt block.

You can select one of these functions from the Function parameter list.

FunctionDescriptionMathematical Expression MATLAB® Equivalent
exp

Exponential

eu

exp
log

Natural logarithm

ln u

log
10^u

Power of base 10

10u

10.^u
(see power)

log10

Common (base 10) logarithm

log u

log10
magnitude^2

Complex modulus

|u|2

(abs(u)).^2
(see abs and power)

square

Power 2

u2

u.^2
(see power)

pow

Power

sign(u)*|u|v (default) or uv

power
conj

Complex conjugate

ū

conj
reciprocal

Reciprocal

1/u

1./u
(see rdivide)

hypot

Square root of sum squares

(u2+v2)0.5

hypot
rem

Remainder after division

rem
mod

Modulus after division

mod
transpose

Transpose

uT

u.'
(see Array vs. Matrix Operations (MATLAB))

hermitian

Complex conjugate transpose

uH

u'
(see Array vs. Matrix Operations (MATLAB))

The block output is the result of the operation of the function on the input or inputs. The functions support these types of operations.

FunctionScalar OperationsElement-Wise Vector and Matrix OperationsVector and Matrix Operations
exp

Yes

Yes

log

Yes

Yes

10^u

Yes

Yes

log10

Yes

Yes

magnitude^2

Yes

Yes

square

Yes

Yes

pow

Yes

Yes

conj

Yes

Yes

reciprocal

Yes

Yes

hypot

Yes, on two inputs

Yes, on two inputs (two vectors or two matrices of the same size, a scalar and a vector, or a scalar and a matrix)

rem

Yes, on two inputs

Yes, on two inputs (two vectors or two matrices of the same size, a scalar and a vector, or a scalar and a matrix)

mod

Yes, on two inputs

Yes, on two inputs (two vectors or two matrices of the same size, a scalar and a vector, or a scalar and a matrix)

transpose

Yes

Yes

hermitian

Yes

Yes

The name of the function appears on the block. The appropriate number of input ports appears automatically.

### Tip

Use the Math Function block when you want vector or matrix output.

### Data Type Support

This table shows the input data types that each function of the block can support.

Functionsingledoublebooleanbuilt-in integerfixed point

exp

Yes

Yes

log

Yes

Yes

10^u

Yes

Yes

log10

Yes

Yes

magnitude^2

Yes

Yes

Yes

Yes

square

Yes

Yes

Yes

Yes

pow

Yes

Yes

conj

Yes

Yes

Yes

Yes

reciprocal

Yes

Yes

Yes

Yes

hypot

Yes

Yes

rem

Yes

Yes

Yes

mod

Yes

Yes

Yes

transpose

Yes

Yes

Yes

Yes

Yes

hermitian

Yes

Yes

Yes

Yes

## Ports

### Input

expand all

Input signal specified as a scalar, vector, or matrix. All supported modes accept both real and complex inputs, except for reciprocal, which does not accept complex fixed-point inputs. See Description for more information.

#### Dependencies

Data type support for this block depends on the Function you select and the size of the input(s). For more information, see Data Type Support.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean | fixed point

Input signal specified as a scalar, vector, or matrix. All supported modes accept both real and complex inputs, except for reciprocal, which does not accept complex fixed-point inputs.

#### Dependencies

To enable this port, set Function to hypot, rem, or mod.

Data type support for this block depends on the Function you select, and the size of the input(s). For more information, see Data Type Support.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean | fixed point

### Output

expand all

Output signal specified as a scalar, vector, or matrix. The dimensions of the block output depend on the Function you select and the size of the inputs. The block output is real or complex, depending on what you select for Output signal type. See Description for more information.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean | fixed point

## Parameters

expand all

### Main

#### Dependency

Setting Function to pow enables the Signed power parameter.

#### Programmatic Use

 Block Parameter: Operator Type: character vector Values: 'exp' | 'log' | '10^u' | 'log10' | 'magnitude^2' | 'square' | 'pow' | 'conj' | 'reciprocal' | 'hypot' | 'rem' | 'mod' | 'transpose' | 'hermitian' Default: 'exp'

Take into account sign of the input signal when calculating power, specified as on or off.

• on — Calculate power of the absolute value of the input, multiplied by the sign of the input.

• off — Calculate power of the actual input value. If the first input is negative and the second input is a fractional exponent, return nan.

#### Dependency

Setting Function to pow enables this parameter.

#### Programmatic Use

 Block Parameter: SignedPower Type: character vector Values: 'on' | 'off' | Default: 'on'

Specify the output signal type of the Math Function block as auto, real, or complex.

FunctionInput Signal TypeOutput Signal Type
AutoRealComplex

exp, log, 10u, log10, square, pow, reciprocal, conjugate, transpose, hermitian

real

complex

real

complex

real

error

complex

complex

magnitude squared

real

complex

real

real

real

real

complex

complex

hypot, rem, mod

real

complex

real

error

real

error

complex

error

#### Programmatic Use

 Block Parameter: OutputSignalType Type: character vector Values: 'auto' | 'real' | 'complex' Default: 'auto'

Specify the sample time as a value other than -1. For more information, see Specify Sample Time.

#### Dependencies

This parameter is not visible unless it is explicitly set to a value other than -1. To learn more, see Blocks for Which Sample Time Is Not Recommended.

#### Programmatic Use

 Block Parameter: SampleTime Type: character vector Values: scalar or vector Default: '-1'

### Signal Attributes

Lower value of the output range that Simulink® checks.

Simulink uses the minimum to perform:

### Note

Output minimum does not saturate or clip the actual output signal. Use the Saturation block instead.

#### Programmatic Use

 Block Parameter: OutMin Type: character vector Values: '[ ]'| scalar Default: '[ ]'

Upper value of the output range that Simulink checks.

Simulink uses the maximum value to perform:

### Note

Output maximum does not saturate or clip the actual output signal. Use the Saturation block instead.

#### Programmatic Use

 Block Parameter: OutMax Type: character vector Values: '[ ]'| scalar Default: '[ ]'

Specify the output data type. You can set it to:

• A rule that inherits a data type, for example, Inherit: Inherit via back propagation

• The name of a built-in data type, for example, single

• The name of a data type object, for example, a Simulink.NumericType object

• An expression that evaluates to a data type, for example, fixdt(1,16,0)

Click the button to display the Data Type Assistant, which helps you set the data type attributes. For more information, see Specify Data Types Using Data Type Assistant.

#### Dependencies

To enable this parameter, set the Function to magnitude^2, square, or reciprocal.

#### Programmatic Use

 Block Parameter: OutDataTypeStr Type: character vector Values: 'Inherit: Inherit via internal rule | 'Inherit: Same as first input' | 'Inherit: Inherit via back propagation' | 'double' | 'single' | 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32' | 'int64' | 'uint64' | 'fixdt(1,16)' | 'fixdt(1,16,0)' | 'fixdt(1,16,2^0,0)' | '' Default: 'Inherit: Same as first input'

Select this parameter to prevent the fixed-point tools from overriding the Output data type you specify on the block. For more information, see Use Lock Output Data Type Setting (Fixed-Point Designer).

#### Dependencies

To enable this parameter, set the Function to magnitude^2, square, or reciprocal.

#### Programmatic Use

 Block Parameter: LockScale Type: character vector Values: 'off' | 'on' Default: 'off'

Rounding mode for fixed-point operations. For more information, see Rounding (Fixed-Point Designer).

Block parameters always round to the nearest representable value. To control the rounding of a block parameter, enter an expression using a MATLAB rounding function into the mask field.

#### Dependencies

To enable this parameter, set the Function to magnitude^2, square, or reciprocal.

#### Programmatic Use

 Block Parameter: RndMeth Type: character vector Values: 'Ceiling' | 'Convergent' | 'Floor' | 'Nearest' | 'Round' | 'Simplest' | 'Zero' Default: 'Floor'

ActionReasons for Taking This ActionWhat Happens for OverflowsExample

Select this check box.

Your model has possible overflow, and you want explicit saturation protection in the generated code.

Overflows saturate to either the minimum or maximum value that the data type can represent.

The maximum value that the int8 (signed, 8-bit integer) data type can represent is 127. Any block operation result greater than this maximum value causes overflow of the 8-bit integer. With the check box selected, the block output saturates at 127. Similarly, the block output saturates at a minimum output value of -128.

Do not select this check box.

You want to optimize efficiency of your generated code.

You want to avoid overspecifying how a block handles out-of-range signals. For more information, see Check for Signal Range Errors.

Overflows wrap to the appropriate value that is representable by the data type.

The maximum value that the int8 (signed, 8-bit integer) data type can represent is 127. Any block operation result greater than this maximum value causes overflow of the 8-bit integer. With the check box cleared, the software interprets the overflow-causing value as int8, which can produce an unintended result. For example, a block result of 130 (binary 1000 0010) expressed as int8, is -126.

When you select this check box, saturation applies to every internal operation on the block, not just the output or result. Usually, the code generation process can detect when overflow is not possible. In this case, the code generator does not produce saturation code.

#### Dependencies

To enable this parameter, set the Function to magnitude^2, square, conj, reciprocal, or hermitian.

#### Programmatic Use

 Block Parameter: SaturateOnIntegerOverflow Type: character vector Value: 'off' | 'on' Default: 'on'

## Block Characteristics

 Data Types Boolean | double | fixed point | integer | single Direct Feedthrough no Multidimensional Signals no Variable-Size Signals yes Zero-Crossing Detection no