complex arithmetic in D: multiple questions

Nicholas Wilson iamthewilsonator at hotmail.com
Fri Mar 9 13:56:33 UTC 2018


On Friday, 9 March 2018 at 12:34:40 UTC, J-S Caux wrote:
> Please bear with me, this is a long question!
> To explain, I'm a scientist considering switching from C++ to 
> D, but before I do, I need to ensure that I can:
> - achieve execution speeds comparable to C++ (for the same 
> accuracy; I can accept a slight slowdown, call it 30%, to get a 
> few more digits (which I typically don't need))
> - easily perform all standard mathematical operations on 
> complex numbers
> - (many other things not relevant here: memory use, 
> parallelization, etc).
>
> In the two files linked below, I compare execution 
> speed/accuracy between D and C++ when using log on complex 
> variables:
>
> https://www.dropbox.com/s/hfw7nkwg25mk37u/test_cx.d?dl=0
> https://www.dropbox.com/s/hfw7nkwg25mk37u/test_cx.d?dl=0
>
> The idea is simple: let a complex variable be uniformly 
> distributed around the unit circle. Summing the logs should 
> give zero.
>
> In the D code, I've defined an "own" version of the log, 
> log_cx, since std.complex (tragically!) does not provide this 
> function (and many others, see recent threads 
> https://forum.dlang.org/post/dewzhtnpqkaqkzxwpkrs@forum.dlang.org and https://forum.dlang.org/thread/lsnuevdefktulxltoqpj@forum.dlang.org, and issue https://issues.dlang.org/show_bug.cgi?id=18571).
>
> [snip]
>
> So simple C++ is thrice as fast as the best-achieved D I 
> managed.
>
> Now for my questions:
> - I would expect the D `Complex!double` case to work faster 
> than the `real` one. Why is it the other way around? [I can 
> accept (and use) D with Complex!real running 1/3 the speed of 
> C++ (but with increased accuracy), but I'd also love to be able 
> to run D with `Complex!double` at C++ speeds, since the 
> tradeoff might be worth it for some calculations]

because the double version is doing the exact same work as the 
real
except that it is also converting between real and double for 
atan2 (from arg). 
https://github.com/dlang/phobos/blob/master/std/math.d#L1352

I'm really not sure why phobos does that, it should't.

> - what is the best way to correct the unfortunate (to be 
> polite) omission of many standard mathematical functions from 
> std.complex? [if I may be frank, this is a real pain for us 
> scientists] There exists 
> https://gist.github.com/Biotronic/17af645c2c9b7913de1f04980cd22b37 but can this be integrated (after improvements) in the language, or should I (re)build my own?

It will be much faster to build your own that just forward to the 
C functions (or LLVM intrinsics) see 
https://github.com/libmir/mir-algorithm/blob/master/source/mir/math/common.d#L126 for a starting point (we'd greatly appreciate pull requests for anything missing).
Even if the decision to make std.math behave at the appropriate 
precision is accepted, it will still take an entire release cycle 
(unless you use dmd master), and I'm not so sure it will.

> - for my education, why was the decision made to go from the 
> built-in types `creal` etc to the `Complex` type?

I think because they can be done in the library.
I personally don't think they should have been, they don't 
complicate things that much.

> [related questions:

Did you press send too soon?


More information about the Digitalmars-d-learn mailing list