[pythran] Re: cx-limited-range

  • From: serge Guelton <sguelton@xxxxxxxxxxxxx>
  • To: pythran@xxxxxxxxxxxxx
  • Date: Fri, 14 Nov 2014 22:23:12 +0100

On Fri, Nov 14, 2014 at 12:02:15PM -0800, Mehdi AMINI wrote:
> Hi,
> from pythran/pythonic/patch/README.rst
> """
> The implementation of std::complex is very slow, due to the complex limited
> range (see `-fcx-limited-range`) feature. Numpy does not implement it, so we
> have to conform to numpy's version.
> """

Let's focus on the multiplication.

The multiplication in CPython is implemented as

c_prod(Py_complex a, Py_complex b)
    Py_complex r;
    r.real = a.real*b.real - a.imag*b.imag;
    r.imag = a.real*b.imag + a.imag*b.real;
    return r;

and the result of the following:

>>> c = complex(4, -np.inf)
>>> d = complex(-4, -np.inf)
>>> c * d

same behavior with np.complex

while in C++

#include <iostream>
#include <complex>
#include <limits>

int main() {
  std::complex<double>c{-4., std::numeric_limits<double>::quiet_NaN()};
  std::complex<double>d{4., std::numeric_limits<double>::quiet_NaN()};
  auto f = c * d;
  std::cout << f << std::endl;
  return 0;

prints (nan,nan)

So, Python/Numpy behavior and C++ behavior are different with respect to
complex multiplication. What we did is provide a std::complex<T> that
match Python/Numpy behavior. We cannot ask nt2 to follow numpy's
behvaior, so... we hijacked std::complex :-/

I have not checked the division though

Other related posts: