Skip to content

Commit

Permalink
Merge pull request #5316 from schveiguy/fix15763
Browse files Browse the repository at this point in the history
fix issue 15763 - Document behavior of relative difference
merged-on-behalf-of: Jack Stouffer <jack@jackstouffer.com>
  • Loading branch information
dlang-bot committed Apr 10, 2017
2 parents daa647e + 69b3298 commit 74a339e
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions std/math.d
Expand Up @@ -7098,13 +7098,23 @@ private real polyImpl(real x, in real[] A) @trusted pure nothrow @nogc


/**
Computes whether $(D lhs) is approximately equal to $(D rhs)
admitting a maximum relative difference $(D maxRelDiff) and a
maximum absolute difference $(D maxAbsDiff).
If the two inputs are ranges, $(D approxEqual) returns true if and
only if the ranges have the same number of elements and if $(D
approxEqual) evaluates to $(D true) for each pair of elements.
Computes whether two values are approximately equal, admitting a maximum
relative difference, and a maximum absolute difference.
Params:
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.
*/
bool approxEqual(T, U, V)(T lhs, U rhs, V maxRelDiff, V maxAbsDiff = 1e-5)
{
Expand Down Expand Up @@ -7142,8 +7152,13 @@ bool approxEqual(T, U, V)(T lhs, U rhs, V maxRelDiff, V maxAbsDiff = 1e-5)
{
static if (isInputRange!U)
{
// lhs is number, rhs is array
return approxEqual(rhs, lhs, maxRelDiff, maxAbsDiff);
// lhs is number, rhs is range
for (; !rhs.empty; rhs.popFront())
{
if (!approxEqual(lhs, rhs.front, maxRelDiff, maxAbsDiff))
return false;
}
return true;
}
else static if (isIntegral!T || isIntegral!U)
{
Expand Down Expand Up @@ -7285,6 +7300,18 @@ deprecated("Phobos1 math functions are deprecated, use isInfinity ") alias isinf
auto y = ceil(1.2);
}

@safe pure nothrow unittest
{
// relative comparison depends on rhs, make sure proper side is used when
// comparing range to single value. Based on bugzilla issue 15763
auto a = [2e-3 - 1e-5];
auto b = 2e-3 + 1e-5;
assert(a[0].approxEqual(b));
assert(!b.approxEqual(a[0]));
assert(a.approxEqual(b));
assert(!b.approxEqual(a));
}

/***********************************
* Defines a total order on all floating-point numbers.
*
Expand Down

0 comments on commit 74a339e

Please sign in to comment.