Merge remote-tracking branch 'upstream/stable' into merge_stable

This commit is contained in:
Martin Nowak 2017-12-30 19:15:43 +01:00
commit ac821be1de
2 changed files with 17 additions and 3 deletions

View file

@ -2483,7 +2483,7 @@ Convenience function. Like find, but only returns whether or not the search
was successful. was successful.
See_Also: See_Also:
$(LREF among) for checking a value against multiple possibilities. $(REF among, std,algorithm,comparison) for checking a value against multiple possibilities.
+/ +/
template canFind(alias pred="a == b") template canFind(alias pred="a == b")
{ {

View file

@ -527,14 +527,20 @@ enum real SQRT1_2 = SQRT2/2; /** $(SQRT)$(HALF)
* = hypot(z.re, z.im). * = hypot(z.re, z.im).
*/ */
Num abs(Num)(Num x) @safe pure nothrow Num abs(Num)(Num x) @safe pure nothrow
if (is(typeof(Num.init >= 0)) && is(typeof(-Num.init)) && if ((is(typeof(Num.init >= 0)) && is(typeof(-Num.init)) ||
(is(Num == short) || is(Num == byte))) &&
!(is(Num* : const(ifloat*)) || is(Num* : const(idouble*)) !(is(Num* : const(ifloat*)) || is(Num* : const(idouble*))
|| is(Num* : const(ireal*)))) || is(Num* : const(ireal*))))
{ {
static if (isFloatingPoint!(Num)) static if (isFloatingPoint!(Num))
return fabs(x); return fabs(x);
else
{
static if (is(Num == short) || is(Num == byte))
return x >= 0 ? x : cast(Num) -int(x);
else else
return x >= 0 ? x : -x; return x >= 0 ? x : -x;
}
} }
/// ditto /// ditto
@ -566,6 +572,14 @@ if (is(Num* : const(ifloat*)) || is(Num* : const(idouble*))
assert(abs(-1L+1i) == sqrt(2.0L)); assert(abs(-1L+1i) == sqrt(2.0L));
} }
@safe pure nothrow @nogc unittest
{
short s = -8;
byte b = -8;
assert(abs(s) == 8);
assert(abs(b) == 8);
}
@safe pure nothrow @nogc unittest @safe pure nothrow @nogc unittest
{ {
import std.meta : AliasSeq; import std.meta : AliasSeq;