On 7/23/2014 12:49 AM, Don wrote:
On Tuesday, 22 July 2014 at 15:31:22 UTC, Ola Fosheim Grøstad wrote:
D claims to focus generic programming. So it should also encourage pure
functions that can be specified for floats, ints and other numeric types that
are subtypes of (true) reals in the same clean definition.
I think it's a complete fantasy to think you can write generic code that will
work for both floats and ints. The algorithms are completely different.
One of the simplest examples is that given float f; int i;
(f + 1) and (i + 1) have totally different semantics.
There are no values of i for which i + 1 == i,
but if abs(f) > 1/real.epsilon, then f + 1 == f.
Likewise there is no value of i for which i != 0 && i+1 == 1,
but for any abs(f) < real.epsilon, f + 1 == 1.
If you express the expression in a clean way to get down to the actual (more
limited type) then the optimizer sometimes can pick an efficient sequence of
instructions that might be a very fast approximation if you reduce the
precision sufficiently in the end-result.
To get there you need to differentiate between a truncating division and a
non-truncating division etc.
Well, it's not a small number of differences. Almost every operation is
different. Maybe all of them. I can't actually think of a single operation where
the semantics are the same for integers and floating point.
Negation comes close, but even then you have the special cases -0.0 and
-(-int.max - 1).
The philosophy behind generic programming and the requirements for efficient
generic programming is quite different from the the machine-level hand
optimizing philosophy of classic C, IMO.
I think that unfortunately, it's a quest that is doomed to fail. Producing
generic code that works for both floats and ints is a fool's errand.
I quoted you on https://github.com/D-Programming-Language/phobos/pull/2366 !