Main Content

Sqrt

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

  • Library:
  • Simulink / Math Operations

    HDL Coder / HDL Floating Point Operations

    HDL Coder / Math Operations

  • Sqrt block

Description

TheSqrtblock calculates the square root, signed square root, or reciprocal of square root on the input signal. Select one of the following functions from theFunctionparameter list.

Function Description Mathematical Expression MATLAB®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 icon changes to match the function.

Ports

Input

expand all

Input signal to the block to calculate the square root, signed square root, or reciprocal of square root. Thesqrtfunction accepts real or complex inputs, except for complex fixed-point signals.signedSqrtandrSqrtdo not accept complex inputs. The input signal must be a floating point number.

This table summarizes the support for complex types and negative values for floating point, integer, and fixed-point data types forsqrt,rSqrt, andsignedSqrtfunctions.

Function Data Type Complex Negative Values
Input Output
sqrt Floating point Yes Yes Yes
Integer and fixed-point No No No

rSqrt

Floating point No No Yes
Integer and fixed-point No No No
signedSqrt Floating point No Yes Yes
Integer and fixed-point No No No

If the input is negative, set theOutput signalto complex for all functions exceptsignedSqrt.

Data Types:single|double|half|int8|int16|int32|int64|uint8|uint16|uint32|uint64|fixed point

Output

expand all

Output signal that is the square root, signed square root, or reciprocal of square root of the input signal. When the input is an integer or fixed-point type, the output must be floating point.

Data Types:single|double|half|int8|int16|int32|int64|uint8|uint16|uint32|uint64|fixed point

Parameters

expand all

Main

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

Function Block Icon
sqrt
signedSqrt
rSqrt

依赖ency

When this parameter is set tosignedSqrt, theIntermediate results data typeparameter is disabled.

Programmatic Use

Block Parameter:Operator
Type: character vector
Values:'sqrt'|'signedSqrt'|'rSqrt'
Default:'sqrt'

Specify the output signal type of the block.

Function Input Signal Type Output Signal Type
Auto Real Complex

sqrt

real

realfor nonnegative inputs

NaNfor negative inputs

realfor nonnegative inputs

NaNfor 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

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, seeSpecify Sample Time.

依赖encies

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

Programmatic Use

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

Algorithm

Specify the method for computing the reciprocal of a square root. This parameter is only valid for therSqrtfunction.

Method Data Types Supported When to Use This Method
Exact

Floating point

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.

TheExactmethod provides results that are consistent with MATLAB computations.

Note

The algorithms forsqrtandsignedSqrtare always ofExacttype, no matter what selection appears on the block dialog box.

Programmatic Use

Block Parameter:AlgorithmType
Type: character vector
Values:'Exact'|'Newton-Raphson'
Default:'Exact'

Specify the number of iterations to perform the Newton-Raphson algorithm. This parameter is valid with therSqrtfunction and theNewton-Raphsonvalue forMethod.

Note

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

Programmatic Use

Block Parameter:Iterations
Type: character vector
Values: integer
Default:'3'

Data Types

Click theShow data type assistantbuttonto display theData Type Assistant, which helps you set the data type attributes. For more information, seeSpecify Data Types Using Data Type Assistant.

Specify the data type for intermediate results when you setFunctiontosqrtorrSqrton theMainpane.

可以inhe类型rited, specified directly, or expressed as a data type object such asSimulink.NumericType.

Note

To avoid overflow, the intermediate data type must be larger than or equal to a data type that can contain the square of the output data type.

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

Input and Output Data Types Intermediate Data Type
Input or output is double. Use double.
Input or output is single, and any non-single data type isnotdouble. 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 Types Intermediate 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 setIntermediate results data typeto继承:Inherit from outputwhen:

  • You selectNewton-Raphsonto 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 from outputyields 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 aData Type Conversionblock in front of the Sqrt block to perform the conversion.

依赖encies

This parameter is disabled when theFunctionparameter is set tosignedSqrt.

Programmatic Use

Block Parameter:IntermediateResultsDataTypeStr
Type: character vector
Values:'Inherit: Inherit via internal rule'|'Inherit: Inherit from input'|'Inherit: Inherit from output'|'double'|'single','int8','uint8',int16,'uint16','int32','uint32','int64','uint64',fixdt(1,16,0),fixdt(1,16,2^0,0).''
Default:'Inherit: Inherit via internal rule'

指定输出数据类型。可以inhe类型rited, specified directly, or expressed as a data type object such asSimulink.NumericType.

依赖encies

When input is a floating-point data type smaller than single precision, the继承:Inherit via internal ruleoutput data type depends on the setting of theInherit floating-point output type smaller than single precisionconfiguration parameter. Data types are smaller than single precision when the number of bits needed to encode the data type is less than the 32 bits needed to encode the single-precision data type. For example,halfandint16are smaller than single precision.

Programmatic Use

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

Specify the lower value of the output range that Simulink®checks as a finite, real, double, scalar value.

Note

If you specify a bus object as the data type for this block, do not set the minimum value for bus data on the block. Simulink ignores this setting. Instead, set the minimum values for bus elements of the bus object specified as the data type. For information on the Minimum parameter for a bus element, seeSimulink.BusElement.

Simulink uses the minimum to perform:

Note

Output minimumdoes not saturate or clip the actual output signal. Use theSaturationblock instead.

Programmatic Use

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

Specify the upper value of the output range that Simulink checks as a finite, real, double, scalar value.

Note

If you specify a bus object as the data type for this block, do not set the maximum value for bus data on the block. Simulink ignores this setting. Instead, set the maximum values for bus elements of the bus object specified as the data type. For information on the Maximum parameter for a bus element, seeSimulink.BusElement.

Simulink uses the maximum value to perform:

Note

Output maximumdoes not saturate or clip the actual output signal. Use theSaturationblock instead.

Programmatic Use

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

Specify the rounding mode for fixed-point operations.For more information, seeRounding(Fixed-Point Designer).

Programmatic Use

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

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, seeUse Lock Output Data Type Setting(Fixed-Point Designer).

Programmatic Use

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

Action Reasons for Taking This Action What Happens for Overflows Example

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 theint8(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, seeTroubleshoot Signal Range Errors.

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

The maximum value that theint8(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 asint8, which can produce an unintended result. For example, a block result of 130 (binary 1000 0010) expressed asint8, 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.

Programmatic Use

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

Block Characteristics

Data Types

double|fixed point|half|integer|single

Direct Feedthrough

yes

多维信号

yes

Variable-Size Signals

yes

Zero-Crossing Detection

no

Extended Capabilities

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

PLC Code Generation
Generate Structured Text code using Simulink® PLC Coder™.

Fixed-Point Conversion
Design and simulate fixed-point systems using Fixed-Point Designer™.

Introduced in R2010a