2010/1/21 Sean Kelly <[email protected]>: > My first inclination would be for abs to only accept signed values. Unsigned > values don't really seem appropriate for unchecked math operations.
I agree, it doesn't make sense to take the absolute value of something which doesn't have a sign. With range checking, it ought to be possible to implicitly cast from uint to int, which would allow all of the sensible uses. Unfortunately, implicit casting and templates don't interact well. So I think abs() might need to go back to being a non-template function. > On Jan 20, 2010, at 8:50 PM, Andrei Alexandrescu wrote: > >> 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 <[email protected]> >> 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 >> [email protected] >> http://lists.puremagic.com/mailman/listinfo/phobos > > _______________________________________________ > phobos mailing list > [email protected] > http://lists.puremagic.com/mailman/listinfo/phobos > _______________________________________________ phobos mailing list [email protected] http://lists.puremagic.com/mailman/listinfo/phobos
