Main Content

matlab.unittest.constraints.RelativeTolerance 클래스

네임스페이스: matlab.unittest.constraints
슈퍼클래스: matlab.unittest.constraints.Tolerance

상대 수치 허용오차

설명

matlab.unittest.constraints.RelativeTolerance 클래스는 actualexpected 숫자형 배열을 비교하기 위한 상대 수치 허용오차를 제공합니다. 허용오차는 expected 배열을 기준으로 두 배열 간의 차이의 크기를 확인합니다. 상대 허용오차 값 RelTol을 만족하려면 abs(expected-actual) <= RelTol.*abs(expected)true여야 합니다.

숫자형 배열 비교를 위한 허용오차를 지정하면 테스트 프레임워크에서 먼저 actual 배열과 expected 배열 간에 클래스, 크기, 희소성이 동일한지 확인합니다. 이러한 확인 중 하나라도 실패하는 경우 배열은 같다고 간주되지 않습니다. 확인 결과가 통과이지만 배열의 실수/복소수 여부가 동일하지 않거나 isequaln 함수에서 이 둘 사이의 차이를 찾으면 프레임워크는 비교를 허용오차에 위임합니다.

생성

설명

예제

t = matlab.unittest.constraints.RelativeTolerance(value1,...,valueN)은 지정된 허용오차 값을 사용하여 상대 허용오차를 만듭니다. 예를 들어, t = matlab.unittest.constraints.RelativeTolerance(10*eps("single"),0.1)은 단정밀도 숫자형 배열의 쌍 또는 배정밀도 숫자형 배열의 쌍을 비교하기 위한 상대 허용오차를 만듭니다.

  • 단정밀도 숫자형 배열을 비교할 때는 허용오차 값 10*eps("single")이 적용됩니다.

  • 배정밀도 숫자형 배열을 비교할 때는 허용오차 값 0.1이 적용됩니다.

허용오차 값의 데이터형이 허용오차가 지원하는 유일한 데이터형입니다. 실제 배열과 예상 배열에 서로 다른 데이터형의 값이 포함된 경우 허용오차는 허용오차가 지원하는 데이터 유형의 값에만 적용됩니다.

숫자형 허용오차를 &| 연산자와 결합하여 특정 숫자형에 대한 허용오차 값을 두 개 이상 지정할 수 있습니다. 이러한 방식으로 특정 숫자형에 대해 허용오차 값을 여러 개 지정할 경우 이러한 허용오차 값들은 크기가 동일하거나 서로 호환되어야 합니다. 호환되는 배열에 대한 자세한 내용은 기본 연산에 대해 호환되는 배열 크기 항목을 참조하십시오.

입력 인수

모두 확장

허용오차 값으로, 쉼표로 구분된 부동소수점 배열의 목록으로 지정됩니다. MATLAB®에서 부동소수점 데이터형에는 single형과 double형(그리고 single형과 double형의 서브클래스)이 있습니다.

지정된 허용오차 값은 고유한 데이터형을 가져야 합니다. 허용오차 값이 비 스칼라인 경우 각각의 차원의 길이는 1이거나 예상 숫자형 배열의 대응하는 차원 길이와 같아야 합니다.

이 인수는 Values 속성을 설정합니다.

예: 0.1

예: [1e-2 1-e4]

예: 10*eps("single"),1e-2

속성

모두 확장

허용오차 값으로, 부동소수점 배열로 구성된 셀형 배열로 반환됩니다. 셀형 배열의 각 요소는 허용오차를 생성하는 동안 지정된 허용오차 값 중 하나에 대응됩니다.

이 속성은 value1,...,valueN 입력 인수에 의해 설정됩니다.

특성:

GetAccess
public
SetAccess
immutable

예제

모두 축소

RelativeTolerance 클래스로 실제 값과 예상 값을 비교합니다.

먼저 이 예제에서 사용되는 클래스를 가져옵니다.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.RelativeTolerance

대화형 방식 테스트를 위한 테스트 케이스를 생성합니다.

testCase = TestCase.forInteractiveUse;

4.14.5가 같은지 테스트합니다. 테스트가 실패합니다.

testCase.verifyThat(4.1,IsEqualTo(4.5))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> NumericComparator failed.
        --> The numeric values are not equal using "isequaln".
        --> Failure table:
                Actual    Expected    Error      RelativeError   
                ______    ________    _____    __________________
                                                                 
                 4.1        4.5       -0.4     -0.088888888888889
        
        Actual Value:
           4.100000000000000
        Expected Value:
           4.500000000000000
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareValuesUsingRelativeToleranceExample.m (CompareValuesUsingRelativeToleranceExample) at 16

