This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Francesco Biscani
Recipients Francesco Biscani, Saksham Agrawal, berker.peksag, eric.smith, ezio.melotti, lemburg, mark.dickinson, steven.daprano, stutzbach, tim.peters
Date 2015-10-21.18:46:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1445453163.17.0.87817016353.issue25453@psf.upfronthosting.co.za>
In-reply-to
Content
Hi Mark,

the original code is C++, and the inf + nanj result can be reproduced by the following snippet:

"""
#include <complex>
#include <iostream>

int main(int argc, char * argv[]) {
  std::cout << std::complex<double>(3,0) / 0. << '\n';
  return 0;
}
"""

Here is the C99 version:

"""
#include <complex.h>
#include <math.h>
#include <stdio.h>

int main(int argc, char * argv[]) {
  double complex a = 3.0 + 0.0*I;
  printf("%f + i%f\n", creal(a/0.), cimag(a/0.));
  return 0;
}
"""

This is on Linux x86_64 with GCC 4.9. Clang gives the same result. Adding the "-ffast-math" compilation flag changes the result for the C version but apparently not for the C++ one.

The original code came from an implementation of a special function f(z) which has a pole near the origin. When computing f(0), the C++ code returns inf+nan*j, which is fine (in the sense that it is a complex infinity of some kind). I then need to use this result in a larger piece of code which at one point needs to compute 1/f(0), with the expected result being 0 mathematically. This works if I implement the calculation all within C/C++, but if I switch to Python when computing 1/f(0) I get nan + nan*j as a result.

Of course I could do all sorts of stuff to improve this specific calculation and avoid the problems (possibly improving the numerical behaviour near the pole via a Taylor expansion, etc. etc. etc.).

Beware that implementing C99 behaviour will result in a noticeable overhead for complex operations. In compiled C/C++ code one usually has the option to switch on/off the -ffast-math flag to improve performance at the expense of standard-conformance, but I imagine this is not feasible in Python.
History
Date User Action Args
2015-10-21 18:46:03Francesco Biscanisetrecipients: + Francesco Biscani, lemburg, tim.peters, mark.dickinson, eric.smith, stutzbach, ezio.melotti, steven.daprano, berker.peksag, Saksham Agrawal
2015-10-21 18:46:03Francesco Biscanisetmessageid: <1445453163.17.0.87817016353.issue25453@psf.upfronthosting.co.za>
2015-10-21 18:46:03Francesco Biscanilinkissue25453 messages
2015-10-21 18:46:02Francesco Biscanicreate
Morty Proxy This is a proxified and sanitized view of the page, visit original site.