将 libquadmath 与特征一起使用

Using libquadmath with eigen

本文关键字:一起 特征 libquadmath      更新时间:2023-10-16

我想将__float128与 eigen 的 erf(( 函数一起使用,但发现目前它只支持浮点数和双精度:

此函数仅支持 c++11 中的浮点数和双标量类型 模式。支持其他标量类型,或在非 c++11 中支持浮点/双精度 模式,用户必须为任何标量提供 erf(T( 的实现 键入要支持的 T。

由于我想使用__float128,如果可能的话,我想依靠libquadmath erfq实现。但是怎么做呢?我目前能想到的唯一(丑陋的?(方法是使用特征unaryExpr()。还有其他可能性吗?

您可以专门化Eigen::internal::erf_impl(当然类似于任何其他功能(:

#include <quadmath.h>
#include <iostream>
#include <unsupported/Eigen/SpecialFunctions>
namespace Eigen { namespace internal {
template<>
struct erf_impl<__float128> {
  EIGEN_DEVICE_FUNC
  static EIGEN_STRONG_INLINE __float128 run(__float128 x) { return ::erfq(x); }
};
}}
int main()
{
    typedef Eigen::Array<__float128, Eigen::Dynamic, 1> ArrayXF;
    ArrayXF a(4); a << 0, 0.25, 0.5, 0.75;
    ArrayXF b = a.erf();
    for(int i=0; i<4; ++i){
        char buf[100];
        quadmath_snprintf(buf, 100, "%.50Qe", b[i]); std::cout << buf << 'n';
    }
}

输出:

0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

2.76326390168236932985068267764815703534647315720851e-01 5.20499877813046537682746653891964513119913394193564e-01 7.11155633653515131598937834591410814324096358715387e-01