SIGFPE encountered in `hypot_impl::run` with two zero arguments.
An arithmetic error occurs in Eigen's `hypot_impl::run`, during the use of Spectra to solve an eigenvalue problem. This is caused by a call to `hypot_impl::run` with two zero arguments .
Steps to Reproduce:
The bug is reproduceable when `hypot_impl::run` is called with two zero values.
Obviously the bug can be avoided by disabling FE_INVALID but this is an unattractive option in a large simulation.
using namespace Eigen::internal;
double c = hypot_impl<double>::run(a,b);
The program throws a SIGFPE, crashing. If the FE_INVALID is disabled the exception is avoided and the expected result is obtained.
I would change the implementation to something like this (requires re-ordering of methods in MathFunctions.h):
typedef typename NumTraits<Scalar>::Real RealScalar;
static inline RealScalar run(const Scalar& x, const Scalar& y)
RealScalar _x = abs(x);
RealScalar _y = abs(y);
RealScalar p = numext::maxi(_x, _y);
if(p==RealScalar(0)) return RealScalar(0);
RealScalar q = numext::mini(_x, _y);
RealScalar qp = q / p;
return p * sqrt(RealScalar(1) + qp*qp);
Another advantage: Only one branch required (which most of the time is false).
Open question: should infinities and NaNs be handled properly? hypot(inf, inf) would result in NaN, and either hypot(0, NaN) or hypot(NaN, 0) could result in 0, depending on how `maxi` and `mini` are implemented
Also: This implementation does not make sense for complex scalars, since `abs` is not necessarily stable.
I applied a slightly different patch to propagate NaN, together with dedicated unit tests and code factorization with scalar_hypot_op<>.
Regarding complexes, only pretty old versions of clang's stdlib had numerical issues with abs(complex), so we should be fine.
-- GitLab Migration Automatic Message --
This bug has been migrated to gitlab.com's GitLab instance and has been closed from further activity.
You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.com/libeigen/eigen/issues/1521.