Main Content

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.


collapse all

Create a test case for interactive testing.

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

testCase = TestCase.forInteractiveUse;

Use a numeric comparator to test that 1.618 is equal to 1.618.

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

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

testCase.verifyThat((1+sqrt(5))/2, IsEqualTo(1.618, ...
    'Using', NumericComparator))
Verification failed.
    Framework Diagnostic:
    IsEqualTo failed.
    --> NumericComparator failed.
        --> The numeric values are not equal using "isequaln".
        --> Failure table:
                     Actual         Expected           Error               RelativeError    
                ________________    ________    ____________________    ____________________
                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))))
Verification passed.


  • In most cases, you are not required to use a NumericComparator object. The IsEqualTo class creates a constraint to test for equality between data of various types, including numeric values.

    Use a NumericComparator object when you need to override the comparison performed by the IsEqualTo class. For example, if you want the comparison to fail when actual and expected values are nonnumeric, include a NumericComparator object in your test. You also can use NumericComparator to restrict the values contained in structures, cell arrays, and tables. In this example, MATLAB® throws an exception because the actual and expected values are cell arrays.

    import matlab.unittest.constraints.IsEqualTo
    import matlab.unittest.constraints.NumericComparator
    exp = {1,2,3}; 
    act = exp;
    testCase = matlab.unittest.TestCase.forInteractiveUse;

Introduced in R2013a