상대 허용오차를 지정하여 테스트를 반복합니다. 값이 10% 내에서 같은지 확인합니다. 테스트가 통과합니다.

testCase.verifyThat(4.1,IsEqualTo(4.5, ...
    "Within",RelativeTolerance(0.1)))
Verification passed.

두 셀형 배열이 2% 내에서 같은지 테스트합니다. 허용오차가 double형 요소에만 적용되기 때문에 테스트가 실패합니다.

actual = {'abc',123,single(106)};
expected = {'abc',122,single(105)};
testCase.verifyThat(actual,IsEqualTo(expected, ...
    "Within",RelativeTolerance(0.02)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> Path to failure: <Value>{3}
        --> NumericComparator failed.
            --> The numeric values are not equal using "isequaln".
            --> The tolerance was ignored. The tolerance as specified does not support comparisons of single values.
            --> Failure table:
                    Actual    Expected    Error    RelativeError
                    ______    ________    _____    _____________
                                                                
                     106        105         1       0.00952381  
            
            Actual Value:
              single
            
               106
            Expected Value:
              single
            
               105
    
    Actual Value:
      1×3 cell array
    
        {'abc'}    {[123]}    {[106]}
    Expected Value:
      1×3 cell array
    
        {'abc'}    {[122]}    {[105]}
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareValuesUsingRelativeToleranceExample.m (CompareValuesUsingRelativeToleranceExample) at 27

여러 다른 숫자형을 지원하는 상대 허용오차를 만듭니다. double형과 single형의 셀형 배열 요소를 각각 비교하기 위해 허용오차 값 0.02single(0.02)를 지정합니다. 이 허용오차를 사용하여 셀형 배열을 비교하면 테스트가 통과합니다.

tol = RelativeTolerance(0.02,single(0.02));
testCase.verifyThat(actual,IsEqualTo(expected, ...
    "Within",tol))
Verification passed.

수치 허용오차의 조합을 사용하여 실제 값과 기대 값을 비교합니다. 허용오차를 조합하려면 & 연산자와 | 연산자를 사용합니다.

먼저 이 예제에서 사용되는 클래스를 가져옵니다.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.AbsoluteTolerance
import matlab.unittest.constraints.RelativeTolerance

대화형 방식 테스트를 위한 테스트 케이스를 생성합니다.

testCase = TestCase.forInteractiveUse;

3.14pi를 비교합니다. 테스트가 실패합니다.

testCase.verifyThat(3.14,IsEqualTo(pi))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> NumericComparator failed.
        --> The numeric values are not equal using "isequaln".
        --> Failure table:
                Actual        Expected               Error                RelativeError    
                ______    ________________    ____________________    _____________________
                                                                                           
                 3.14     3.14159265358979    -0.00159265358979299    -0.000506957382897213
        
        Actual Value:
           3.140000000000000
        Expected Value:
           3.141592653589793
    ------------------
    Stack Information:
    ------------------
    In C:\work\CombineAbsoluteAndRelativeTolerancesExample.m (CombineAbsoluteAndRelativeTolerancesExample) at 18

절대 허용오차와 상대 허용오차를 사용하여 값을 비교합니다. 실제 값과 예상 값이 절대 허용오차 내에서, 상대 허용오차 내에서 또는 허용오차 둘 다 내에서 같은지 확인합니다. 테스트가 통과합니다.

tol1 = AbsoluteTolerance(0.001);
tol2 = RelativeTolerance(0.0025);
testCase.verifyThat(3.14,IsEqualTo(pi, ...
    "Within",tol1 | tol2))
Verification passed.

실제 값과 예상 값이 허용오차 둘 다 내에서 같은지 테스트합니다. 절대 허용오차가 충족되지 않기 때문에 이 테스트는 실패합니다.

testCase.verifyThat(3.14,IsEqualTo(pi, ...
    "Within",tol1 & tol2))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> NumericComparator failed.
        --> The numeric values are not equal using "isequaln".
        --> AndTolerance failed.
            --> AbsoluteTolerance failed.
                --> The error was not within absolute tolerance.
            --> RelativeTolerance passed.
                --> The error was within relative tolerance.
            --> Failure table:
                    Actual        Expected               Error                RelativeError        AbsoluteTolerance    RelativeTolerance
                    ______    ________________    ____________________    _____________________    _________________    _________________
                                                                                                                                         
                     3.14     3.14159265358979    -0.00159265358979299    -0.000506957382897213          0.001               0.0025      
        
        Actual Value:
           3.140000000000000
        Expected Value:
           3.141592653589793
    ------------------
    Stack Information:
    ------------------
    In C:\work\CombineAbsoluteAndRelativeTolerancesExample.m (CombineAbsoluteAndRelativeTolerancesExample) at 31

숫자형 배열을 비교하는 방법을 사용자 정의하려면 테스트에 허용오차와 제약 조건을 모두 사용하십시오.

먼저 이 예제에서 사용되는 클래스를 가져옵니다.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.AbsoluteTolerance
import matlab.unittest.constraints.RelativeTolerance

대화형 방식 테스트를 위한 테스트 케이스를 생성합니다.

testCase = TestCase.forInteractiveUse;

IsEqualTo 제약 조건을 사용하여 두 개의 숫자형 벡터를 비교합니다. 테스트가 실패합니다.

exp = [1 100];
act = [1.1 101.1];
testCase.verifyThat(act,IsEqualTo(exp))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> NumericComparator failed.
        --> The numeric values are not equal using "isequaln".
        --> Failure table:
                Index    Actual    Expected         Error            RelativeError   
                _____    ______    ________    ________________    __________________
                                                                                     
                  1      1.1         1         0.1                 0.1               
                  2      101.1       100       1.09999999999999    0.0109999999999999
        
        Actual Value:
           1.0e+02 *
        
           0.011000000000000   1.011000000000000
        Expected Value:
             1   100
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareArraysWithDifferentTolerancesExample.m (CompareArraysWithDifferentTolerancesExample) at 21

절대 허용오차와 상대 허용오차를 사용하여 벡터 간에 요소별로 비교합니다. 대응하는 벡터 요소가 두 허용오차 중 하나를 충족하는지 확인합니다. 테스트가 통과합니다.

absTol = AbsoluteTolerance(1);
relTol = RelativeTolerance(0.02);
testCase.verifyThat(act,IsEqualTo(exp,"Within",absTol | relTol))
Verification passed.

이제 모든 대응하는 요소들이 절대 허용오차를 충족하는지 또는 모두 상대 허용오차를 충족하는지 테스트합니다. 첫 번째 요소는 절대 허용오차만 충족하고 두 번째 요소는 상대 허용오차만 충족하기 때문에 테스트가 실패합니다.

testCase.verifyThat(act, ...
    IsEqualTo(exp,"Within",absTol) | IsEqualTo(exp,"Within",relTol))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    OrConstraint failed.
    --> + [First Condition]:
         |   IsEqualTo failed.
         |   --> NumericComparator failed.
         |       --> The numeric values are not equal using "isequaln".
         |       --> AbsoluteTolerance failed.
         |           --> The error was not within absolute tolerance.
         |           --> Failure table:
         |                   Index    Actual    Expected         Error            RelativeError       AbsoluteTolerance
         |                   _____    ______    ________    ________________    __________________    _________________
         |                                                                                                             
         |                     2      101.1       100       1.09999999999999    0.0109999999999999            1        
         |       
         |       Actual Value:
         |          1.0e+02 *
         |       
         |          0.011000000000000   1.011000000000000
         |       Expected Value:
         |            1   100
    --> OR
        + [Second Condition]:
         |   IsEqualTo failed.
         |   --> NumericComparator failed.
         |       --> The numeric values are not equal using "isequaln".
         |       --> RelativeTolerance failed.
         |           --> The error was not within relative tolerance.
         |           --> Failure table:
         |                   Index    Actual    Expected    Error    RelativeError    RelativeTolerance
         |                   _____    ______    ________    _____    _____________    _________________
         |                                                                                             
         |                     1       1.1         1         0.1          0.1               0.02       
         |       
         |       Actual Value:
         |          1.0e+02 *
         |       
         |          0.011000000000000   1.011000000000000
         |       Expected Value:
         |            1   100
        -+---------------------
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareArraysWithDifferentTolerancesExample.m (CompareArraysWithDifferentTolerancesExample) at 34

허용오차를 조합하여 구조체에 포함된 값을 비교할 때 값이 0에 가까우면 절대 허용오차를 적용하고 값이 훨씬 크면 상대 허용오차를 적용하도록 합니다.

먼저 이 예제에서 사용되는 클래스를 가져옵니다.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.AbsoluteTolerance
import matlab.unittest.constraints.RelativeTolerance

대화형 방식 테스트를 위한 테스트 케이스를 생성합니다.

testCase = TestCase.forInteractiveUse;

SI 단위로 표현되는 진공의 전자기 속성 값을 포함하는 구조체를 2개 만듭니다. approximate 구조체에는 baseline 구조체에 포함된 값에 대한 근삿값이 들어 있습니다.

baseline.LightSpeed = 299792458;
baseline.Permeability = 4*pi*10^-7;
baseline.Permittivity = 1/(baseline.Permeability*baseline.LightSpeed^2);

approximate.LightSpeed = 2.9979e+08;
approximate.Permeability = 1.2566e-06;
approximate.Permittivity = 8.8542e-12;

해당 근삿값과 기준선 값(Baseline Value) 간의 상대 오차가 eps*1.0000e+11 내에 있는지 테스트합니다. 투자율 간의 오차가 작기는 하지만 예상 투자율에 대비해 상대 허용오차를 충족할 만큼 작지는 않습니다.

testCase.verifyThat(approximate,IsEqualTo(baseline, ...
    "Within",RelativeTolerance(eps*1.0000e+11)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> Path to failure: <Value>.Permeability
        --> NumericComparator failed.
            --> The numeric values are not equal using "isequaln".
            --> RelativeTolerance failed.
                --> The error was not within relative tolerance.
                --> Failure table:
                          Actual            Expected                  Error                RelativeError         RelativeTolerance  
                        __________    ____________________    _____________________    _____________________    ____________________
                                                                                                                                    
                        1.2566e-06    1.25663706143592e-06    -3.70614359173257e-11    -2.94925536216295e-05    2.22044604925031e-05
            
            Actual Value:
                 1.256600000000000e-06
            Expected Value:
                 1.256637061435917e-06
    
    Actual Value:
      struct with fields:
    
          LightSpeed: 299790000
        Permeability: 1.256600000000000e-06
        Permittivity: 8.854200000000000e-12
    Expected Value:
      struct with fields:
    
          LightSpeed: 299792458
        Permeability: 1.256637061435917e-06
        Permittivity: 8.854187817620389e-12
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareStructuresThatContainSmallAndLargeValuesExample.m (CompareStructuresThatContainSmallAndLargeValuesExample) at 36

해당 근삿값과 기준선 값(Baseline Value) 간의 절대 오차가 1.0000e-04 내에 있는지 테스트합니다. 광속 간의 오차가 예상 광속에 대비해 작지만 절대 허용오차를 충족하기에는 너무 큽니다.

testCase.verifyThat(approximate,IsEqualTo(baseline, ...
    "Within",AbsoluteTolerance(1.0000e-04)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> Path to failure: <Value>.LightSpeed
        --> NumericComparator failed.
            --> The numeric values are not equal using "isequaln".
            --> AbsoluteTolerance failed.
                --> The error was not within absolute tolerance.
                --> Failure table:
                         Actual      Expected     Error        RelativeError        AbsoluteTolerance
                        _________    _________    _____    _____________________    _________________
                                                                                                     
                        299790000    299792458    -2458    -8.19900545997058e-06         0.0001      
            
            Actual Value:
               299790000
            Expected Value:
               299792458
    
    Actual Value:
      struct with fields:
    
          LightSpeed: 299790000
        Permeability: 1.256600000000000e-06
        Permittivity: 8.854200000000000e-12
    Expected Value:
      struct with fields:
    
          LightSpeed: 299792458
        Permeability: 1.256637061435917e-06
        Permittivity: 8.854187817620389e-12
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareStructuresThatContainSmallAndLargeValuesExample.m (CompareStructuresThatContainSmallAndLargeValuesExample) at 44

이제 허용오차를 조합하여 해당 근삿값과 기준선 값 사이의 절대 오차가 1.0000e-04 내에 있는지 또는 상대 오차가 eps*1.0000e+11 내에 있는지 확인합니다. 이 경우 0에 가까운 투자율은 절대 허용오차를 충족하고 훨씬 큰 광속은 상대 허용오차를 충족합니다.

testCase.verifyThat(approximate,IsEqualTo(baseline, ...
    "Within",RelativeTolerance(eps*1.0000e+11) | ...
    AbsoluteTolerance(1.0000e-04)))
Verification passed.

버전 내역

R2013a에 개발됨