Main Content

matlab.unittest.constraints.NumericComparator class

Package: matlab.unittest.constraints

Comparator for numeric data types

Construction

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

tolObj

matlab.unittest.constraints.Tolerance instance

Properties

Tolerance

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.

Examples

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:
           1.618033988749895
        Expected Value:
           1.618000000000000

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.
Introduced in R2013a