Compiling C++ code using __float128

妖精的绣舞 提交于 2019-12-12 11:47:44

问题


I'm trying to use __float128 in my C++ program.

However I'm in trouble with compiling it.

Here is simple c++ code (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;
}

And I compile this code with

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

which comes with following error

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

How can I fix it?


回答1:


Gcc-5 prints this helpful additional note:

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



回答2:


As I commented earlier, I think your problem is that the C++11 literal handling is pre-empting the GCC-specific C extension that handles the q suffix. Try providing a C++11 operator - as below:

#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';
}


来源:https://stackoverflow.com/questions/29404555/compiling-c-code-using-float128

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!