assert_isapprox(computed, expected)
assert_isapprox(computed, expected, precision)
res = assert_isapprox(computed, expected)
res = assert_isapprox(computed, expected, precision)
[res, msg] = assert_isapprox(computed, expected)
[res, msg] = assert_isapprox(computed, expected, precision)
| Parameter | Description |
|---|---|
| computed | a numeric value: matrix, sparse double, or multidimensional array. |
| expected | a numeric value: matrix, sparse double, or multidimensional array. |
| precision | a double value specifying the relative tolerance. Default precision is 0. |
| Parameter | Description |
|---|---|
| res | a logical value: true if values are approximately equal, false otherwise. |
| msg | a string containing the error message. If res == true, then msg == ''. If res == false, then msg contains the assertion failure message. |
assert_isapprox raises an error if the computed value is not approximately equal to the expected value.
This function compares two floating point numbers with a specified tolerance, allowing you to check that two numbers are "approximately" equal when exact equality is not suitable due to floating-point precision limitations.
The comparison uses relative error calculation to determine if the values are within the specified precision tolerance.
This function is particularly useful in unit testing when dealing with numerical computations that may have small rounding errors.
assert_isapprox(1.23456, 1.23457, 1e-5)
try
assert_isapprox(1.23456, 1.23457, 1e-6)
catch ME
disp(['Error: ' ME.message])
end
[r, msg] = assert_isapprox(1.23456, 1.23457, 1e-6);
assert_isfalse(r);
assert_isequal(msg, _('Assertion failed: expected and computed values are too different.'));
A = [1.0001, 2.0002; 3.0003, 4.0004];
B = [1.0000, 2.0000; 3.0000, 4.0000];
assert_isapprox(A, B, 1e-3)
| Version | Description |
|---|---|
| 1.0.0 | initial version |