4

根据https://stackoverflow.com/a/17932632/1700939,应该可以使用带有 boost::multiprecision 和 gcc-4.7 的复数。这确实适用于 boost::multiprecision::float128:

-----------test.cpp------------
#include <cmath>
#include <boost/multiprecision/float128.hpp>

using namespace std;

typedef boost::multiprecision::float128 real_type_b;
typedef complex<real_type_b> numeric_type_b;


int main()
{

    numeric_type_b a(2,2);
    numeric_type_b r = numeric_type_b(2,2)*a;
    a = sin(r);
    a = exp(r);
    cout << a << endl;

}
-----------test.cpp------------

$ g++-4.7 -std=c++0x test.cpp -lquadmath -o test
$ ./test
(-0.1455,0.989358)

使用 boost::multiprecision::mpfr 尝试相同的操作失败 -Function expsin工作正常(!))

-----------test.cpp------------
#include <cmath>
#include <boost/multiprecision/mpfr.hpp>

using namespace std;

typedef boost::multiprecision::mpfr_float_500 real_type_b;
typedef complex<real_type_b> numeric_type_b;


int main()
{

    numeric_type_b a(2,2);
    numeric_type_b r = numeric_type_b(2,2)*a;
    a = sin(r);
    a = exp(r);
    cout << a << endl;

}
-----------test.cpp------------

编译失败:

$ g++-4.7 -std=c++0x test.cpp -lmpfr -o test
In file included from /usr/include/boost/config/no_tr1/complex.hpp:21:0,
                from /usr/include/boost/math/policies/error_handling.hpp:15,
                from /usr/include/boost/multiprecision/detail/default_ops.hpp:9,
                from /usr/include/boost/multiprecision/detail/generic_interconvert.hpp:9,
                from /usr/include/boost/multiprecision/number.hpp:22,
                from /usr/include/boost/multiprecision/mpfr.hpp:9,
                from test.cpp:3:
/usr/include/c++/4.7/complex: In instantiation of ‘std::complex<_Tp> std::__complex_exp(const std::complex<_Tp>&) [with _Tp = boost::multiprecision::number<boost::multiprecision::backends::mpfr_float_backend<500u> >]’:
/usr/include/c++/4.7/complex:751:68:   required from ‘std::complex<_Tp> std::exp(const std::complex<_Tp>&) [with _Tp = boost::multiprecision::number<boost::multiprecision::backends::mpfr_float_backend<500u> >]’
test.cpp:17:18:   required from here
/usr/include/c++/4.7/complex:736:52: error: no matching function for call to ‘polar(boost::enable_if_c<true, boost::multiprecision::detail::expression<boost::multiprecision::detail::function, boost::multiprecision::detail::exp_funct<boost::multiprecision::backends::mpfr_float_backend<500u> >, boost::multiprecision::number<boost::multiprecision::backends::mpfr_float_backend<500u> >, void, void> >::type, boost::multiprecision::number<boost::multiprecision::backends::mpfr_float_backend<500u> >)’
/usr/include/c++/4.7/complex:736:52: note: candidate is:
/usr/include/c++/4.7/complex:662:5: note: template<class _Tp> std::complex<_Tp> std::polar(const _Tp&, const _Tp&)
/usr/include/c++/4.7/complex:662:5: note:   template argument deduction/substitution failed:
/usr/include/c++/4.7/complex:736:52: note:   deduced conflicting types for parameter ‘const _Tp’ (‘boost::multiprecision::detail::expression<boost::multiprecision::detail::function, boost::multiprecision::detail::exp_funct<boost::multiprecision::backends::mpfr_float_backend<500u> >, boost::multiprecision::number<boost::multiprecision::backends::mpfr_float_backend<500u> >, void, void>’ and ‘boost::multiprecision::number<boost::multiprecision::backends::mpfr_float_backend<500u> >’)

但是,如果exp-line 被注释掉,它可以工作:

$ g++-4.7 -std=c++0x test.cpp -lmpfr -o test
$ ./test
(0,1490.48)

我做错了什么,还是我在这里遇到了错误?

4

1 回答 1

2

Seems like a bug in GNU's library. It triggers when there's _GLIBCXX_USE_C99_COMPLEX here:

#if _GLIBCXX_USE_C99_COMPLEX
  // implementations

  template<typename _Tp>
    inline complex<_Tp>
    exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
#else
  template<typename _Tp>
    inline complex<_Tp>
    exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
#endif

It looks like it breaks ADL on the exp call (by forwarding with the value of __rep()).

You can easily find out for yourself by hardcoding the exp implementation:

  template<typename T>
    inline std::complex<T>
    my_exp(const std::complex<T>& x)
    { 
        using std::exp; // use ADL
        return std::polar(exp(x.real()), x.imag()); 
    }

This works

#include <cmath>
#include <boost/multiprecision/float128.hpp>
#include <boost/multiprecision/mpfr.hpp>

#if 1
typedef boost::multiprecision::mpfr_float_500 real_type_b;
typedef std::complex<real_type_b> numeric_type_b;
#else
typedef boost::multiprecision::float128 real_type_b;
typedef std::complex<real_type_b> numeric_type_b;
#endif

namespace check
{
  template<typename T>
    inline std::complex<T>
    my_exp(const std::complex<T>& x)
    { 
        using std::exp; // use ADL
        T const& r = exp(x.real());
        return std::polar(r, x.imag()); 
    }
}

#include <iostream>

int main()
{
    numeric_type_b a(2,2);
    numeric_type_b r = numeric_type_b(2,2)*a;
    a = std::sin(r);
    a = check::my_exp(r);
    std::cout << a << std::endl;
}

Works for both types.

Output

(-0.1455,0.989358)
于 2014-02-12T20:13:51.600 回答