是的,你可以直接從'verifyEqual'
得到你想要的,你只需要指定'AbsTol'
和'RelTol'
。這會給你一個元素OR操作來決定通過/失敗。事情是這樣的:
>> expected = [1e7 1e-7];
>> actual = [1e7+1 1e-5];
>> testCase = matlab.unittest.TestCase.forInteractiveUse;
>> testCase.verifyEqual(actual,expected,'RelTol',1e-3,'AbsTol',1e-3)
Interactive verification passed.
>> testCase.verifyEqual(actual,expected,'AbsTol',1e-3)
Interactive verification failed.
---------------------
Framework Diagnostic:
---------------------
verifyEqual failed.
--> The values are not equal using "isequaln".
--> The error was not within absolute tolerance.
--> Failure table:
Index Actual Expected Error RelativeError AbsoluteTolerance
_____ ________ ________ _____ _____________ _________________
1 10000001 10000000 1 1e-07 0.001
Actual double:
1.0e+07 *
1.000000100000000 0.000000000001000
Expected double:
1.0e+07 *
1.000000000000000 0.000000000000010
>>
>> testCase.verifyEqual(actual,expected,'RelTol',1e-3)
Interactive verification failed.
---------------------
Framework Diagnostic:
---------------------
verifyEqual failed.
--> The values are not equal using "isequaln".
--> The error was not within relative tolerance.
--> Failure table:
Index Actual Expected Error RelativeError RelativeTolerance
_____ ______ ________ _______ _____________ _________________
2 1e-05 1e-07 9.9e-06 99 0.001
Actual double:
1.0e+07 *
1.000000100000000 0.000000000001000
Expected double:
1.0e+07 *
1.000000000000000 0.000000000000010
注意verifyEqual的默認操作,當兩個'AbsTol'
和'RelTol'
是一樣的以下內容:
>> import matlab.unittest.constraints.IsEqualTo;
>> import matlab.unittest.constraints.AbsoluteTolerance;
>> import matlab.unittest.constraints.RelativeTolerance;
>> testCase.verifyThat(actual, IsEqualTo(expected, ...
'Within', AbsoluteTolerance(abstol) | RelativeTolerance(reltol)));
...但如果你想改變一個AND所以所有的數值必須在兩種公差類型之內,你可以這樣做:
>> testCase.verifyThat(actual, IsEqualTo(expected, ...
'Within', AbsoluteTolerance(abstol) & RelativeTolerance(reltol)));
最後,還有一件事可能是helpf ul是指公差可以指定爲單個標量值,也可以指定爲與要比較的值相同的值。這允許您對每個元素都有不同的公差配置文件。例如,在你的情況,你可以做的絕對誤差10的第一要素和1E-3爲第二,它會用一個單一的容差傳遞:
>> testCase.verifyEqual(actual,expected,'AbsTol',[10, 1e-3])
Interactive verification passed.
大獎金學習'TestCase.forInteractiveUse'存在的! –