[phobos] [Fwd: "Unsigned-related bugs never occur in real code."]

Steve Schveighoffer schveiguy at yahoo.com
Thu Jan 21 07:51:04 PST 2010


Fixed version:

import std.math, std.stdio;

void main() {
    auto a = [ 4, 4, 2, 3, 2 ];
    float avgdist = 0;
    uint count;

    foreach (i, e1; a[0 .. $-1]) {
        foreach (j, e2; a[i+1 .. $]) {
            if (e1 != e2) continue;
            count++;
            avgdist += j+1;
        }
    }

    writeln(count, " ", avgdist / count);
}

:)  I'm sure that's not what you were asking though...

-Steve



----- Original Message ----
> From: Andrei Alexandrescu <andrei at erdani.com>
> To: Discuss the phobos library for D <phobos at puremagic.com>
> Sent: Wed, January 20, 2010 11:50:20 PM
> Subject: [phobos] [Fwd: "Unsigned-related bugs never occur in real code."]
> 
> Is there anything we can do about this?
> 
> Andrei
> 
> -------- Original Message --------
> Subject: "Unsigned-related bugs never occur in real code."
> Date: Wed, 20 Jan 2010 20:42:50 -0800
> From: Andrei Alexandrescu 
> Organization: Digital Mars
> Newsgroups: digitalmars.D
> 
> "It's an academic problem. Don't worry about it and move on."
> 
> That's what Walter kept on telling me. Yet I've spent the better part of
> an hour reducing a bug down to the following:
> 
> import std.math, std.stdio;
> 
> void main() {
>     auto a = [ 4, 4, 2, 3, 2 ];
>     float avgdist = 0;
>     uint count;
> 
>     foreach (i, e1; a) {
>         foreach (j, e2; a) {
>             if (i == j) continue;
>             if (e1 != e2) continue;
>             ++count;
>             avgdist += abs(i - j);
>         }
>     }
> 
>     writeln(count, " ", avgdist / count);
> }
> 
> May this post be an innocent victim of the war against unsigned-related
> bugs.
> 
> 
> Andrei
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos



      


More information about the phobos mailing list