[eigen] Eigen::Array initialization with long double and mpreal type

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Hi Eigen,

I am observing value truncation during static array initialization that I am hoping for advice on.

Given a static array declared within a class then defined/initialized outside of class, the initialized values are truncated to double precision.  I observe this with long double types and mpreal types in the same manner within Eigen::Arrays.

When declared and initialized within the class I observe no truncation of the values.

Below is code that can be compiled/executed to illustrate what I observe; it shows the issue with long double types, but uncommenting/commenting a few lines and adding quotes for values as string literals will reveal the same issue with mpreal types.

Is there perhaps another method I should be initializing the array with?

Thank you for any advice you can offer!

​Best regards,
Mark

​​

/**
 * This file is a test for value initializations.
 * To compile from the terminal, save as test.cpp and run the following command
 *     g++ test.cpp -I /usr/include/eigen3 -lmpfr -lgmp -o test
 */

#include <mpfr..h>
#include <gmp.h>
#include <Eigen/Eigenvalues>
#include <unsupported/Eigen/MPRealSupport>

#include <iostream>
#include <iomanip>

using namespace Eigen;

typedef long double Scalar;
// typedef mpfr::mpreal Scalar;

template <typename Scalar>
class MyClass
{
public:
    static Array<Scalar, 2, 1> myArray1;
    static Scalar myScalar1;
};

template <typename Scalar>
    Array<Scalar, 2, 1> MyClass<Scalar>::myArray1 =
        (Array<Scalar, 2, 1>() <<
            0.11111111111111111111111111111111111111111111111111,
            0.00000000000000000000000000000000000000000000000001
        ).finished();

template <typename Scalar>
    Scalar MyClass<Scalar>::myScalar1 = 0.11111111111111111111111111111111111111111111111111L;

int main(void)
{
    // Scalar::set_default_prec(256);

    Array<Scalar, 2, 1> myArray2 =
        (Array<Scalar, 2, 1>() <<
            0.11111111111111111111111111111111111111111111111111,
            0.00000000000000000000000000000000000000000000000001
        ).finished();

    long double myScalar2 = 0.11111111111111111111111111111111111111111111111111L;
    double myDouble = 0.11111111111111111111111111111111111111111111111111;

    std::cout << std::setprecision(50) << std::endl;
    std::cout << "myArray1:" << std::endl;
    std::cout << MyClass<Scalar>::myArray1 << std::endl << std::endl;

    std::cout << "myArray2:" << std::endl;
    std::cout << myArray2 << std::endl << std::endl;

    std::cout << "myScalar1: " << MyClass<Scalar>::myScalar1 << std::endl;
    std::cout << "myScalar2: " << myScalar2 << std::endl;
    std::cout << "myDouble:      " << myDouble << std::endl << std::endl;

    std::cout << "myArray1(0): " << MyClass<Scalar>::myArray1(0) << std::endl;
    std::cout << "myScalar1:   " << MyClass<Scalar>::myScalar1 << std::endl;
    std::cout << "myDouble:    " << myDouble << std::endl;
}


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/