mirror of
https://github.com/dlang/phobos.git
synced 2025-05-01 15:40:36 +03:00
pairwise sum returns double for ranges with float elements
This commit is contained in:
parent
7783916096
commit
637bd45413
1 changed files with 7 additions and 3 deletions
|
@ -1082,12 +1082,12 @@ unittest
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pairwise summation http://en.wikipedia.org/wiki/Pairwise_summation
|
// Pairwise summation http://en.wikipedia.org/wiki/Pairwise_summation
|
||||||
ElementType!R sum(R)(R r)
|
auto sum(R)(R r)
|
||||||
if (hasSlicing!R && hasLength!R && isFloatingPoint!(ElementType!R))
|
if (hasSlicing!R && hasLength!R && isFloatingPoint!(ElementType!R))
|
||||||
{
|
{
|
||||||
switch (r.length)
|
switch (r.length)
|
||||||
{
|
{
|
||||||
case 0: return 0;
|
case 0: return 0.0;
|
||||||
case 1: return r.front;
|
case 1: return r.front;
|
||||||
case 2: return r.front + r[1];
|
case 2: return r.front + r[1];
|
||||||
default: return sum(r[0 .. $ / 2]) + sum(r[$ / 2 .. $]);
|
default: return sum(r[0 .. $ / 2]) + sum(r[$ / 2 .. $]);
|
||||||
|
@ -1097,7 +1097,11 @@ if (hasSlicing!R && hasLength!R && isFloatingPoint!(ElementType!R))
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
static assert(is(typeof(sum([1., 2., 3., 4.])) == double));
|
static assert(is(typeof(sum([1., 2., 3., 4.])) == double));
|
||||||
static assert(is(typeof(sum([1F, 2F, 3F, 4F])) == float));
|
static assert(is(typeof(sum([1F, 2F, 3F, 4F])) == double));
|
||||||
|
const(float[]) a = [1F, 2F, 3F, 4F];
|
||||||
|
static assert(is(typeof(sum(a)) == double));
|
||||||
|
const(float)[] b = [1F, 2F, 3F, 4F];
|
||||||
|
static assert(is(typeof(sum(a)) == double));
|
||||||
|
|
||||||
double[] empty;
|
double[] empty;
|
||||||
assert(sum(empty) == 0);
|
assert(sum(empty) == 0);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue