【问题标题】:Compiling C++ code using __float128使用 __float128 编译 C++ 代码
【发布时间】:2015-06-06 21:53:58
【问题描述】:

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

我该如何解决?

【问题讨论】:

  • 签入quadmath.h 以获得operator "" q(__float128) - 这就是它所说的缺失。如果包含运算符时有任何#if/#endif 条件,请考虑是否/为什么可能会打错分支。您从哪里读到甚至提供了q 后缀?另外,相当多的网站建议使用 extern "C" { #include &lt;quadmath.h&gt; } - 可能是链接所必需的,具体取决于您的 quadmath 库版本的年龄。
  • 我试过extern "C" { #include &lt;quadmath.h&gt; },但它不起作用。
  • 我使用strtoflt128()避免了这个编译错误,但我仍然很好奇如何解决这个编译错误。
  • q 文字后缀是一个扩展,-std=c++11 禁用它。 Compile with -std=gnu++11.
  • 现在我真的很惊讶人们从哪里得到 128 位浮点数的想法,因为 SIMD 浮点数是 80 位宽。 x86/x64 上是否有任何 128 位浮点硬件支持?

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


【解决方案1】:

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

注意:使用-std=gnu++11-fext-numeric-literals 启用更多内置后缀

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这种方法的问题在于strtoflt128 不是constexpr(它是一个C 函数!)。你也不需要extern "C",因为它已经存在于标题中。
    猜你喜欢
    • 2014-09-09
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 2011-06-10
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多