Bug 6154 – std.math.abs on std.complex numbers too
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-06-14T05:03:00Z
Last change time
2013-08-04T05:51:12Z
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2011-06-14T05:03:13Z
I'd like std.math.abs to perform what std.complex.Complex!T.abs does. So I will be able to use it functionally (currently std.math.abs is able to work on the built-in complex numbers too). This means I'd like this code to work:
import std.stdio, std.math, std.algorithm, std.complex;
void main() {
alias Complex!double C;
C[] array2 = [C(1,2), C(2,4)];
auto m2 = map!abs(array2);
writeln(m2);
}
In DMD 2.053 similar code with built-in complex numbers works:
import std.stdio, std.math, std.algorithm;
void main() {
cdouble[] array1 = [1+2i, 2+4i];
auto m1 = map!abs(array1);
writeln(m1);
}
In DMD 2.053 if you want to do the same with complex numbers you need to write:
import std.stdio, std.math, std.algorithm, std.complex;
void main() {
alias Complex!double C;
C[] array2 = [C(1,2), C(2,4)];
auto m2 = map!((c){ return c.abs(); })(array2);
writeln(m2);
}
Comment #1 by kennytm — 2011-06-14T06:26:44Z
(In reply to comment #0)
> In DMD 2.053 if you want to do the same with complex numbers you need to write:
>
> import std.stdio, std.math, std.algorithm, std.complex;
> void main() {
> alias Complex!double C;
> C[] array2 = [C(1,2), C(2,4)];
> auto m2 = map!((c){ return c.abs(); })(array2);
> writeln(m2);
> }
auto m2 = map!`a.abs()`(array2);
Comment #2 by bearophile_hugs — 2011-06-14T10:01:24Z
(In reply to comment #1)
> auto m2 = map!`a.abs()`(array2);
Right. But std.math.abs has to work on complex numbers too, as before, for polymorphism, and for a serious integration of complex numbers in Phobos.
Comment #3 by clugdbug — 2011-06-14T11:03:40Z
(In reply to comment #2)
> (In reply to comment #1)
>
> > auto m2 = map!`a.abs()`(array2);
>
> Right. But std.math.abs has to work on complex numbers too, as before,
No, it does not, and will not.
BTW that approach would lead to executable bloat.
> for polymorphism, and for a serious integration of complex numbers in Phobos.
No. This is what overload sets are for.
Complex must not be given special treatment ahead of user-defined types. Otherwise, user-defined types become second-class citizens.
(std.math.abs is a bit of a problem at the moment, there are a couple of functions in std.math which have range versions, which IMHO have no business being in there).
Comment #4 by kennytm — 2011-06-14T13:22:08Z
(In reply to comment #2)
> (In reply to comment #1)
>
> > auto m2 = map!`a.abs()`(array2);
>
> Right. But std.math.abs has to work on complex numbers too, as before, for
> polymorphism, and for a serious integration of complex numbers in Phobos.
Right, but why must it be std.math.abs? Putting the free function as std.complex.abs works too.
Comment #5 by bearophile_hugs — 2011-06-15T13:04:17Z
(In reply to comment #4)
> Putting the free function as std.complex.abs works too.
OK.