Main Content

isapprox

Determine approximate equality

Since R2024b

Description

TF = isapprox(A,B) determines which corresponding elements in the two input arrays are approximately equal and returns a logical array. TF contains 1 (true) where the corresponding elements are within the region of approximate equality, and 0 (false) otherwise. The default relative and absolute tolerances are both 1e-15, or if A or B is of type single, 5e-7.

example

TF = isapprox(A,B,tol) determines approximate equality using the relative and absolute tolerances set by the tolerance level tol. Specify tol as "verytight", "tight", "loose", or "veryloose".

example

TF = isapprox(A,B,Name=Value) specifies options using one or more name-value arguments. For example, isapprox(A,B,RelativeTolerance=1e-10) determines approximate equality using the specified relative tolerance and an absolute tolerance of 0.

example

Examples

collapse all

Create two numeric scalar values and compare them for equality. The == operator returns logical 0 (false) because the values differ by a very small amount and are not exactly equal.

A = sin(3/4*pi);
B = 1/sqrt(2);
A == B
ans = logical
   0

To account for round-off error, compare the values for approximate equality. The isapprox function returns logical 1 (true) because the difference between the values of A and B is within the default tolerance.

TF = isapprox(A,B)
TF = logical
   1

A - B
ans = 
1.1102e-16

Compare the value of π in double precision, which has 15 digits after the decimal point, and elements in a numeric vector for approximate equality.

format long
A = pi
A = 
   3.141592653589793

B = [2*asin(1) integral(@(x) 1./sqrt(1-x.^2),-1,1) round(pi,10) 22/7]
B = 1×4

   3.141592653589793   3.141592653589721   3.141592653600000   3.142857142857143

TF = isapprox(A,B)
TF = 1x4 logical array

   1   0   0   0

The isapprox function returns logical 1 (true) for only the first element. The difference between all other elements in B and the value of A is greater than the default tolerance. To accept more noise in the input data, specify the relative and absolute tolerance level as "tight".

TF2 = isapprox(A,B,"tight")
TF2 = 1x4 logical array

   1   1   0   0

The isapprox function returns logical 1 (true) for only the first and second elements. To accept even more noise in the input data, specify the relative and absolute tolerance level as "loose".

TF3 = isapprox(A,B,"loose")
TF3 = 1x4 logical array

   1   1   1   0

The isapprox function returns logical 1 (true) for the first three elements. To accept even more noise in the input data, specify the relative and absolute tolerance level as "veryloose".

TF4 = isapprox(A,B,"veryloose")
TF4 = 1x4 logical array

   1   1   1   1

Compare two sets of measurements from two different instruments for approximate equality. Specify the absolute tolerance as 0.02, which is the known precision of the instruments. If you specify just the absolute tolerance, the relative tolerance is 0.

A = [1.00 2.00 3.10 NaN];
B = [0.99 2.01 3.15 NaN];
TF = isapprox(A,B,AbsoluteTolerance=0.02)
TF = 1x4 logical array

   1   1   0   0

Input Arguments

collapse all

Input data, specified as arrays. A and B must either be the same size or have sizes that are compatible. If the input arrays are complex, then they must be of type single or double.

Specifying isapprox(A,B) is the same as specifying isapprox(B,A).

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Complex Number Support: Yes

Relative and absolute tolerance level, specified as one of the values in this table. For more information, see Region of Approximate Equality for Tolerances.

If you specify tol, you cannot specify the RelativeTolerance or AbsoluteTolerance name-value arguments.

Tolerance Level

Tolerance When Both A and B Are of Any Type Other than single

Tolerance When Either A or B Is of Type single

"verytight"

1e-155e-7

"tight"

1e-121e-6

"loose"

1e-81e-4

"veryloose"

1e-31e-2

Data Types: string | char

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: TF = isapprox(A,B,RelativeTolerance=1e-10)

Relative tolerance, specified as a scalar in the range [0, 1).

The relative tolerance defines the maximum allowed difference between approximately equal elements as a fraction of the element with greater magnitude.

  • If you do not specify the RelativeTolerance or AbsoluteTolerance name-value arguments, the relative tolerance is 1e-15, or if A or B are of type single, 5e-7.

  • If you specify the RelativeTolerance name-value argument and do not specify the AbsoluteTolerance name-value argument, the absolute tolerance is 0.

Example: isapprox(A,B,RelativeTolerance=1e-10) uses a relative tolerance of 1e-10 and an absolute tolerance of 0.

Example: isapprox(A,B,RelativeTolerance=1e-10,AbsoluteTolerance=1e-8) uses a relative tolerance of 1e-10 and an absolute tolerance of 1e-8.

Data Types: double | single

Absolute tolerance, specified as a nonnegative scalar.

The absolute tolerance defines the maximum allowed difference between approximately equal elements as a fixed number.

  • If you do not specify the RelativeTolerance or AbsoluteTolerance name-value arguments, the absolute tolerance is 1e-15, or if A or B are of type single, 5e-7.

  • If you specify the AbsoluteTolerance name-value argument and do not specify the RelativeTolerance name-value argument, the relative tolerance is 0.

Example: isapprox(A,B,AbsoluteTolerance=1e-8) uses an absolute tolerance of 1e-8 and a relative tolerance of 0.

Example: isapprox(A,B,RelativeTolerance=1e-10,AbsoluteTolerance=1e-8) uses a relative tolerance of 1e-10 and an absolute tolerance of 1e-8.

Data Types: double | single

Algorithms

collapse all

Region of Approximate Equality

Tolerance

Algorithm

Region of Approximate Equality

Relative tolerance

|AB|relTol×max(|A|,|B|), where A and B are corresponding elements in the input arrays and relTol is the relative tolerance.

Absolute tolerance

|AB|absTol, where A and B are corresponding elements in the input arrays and absTol is the absolute tolerance.

Relative and absolute tolerances

|AB|max(absTol,relTol×max(|A|,|B|)), where A and B are corresponding elements in the input arrays, relTol is the relative tolerance, and absTol is the absolute tolerance.

Version History

Introduced in R2024b