matlab.unittest.constraints.AbsoluteTolerance 类
命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.Tolerance
绝对值容差
描述
matlab.unittest.constraints.AbsoluteTolerance
类为 actual
和 expected
数值数组的比较提供绝对值容差。该容差检查数组之间差异的模。要满足值为 AbsTol
的绝对容差,abs(expected-actual) <= AbsTol
必须为 true
。
当您指定用于比较数值数组的容差时,测试框架首先检查 actual
和 expected
数组的类、大小和稀疏性是否相同。如果其中任一检查失败,则认为数组不相等。如果检查都通过,但数组的复/实性不同,或如果 isequaln
函数发现它们不同,则框架将比较委托给容差。
创建对象
描述
t = matlab.unittest.constraints.AbsoluteTolerance(
使用指定的容差值创建绝对容差。例如,value1,...,valueN
)t = matlab.unittest.constraints.AbsoluteTolerance(10*eps("single"),0.1,int8(1))
创建一个绝对容差,用于比较一对单精度数值数组、双精度数值数组或 int8
类型的数值数组:
比较单精度数值数组时,应用
10*eps("single")
的容差值。比较双精度数值数组时,应用
0.1
的容差值。比较
int8
类型的数值数组时,应用int8(1)
的容差值。
容差值的数据类型是容差支持的唯一数据类型。如果实际数组和预期数组包含不同数据类型的值,则容差仅适用于其数据类型受容差支持的值。
通过将数值容差与 &
和 |
运算符组合,可以为特定数值类型指定多个容差值。如果以这种方式为特定数值类型指定多个容差值,这些容差值的大小必须相同或兼容。有关兼容数组的详细信息,请参阅基本运算的兼容数组大小。
属性
容差值,以数值数组组成的元胞数组形式返回。元胞数组的每个元素对应于在容差创建期间指定的容差值之一。
此属性由 value1,...,valueN
输入参量设置。
属性:
GetAccess | public |
SetAccess | immutable |
示例
使用 AbsoluteTolerance
类比较实际值和预期值。
首先,导入此示例中使用的类。
import matlab.unittest.TestCase import matlab.unittest.constraints.IsEqualTo import matlab.unittest.constraints.AbsoluteTolerance
创建一个供交互测试的测试用例。
testCase = TestCase.forInteractiveUse;
测试值 4.1
和 4.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\CompareValuesUsingAbsoluteToleranceExample.m (CompareValuesUsingAbsoluteToleranceExample) at 16
重复测试,以指定绝对容差。验证这些值在 0.5
的容差范围内是否相等。测试通过。
testCase.verifyThat(4.1,IsEqualTo(4.5, ... "Within",AbsoluteTolerance(0.5)))
Verification passed.
使用容差值 3
测试两个元胞数组是否相等。测试失败,因为容差仅适用于 double
类型的元素。
actual = {'abc',123,single(106),int8([1 2 3])}; expected = {'abc',122,single(105),int8([2 4 6])}; testCase.verifyThat(actual,IsEqualTo(expected, ... "Within",AbsoluteTolerance(3)))
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×4 cell array {'abc'} {[123]} {[106]} {[1 2 3]} Expected Value: 1×4 cell array {'abc'} {[122]} {[105]} {[2 4 6]} ------------------ Stack Information: ------------------ In C:\work\CompareValuesUsingAbsoluteToleranceExample.m (CompareValuesUsingAbsoluteToleranceExample) at 28
创建支持不同数值类型的绝对容差。指定容差值 3
、single(1)
和 int8([1 2 4])
,分别用于比较 double
、single
和 int8
类型的元胞数组元素。如果使用此容差比较元胞数组,则测试通过。
tol = AbsoluteTolerance(3,single(1),int8([1 2 4])); 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.14
与 pi
进行比较。测试失败。
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
组合容差,以便在比较结构体中包含的值时,当值接近于零时应用绝对容差,当值大得多时应用相对容差。
首先,导入此示例中使用的类。
import matlab.unittest.TestCase import matlab.unittest.constraints.IsEqualTo import matlab.unittest.constraints.AbsoluteTolerance import matlab.unittest.constraints.RelativeTolerance
创建一个供交互测试的测试用例。
testCase = TestCase.forInteractiveUse;
创建两个结构体,其中包含以 SI 单位表示的真空电磁属性的值。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;
测试对应的逼近值和基线值之间的相对差是否在 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
测试对应的逼近值和基线值之间的绝对差是否在 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
内。在这种情况下,接近于零的渗透率满足绝对容差,而大很多的光速值则满足相对误差。
testCase.verifyThat(approximate,IsEqualTo(baseline, ... "Within",RelativeTolerance(eps*1.0000e+11) | ... AbsoluteTolerance(1.0000e-04)))
Verification passed.
版本历史记录
在 R2013a 中推出
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)