Documentation Center

  • Trials
  • Product Updates

Sqrt, Signed Sqrt, Reciprocal Sqrt

Calculate square root, signed square root, or reciprocal of square root

Library

Math Operations

Description

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

FunctionDescriptionMathematical ExpressionMATLAB® Equivalent
sqrt

Square root of the input

u0.5

sqrt
signedSqrt

Square root of the absolute value of the input, multiplied by the sign of the input

sign(u)*|u|0.5

rSqrt

Reciprocal of the square root of the input

u-0.5

The block output is the result of applying the function to the input. Each function supports:

  • Scalar operations

  • Element-wise vector and matrix operations

Data Type Support

The block accepts input signals of the following data types:

FunctionInput Data TypesRestrictions

sqrt

  • Floating point

  • Built-in integer

  • Fixed point

None

signedSqrt

  • Floating point

  • Built-in integer

  • Fixed point

When the input is an integer or fixed-point type, the output must be floating point.

rSqrt

  • Floating point

  • Built-in integer

  • Fixed point

None

The block accepts real and complex inputs of the following types:

FunctionTypes of Real InputsTypes of Complex Inputs

sqrt

Any, except for fixed-point inputs that are negative or have nontrivial slope and nonzero bias

Any, except for fixed-point inputs

signedSqrt

None

rSqrt

None

The block output:

  • Uses the data type that you specify for Output data type

  • Is real or complex, depending on your selection for Output signal type

Parameters and Dialog Box

The Main pane of the block dialog box appears as follows:

Function

Specify the mathematical function. The block icon changes to match the function you select.

FunctionBlock Icon
sqrt
signedSqrt
rSqrt

Output signal type

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

FunctionInput Signal TypeOutput Signal Type
AutoRealComplex

sqrt

real

real for nonnegative inputs

NaN for negative inputs

real for nonnegative inputs

NaN for negative inputs

complex

complex

complex

error

complex

signedSqrt

real

real

real

complex

complex

error

error

error

rSqrt

real

real

real

error

complex

error

error

error

Sample time (-1 for inherited)

Specify the time interval between samples. To inherit the sample time, set this parameter to -1. See Specify Sample Time in the online documentation for more information.

The Algorithm pane of the block dialog box appears as follows:

    Note:   The parameters in the Algorithm pane are available only when you set Function to rSqrt on the Main pane.

Method

Specify the method for computing the reciprocal of a square root.

MethodData Types SupportedWhen to Use This Method
Exact

Floating point

If you use a fixed-point or built-in integer type, an upcast to a floating-point type occurs.

You do not want an approximation.

    Note:   The input or output must be floating point.

Newton-Raphson

Floating-point, fixed-point, and built-in integer types

You want a fast, approximate calculation.

The Exact method provides results that are consistent with MATLAB computations.

    Note:   The algorithms for sqrt and signedSqrt are always of Exact type, no matter what selection appears on the block dialog box.

Number of iterations

Specify the number of iterations to perform the Newton-Raphson algorithm. The default value is 3.

This parameter is not available when you select Exact for Method.

    Note:   If you enter 0, the block output is the initial guess of the Newton-Raphson algorithm.

The Data Types pane of the block dialog box appears as follows:

Intermediate results data type

Specify the data type for intermediate results (available only when you set Function to sqrt or rSqrt on the Main pane). You can set the data type to:

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

  • 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)

Follow these guidelines on setting an intermediate data type explicitly for the square root function, sqrt:

Input and Output Data TypesIntermediate Data Type
Input or output is double.Use double.
Input or output is single, and any non-single data type is not double.Use single or double.
Input and output are fixed point.Use fixed point.

Follow these guidelines on setting an intermediate data type explicitly for the reciprocal square root function, rSqrt:

Input and Output Data TypesIntermediate Data Type
Input is double and output is not single.Use double.
Input is not single and output is double.Use double.
Input and output are fixed point.Use fixed point.

    Caution   Do not set Intermediate results data type to Inherit:Inherit from output when:

    • You select Newton-Raphson to compute the reciprocal of a square root.

    • The input data type is floating point.

    • The output data type is fixed point.

    Under these conditions, selecting Inherit:Inherit from output yields suboptimal performance and produces an error.

    To avoid this error, convert the input signal from a floating-point to fixed-point data type. For example, insert a Data Type Conversion block in front of the Sqrt block to perform the conversion.

Output data type

Specify the output data type. You can set the data type 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 Show data type assistant button to display the Data Type Assistant, which helps you set the Output data type parameter.

See Specify Block Output Data Types for more information.

Output minimum

Specify the minimum value that the block can output. The default value is [] (unspecified). Simulink® uses this value to perform:

  • Simulation range checking (see Signal Ranges)

  • Automatic scaling of fixed-point data types

Output maximum

Specify the maximum value that the block can output. The default value is [] (unspecified). Simulink uses this value to perform:

  • Simulation range checking (see Signal Ranges)

  • Automatic scaling of fixed-point data types

Lock output data type setting against changes by the fixed-point tools

Select to lock the output data type setting of this block against changes by the Fixed-Point Tool and the Fixed-Point Advisor. For more information, see Use Lock Output Data Type Setting.

Integer rounding mode

Specify the rounding mode for fixed-point operations. For more information, see Rounding. in the Fixed-Point Designer™ documentation.

Saturate on integer overflow
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 Checking 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.

Examples

sqrt Function

Suppose that you have the following modelmodel:

When the input to the Sqrt block is negative and the Output signal type is auto or real, the sqrt function outputs NaN. However, setting Output signal type to complex produces the correct answer.

signedSqrt Function

Suppose that you have the following modelmodel:

When the input to the Sqrt block is negative, the block output is the same for any Output signal type setting. If you change the first Display block format from short to decimal (Stored Integer), you see the value of the imaginary part for the complex output.

rSqrt Function with Floating-Point Inputs

Suppose that you have the following modelmodel:

In the Sqrt block dialog box, assume that the following parameter settings apply:

ParameterSetting
MethodNewton-Raphson
Number of iterations1
Intermediate results data typeInherit:Inherit from input

After one iteration of the Newton-Raphson algorithm, the block output is within 0.0004 of the final value (0.4834).

rSqrt Function with Fixed-Point Inputs

Suppose that you have the following modelmodel:

In the Sqrt block dialog box, assume that the following parameter settings apply:

ParameterSetting
MethodNewton-Raphson
Number of iterations1
Intermediate results data typeInherit:Inherit from input

After one iteration of the Newton-Raphson algorithm, the block output is within 0.0459 of the final value (0.4834).

Characteristics

Direct Feedthrough

Yes

Sample Time

Specified in the Sample time parameter

Dimensionalized

Yes

Multidimensionalized

Yes

Zero-Crossing Detection

No

Was this topic helpful?