[Issue 4725] std.algorithm.sum()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Mar 25 10:50:06 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=4725



--- Comment #19 from bearophile_hugs at eml.cc 2013-03-25 10:49:55 PDT ---
Is it worth adding to sum() an optional argument with the start value, as in
the Python sum()?

In this program in the first case there is an array of floats and you want to
use a real sum for max precision. In the second example there is an array of
ints, and the programmer wants to sum inside a long to avoid overflow:


import std.stdio, std.algorithm;

void main() {
    float[] a1 = [1.21, 1.3, 1.4];
    real s1 = reduce!q{a + b}(0.0L, a1);
    writefln("%.19f", s1);
    writefln("%.19f", a1.sum);

    int[] a2 = [int.max, int.max, int.max];
    real s2 = reduce!q{a + b}(0L, a2);
    writeln(s2);
    writeln(a2.sum);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list