std.array suggestion
Don Clugston
dac at nospam.com.au
Thu Mar 9 04:59:30 PST 2006
Oskar Linde wrote:
> Hello,
>
> With the new IFTI support I have been looking at ways of upgrading the
> standard library with new and more generic functions.
[snip]
> Is there in general even any interest in adding generic functions to the
> standard library?
Some of these functions are the last thing remaining in std.math2 (but
they definitely don't below there, none of them are truly mathematical).
We definitely want to remove std.math2 prior to 1.0, std.array sounds
good to me.
I'll just comment on one function:
> T sum(T[] arr)
>
> Returns the sum of the element in arr as defined by the + operator.
This one has an interesting twist. I'm not sure that the sum should
necessarily be of type T. I've been playing around with the concept of
what I've called the Archetype of a type, which is the largest type with
the same semantics as T (possibly with the same calculation speed). (ie,
Archetype!(byte)= Archetype!(short)
Archetype!(int) = long
Archetype!(float) = Archetype!(double) = real,
Archetype!(cfloat)= creal, etc). Obviously it's a trivial template.
I think that at least, sum(double[] ) should internally use a real
while accumulating the sum, so that it can satisfy this test (at least
on x86 platforms):
unittest {
const double a = [ double.max, double.max, -double.max];
assert(sum(a) == double.max);
}
After all, this is one of the reasons why reals exist. I'm still not
sure if sum(double []) should return a double or a real, although I'm
inclined to think that *any* function that returns a single floating
point value should return a real (on x87, the 80-bit result is just left
on the FPU stack anyway). But, I'm less confident about how a sum of
ints should behave.
However, all the other functions seem to be free of mathematical
subtleties. sum() is the only one which involves arithmetic operators,
and therefore it might not belong with the rest.
More information about the Digitalmars-d
mailing list