Fix Issue 10448 - min and max are not NaN aware

This commit is contained in:
Bernhard Seckinger 2019-10-21 11:34:52 +02:00
parent 75392b3eca
commit 0320d88c3a
2 changed files with 42 additions and 8 deletions

View file

@ -1513,17 +1513,21 @@ if (isConvertibleToString!Range1 || isConvertibleToString!Range2)
assert(levenshteinDistanceAndPath(S("cat"), "rat")[0] == 1);
}
// max
/**
Iterates the passed arguments and return the maximum value.
Iterates the passed arguments and returns the maximum value.
Params:
args = The values to select the maximum from. At least two arguments must
be passed.
be passed, and they must be comparable with `>`.
Returns:
The maximum of the passed-in args. The type of the returned value is
The maximum of the passed-in values. The type of the returned value is
the type among the passed arguments that is able to store the largest value.
If at least one of the arguments is NaN, the result is an unspecified value.
See $(REF maxElement, std,algorithm,searching) for examples on how to cope
with NaNs.
See_Also:
$(REF maxElement, std,algorithm,searching)
@ -1634,9 +1638,17 @@ if (T.length >= 1)
/**
Iterates the passed arguments and returns the minimum value.
Params: args = The values to select the minimum from. At least two arguments
must be passed, and they must be comparable with `<`.
Returns: The minimum of the passed-in values.
Params:
args = The values to select the minimum from. At least two arguments must
be passed, and they must be comparable with `<`.
Returns:
The minimum of the passed-in values. The type of the returned value is
the type among the passed arguments that is able to store the smallest value.
If at least one of the arguments is NaN, the result is an unspecified value.
See $(REF minElement, std,algorithm,searching) for examples on how to cope
with NaNs.
See_Also:
$(REF minElement, std,algorithm,searching)
*/

View file

@ -3539,6 +3539,24 @@ Params:
Returns: The minimal element of the passed-in range.
Note:
If at least one of the arguments is NaN, the result is an unspecified value.
If you want to ignore NaNs, you can use $(REF filter, std,algorithm,iteration)
and $(REF isNaN, std,math) to remove them, before applying minElement.
Add a suitable seed, to avoid error messages if all elements are NaNs:
---
<range>.filter!(a=>!a.isNaN).minElement(<seed>);
---
If you want to get NaN as a result if a NaN is present in the range,
you can use $(REF fold, std.algorithm,iteration) and $(REF isNaN, std,math):
---
<range>.fold!((a,b)=>a.isNaN || b.isNaN ? real.nan : a < b ? a : b);
---
See_Also:
$(LREF maxElement), $(REF min, std,algorithm,comparison), $(LREF minCount),
@ -3662,16 +3680,20 @@ Iterates the passed range and returns the maximal element.
A custom mapping function can be passed to `map`.
In other languages this is sometimes called `argmax`.
Complexity:
Complexity: O(n)
Exactly `n - 1` comparisons are needed.
Params:
map = custom accessor for the comparison key
r = range from which the maximum will be selected
r = range from which the maximum element will be selected
seed = custom seed to use as initial element
Returns: The maximal element of the passed-in range.
Note:
If at least one of the arguments is NaN, the result is an unspecified value.
See $(REF minElement, std,algorithm,searching) for examples on how to cope
with NaNs.
See_Also: