std.math.approxEqual - multiple declarations
Function approxEqual
Returns approxEqual(lhs, rhs, 1e-2, 1e-5).
bool approxEqual(T, U)
(
T lhs,
U rhs
);
Example
assert(approxEqual(1.0, 1.0099));
assert(!approxEqual(1.0, 1.011));
float[] arr1 = [ 1.0, 2.0, 3.0 ];
double[] arr2 = [ 1.001, 1.999, 3 ];
assert(approxEqual(arr1, arr2));
real num = real .infinity;
assert(num == real .infinity); // Passes.
assert(approxEqual(num, real .infinity)); // Fails.
num = -real .infinity;
assert(num == -real .infinity); // Passes.
assert(approxEqual(num, -real .infinity)); // Fails.
assert(!approxEqual(3, 0));
assert(approxEqual(3, 3));
assert(approxEqual(3.0, 3));
assert(approxEqual([3, 3, 3], 3.0));
assert(approxEqual([3.0, 3.0, 3.0], 3));
int a = 10;
assert(approxEqual(10, a));
Function approxEqual
Computes whether two values are approximately equal, admitting a maximum relative difference, and a maximum absolute difference.
bool approxEqual(T, U, V)
(
T lhs,
U rhs,
V maxRelDiff,
V maxAbsDiff = 1e-05
);
Parameters
| Name | Description |
|---|---|
| lhs | First item to compare. |
| rhs | Second item to compare. |
| maxRelDiff | Maximum allowable difference relative to rhs. |
| maxAbsDiff | Maximum absolute difference. |
Returns
true if the two items are approximately equal under either criterium.
If one item is a range, and the other is a single value, then the result
is the logical and-ing of calling approxEqual on each element of the
ranged item against the single item. If both items are ranges, then
approxEqual returns true if and only if the ranges have the same
number of elements and if approxEqual evaluates to true for each
pair of elements.
Authors
Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger