This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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.


expand 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))
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:
                     Actual         Expected           Error               RelativeError    
                ________________    ________    ____________________    ____________________
                1.61803398874989    1.618       3.39887498947977e-05    2.10066439399244e-05

Actual double:
Expected double:

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.

Introduced in R2013a

Was this topic helpful?