mirror of
https://github.com/dlang/phobos.git
synced 2025-05-01 15:40:36 +03:00
Merge pull request #3022 from ivan-timokhin/totalOrder
Total ordering for floating-point values.
This commit is contained in:
commit
f2220c2139
2 changed files with 245 additions and 0 deletions
|
@ -933,6 +933,7 @@ $(D less(a,c)) (transitivity), and, conversely, $(D !less(a,b) && !less(b,c)) to
|
|||
imply $(D !less(a,c)). Note that the default predicate ($(D "a < b")) does not
|
||||
always satisfy these conditions for floating point types, because the expression
|
||||
will always be $(D false) when either $(D a) or $(D b) is NaN.
|
||||
Use $(XREF math, cmp) instead.
|
||||
|
||||
Returns: The initial range wrapped as a $(D SortedRange) with the predicate
|
||||
$(D binaryFun!less).
|
||||
|
@ -1010,6 +1011,21 @@ unittest
|
|||
assert(words == [ "a", "aBc", "abc", "ABC", "b", "c" ]);
|
||||
}
|
||||
|
||||
///
|
||||
unittest
|
||||
{
|
||||
// Sorting floating-point numbers in presence of NaN
|
||||
double[] numbers = [-0.0, 3.0, -2.0, double.nan, 0.0, -double.nan];
|
||||
|
||||
import std.math : cmp, isIdentical;
|
||||
import std.algorithm.comparison : equal;
|
||||
|
||||
sort!((a, b) => cmp(a, b) < 0)(numbers);
|
||||
|
||||
double[] sorted = [-double.nan, -2.0, -0.0, 0.0, 3.0, double.nan];
|
||||
assert(numbers.equal!isIdentical(sorted));
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
import std.algorithm.internal : rndstuff;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue