New user self-registration is currently disabled. Please email eigen-core-team @ lists.tuxfamily.org if you need an account.
Bug 859 - Eigen exp() returns Inf instead of NaN
Eigen exp() returns Inf instead of NaN
Status: RESOLVED FIXED
Product: Eigen
Classification: Unclassified
Component: Core - general
3.2
x86 - SSE Linux
: Normal Wrong Result
Assigned To: Nobody
: JuniorJob, test-needed
Depends on:
Blocks: 3.3
  Show dependency treegraph
 
Reported: 2014-08-20 19:27 UTC by Marco Inacio
Modified: 2014-10-20 12:50 UTC (History)
4 users (show)



Attachments

Description Marco Inacio 2014-08-20 19:27:43 UTC
Whenever there is a NaN value inside an array, exp() will return an Inf instead of NaN.

Example bellow:


  #include <iostream>
  #include <limits>
  #include <Eigen/Dense>

  int main(){
    double nan = std::numeric_limits<double>::quiet_NaN();

    Eigen::MatrixXd m1(3,3);
    m1 << 10, 1.5, 3.2,
          nan, 10, 4.1,
          0.1, 4.1, 10;
         
    Eigen::MatrixXd m2(3,3);
    Eigen::ArrayXXd a1(3,3);

    //Has bug
    m2 = m1.array().exp().matrix();
    std::cout << m2 << std::endl << std::endl;
    
    //Has bug
    a1 = m1.array().exp();
    std::cout << a1 << std::endl << std::endl;

    //Output:
    //22026.5 4.48169 24.5325
    //inf 22026.5 60.3403
    //1.10517 60.3403 22026.5
    //
    //22026.5 4.48169 24.5325
    //inf 22026.5 60.3403
    //1.10517 60.3403 22026.5

    



    //Now, some other calls, just for comparison:
    a1 = m1.array();
    std::cout << a1 << std::endl << std::endl;  
      
    for (int i; i < m1.size(); i++)
      std::cout << std::exp(m1(i)) << std::endl;
    
    std::cout << std::endl;

    m2 = m1.array().log().matrix();
    std::cout << m2 << std::endl << std::endl;
    
    a1 = m1.array().log();
    std::cout << a1 << std::endl << std::endl;
  }
Comment 1 Christoph Hertzberg 2014-08-21 11:36:31 UTC
It seems the problem is that pexp in Eigen/src/Core/arch/SSE/MathFunctions.h does not check for NaNs.
We could simply return pmax(x, pexp(x)); because exp(x) is always bigger than x, and max(NaN, x) is NaN (beware that (max(x, NaN)==x) !)

Or we document that NaNs result in undefined values.
Comment 2 Gael Guennebaud 2014-08-21 15:19:51 UTC
Good idea to return return pmax(x, pexp(x));, the overhead is negligible.
Comment 3 Gael Guennebaud 2014-10-20 12:50:03 UTC
Fixed:

https://bitbucket.org/eigen/eigen/commits/f20b295aa91e/
Changeset:   f20b295aa91e
User:        ggael
Date:        2014-10-20 09:38:51+00:00
Summary:     Fix bug 859: pexp(NaN) returned Inf instead of NaN

3.2 backport:
https://bitbucket.org/eigen/eigen/commits/ff389140fde7/

Note You need to log in before you can comment on or make changes to this bug.