使用 __float128 编译 C++ 代码

Compiling C++ code using __float128

我正在尝试在我的 C++ 程序中使用 __float128

但是我在编译它时遇到了麻烦。

这是简单的 C++ 代码 (test.cc):

#include <iostream>
#include <quadmath.h>

using namespace std;

int main(){
  __float128 r=0.0q;
  __float128 exp_d = expq(12.45q);

  cout << "r=" << (double)r << endl;
  cout << "exp_d=" << (double)exp_d << endl;
}

我用

编译这段代码

g++ test.cc -lquadmath -std=c++11

出现以下错误

error:unable to find numeric literal operator 'operateor"" q'

我该如何解决?

正如我之前评论的那样,我认为您的问题是 C++11 文字处理抢占了处理 q 后缀的特定于 GCC 的 C 扩展。尝试提供 C++11 运算符 - 如下所示:

#include <iostream>
extern "C"
{
    #include <quadmath.h>
}

__float128 operator ""q(const char* p)
{
    return strtoflt128(p, NULL);
}

int main()
{
    __float128 x = expq(282.49q);
    char buffer[128];
    quadmath_snprintf(buffer, sizeof buffer, "%.36Qg\n", x);
    std::cout << buffer << '\n';
}

Gcc-5 打印了这个有用的附加说明:

note: use -std=gnu++11 or -fext-numeric-literals to enable more built-in suffixes