Accelerating the pace of engineering and science

Documentation Center

• Trial Software

matlab.unittest.constraints.RelativeTolerance class

Package: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.Tolerance

Relative numeric tolerance

Description

This numeric Tolerance assesses the magnitude of the difference between actual and expected values, relative to the expected value. For the tolerance to be satisfied, abs(expVal - actVal) <= relTol.*abs(expVal) must be true.

Construction

RelativeTolerance(tolVals) creates a relative tolerance object which assesses the magnitude of the difference between actual and expected values, relative to the expected value.

The data types of the inputs to the RelativeTolerance constructor determine the data types to which the tolerance is applied. For example, RelativeTolerance(10*eps) constructs a RelativeTolerance for comparing double-precision numeric arrays while RelativeTolerance(int8(2)) constructs a RelativeTolerance for comparing numeric arrays of type int8. If the actual and expected values being compared contain more than one numeric data type, the tolerance only applies to the data types specified by the values passed into the constructor.

Different tolerance values can be specified for different data types by passing multiple tolerance values to the constructor. For example, RelativeTolerance(10*eps, 10*eps('single'), int8(1)) constructs an RelativeTolerance that would apply the following absolute tolerances.

• 10*eps applies a relative tolerance of 10*eps for double-precision numeric arrays.

• 10*eps('single') applies a relative tolerance of 10*eps for single-precision numeric arrays.

• int8(1) applies a relative tolerance of 1 for numeric arrays of type int8.

More than one tolerance can be specified for a particular data type by combining tolerances with the & and | operators. In order to combine two tolerances, the sizes of the tolerance values for each data type must be compatible.

Input Arguments

 tolVals Numeric tolerances, specified as a cell array of numeric arrays. Each input argument contains the tolerance specification for a particular data type. Each numeric array may be a scalar or array of size equal to the actual and expected values.

Properties

 Values Numeric tolerances, specified by the tolVals input argument.

Methods

 and Form logical element-wise conjunction of tolerances or Form logical element-wise disjunction of tolerances

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects in the MATLAB® documentation.

Examples

expand all

Test with Relative Tolerance

```import matlab.unittest.constraints.IsEqualTo;
import matlab.unittest.constraints.RelativeTolerance;

% Create a TestCase for interactive use
testCase = matlab.unittest.TestCase;

% Simple use in IsEqualTo constraint
testCase.verifyThat(4.1, IsEqualTo(4.5, ...
'Within', RelativeTolerance(0.1)));

% Specify different tolerances for different data types
act = {'abc', 123, single(123), int8([1, 2, 3])};
exp = {'abc', 122, single(120), int8([2, 4, 6])};
testCase.verifyThat(act, IsEqualTo(exp, 'Within', ...
RelativeTolerance(2, single(1))));```