# matlab.unittest.constraints.RelativeTolerance 类

## 描述

`matlab.unittest.constraints.RelativeTolerance` 类为数值数组 `actual``expected` 的比较提供相对数值容差。该容差检查数组间差的模相对于 `expected` 数组的情况。为了满足值为 `RelTol` 的相对容差，`abs(expected-actual) <= RelTol.*abs(expected)` 必须为 `true`

## 创建对象

### 描述

````t = matlab.unittest.constraints.RelativeTolerance(value1,...,valueN)` 使用指定的容差值创建相对容差。例如，`t = matlab.unittest.constraints.RelativeTolerance(10*eps("single"),0.1)` 为比较一对单精度或双精度数值数组创建相对容差： 比较单精度数值数组时，应用 `10*eps("single")` 的容差值。比较双精度数值数组时，应用 `0.1` 的容差值。 容差值的数据类型是容差支持的唯一数据类型。如果实际数组和预期数组包含不同数据类型的值，则容差仅适用于其数据类型受容差支持的值。通过将数值容差与 `&` 和 `|` 运算符组合，可以为特定数值类型指定多个容差值。如果以这种方式为特定数值类型指定多个容差值，这些容差值的大小必须相同或兼容。有关兼容数组的详细信息，请参阅基本运算的兼容数组大小。```

## 属性

#### 属性：

 `GetAccess` `public` `SetAccess` `immutable`

## 示例

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

```testCase = TestCase.forInteractiveUse; ```

```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 ```

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

```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 ```

```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; ```

```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; ```

```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; ```

```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; ```

```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 ```

```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 ```

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