使用__float128编译C ++代码

时间:2015-04-02 03:35:06

标签: c++ c++11 g++

我正在尝试在我的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'

我该如何解决?

2 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分:1)

正如我之前评论过的,我认为您的问题是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';
}
相关问题