matlab.unittest.constraints.NumericComparator class

Package: matlab.unittest.constraints

Comparator for numeric data types


NumericComparator creates a comparator for numeric data types. The comparator is satisfied if inputs are of the same class with equivalent size, complexity, and sparsity and the built-in isequaln function returns true.

NumericComparator('Within',tolObj) creates a comparator using a specified tolerance. In this case, NumericComparator first checks for equivalent class, size, and sparsity of the actual and expected values. If these checks fail, the comparator is not satisfied. If these checks pass and the isequaln or complexity check fails, NumericComparator delegates comparison to the supplied tolerance, tolObj.

Input Arguments


matlab.unittest.constraints.Tolerance instance



Specific tolerance used in construction of the comparator, specified as a Tolerance object in the tolObj input argument.

Copy Semantics

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


expand all

Compare Numeric Values

Create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.NumericComparator
import matlab.unittest.constraints.IsEqualTo

testCase = TestCase.forInteractiveUse;

Test 1.618 is equal to 1.618 using a numeric comparator.

testCase.verifyThat(1.618, IsEqualTo(1.618,...
    'Using', NumericComparator))
Interactive verification passed.

Verify that (1+sqrt(5))/2 is equal to 1.618.

testCase.verifyThat((1+sqrt(5))/2, IsEqualTo(1.618, ...
    'Using', NumericComparator))
Interactive verification failed.

Framework Diagnostic:
IsEqualTo failed.
--> NumericComparator failed.
    --> The values are not equal using "isequaln".
    --> Failure table:
                Index         Actual         Expected           Error               RelativeError    
                _____    ________________    ________    ____________________    ____________________
                1        1.61803398874989    1.618       3.39887498947977e-05    2.10066439399244e-05

Actual Value:
Expected Value:

Retest using a relative tolerance of 0.25%.

import matlab.unittest.constraints.RelativeTolerance

testCase.verifyThat((1+sqrt(5))/2, IsEqualTo(1.618, ...
    'Using', NumericComparator('Within', RelativeTolerance(0.0025))))
Interactive verification passed.
Was this topic helpful?