【问题标题】:Quad-precision numbers with Intel Compiler (icc)使用英特尔编译器 (icc) 的四精度数字
【发布时间】:2021-01-31 15:14:36
【问题描述】:

我一直在尝试使用英特尔的四精度浮点数。我有以下代码,它返回了意外的结果。

#include <stdio.h>
#include <math.h>

int print(const char *label, _Quad r) {
  int prec = 20;
  int width = 46;
  char buf[128];

  int n = quadmath_snprintf(buf, sizeof buf, "%+-#*.36Qe", width, r);
  printf ("%s: %s\n", label, buf);
  return 0;
}

int main () {
  _Quad x = 3.14159265358979323846264338327950288q;
  print("value", x);
  print("log", logq(x));
  print("log10", log10q(x));
  print("cos", cosq(x));
  print("sin", sinq(x));
  print("sqrt", sqrtq(x));
}

此程序返回以下结果:

value: +3.141592653589793238462643383279502797e+00   
log: +7.644623500000000000000000000000000000e+07   
log10: -6.174980530000000000000000000000000000e+08   
cos: +0.000000000000000000000000000000000000e+00   
sin: +0.000000000000000000000000000000000000e+00   
sqrt: -1.994699018000000000000000000000000000e+09   

看起来四精度文字被正确解释了。但是,logqlog10qcosqsinqsqrtq 函数返回的结果不正确。

我发现的有关 Intel 的 _Quad 类型的唯一方向是 here

我在 MacOS 上编译这段代码:

icc -fPIC -wd1572 -Qoption,cpp,--extended_float_type -Wconversion -lquadmath -L/usr/local/Cellar/gcc/10.2.0/lib/gcc/10 test.c

我是否正确使用了四精度数学函数?

另外,我尝试使用this post 中描述的函数模式。

libm 函数的四倍精度类似物具有“__”前缀(双下划线)和“q”后缀。”

但是,这会导致所有函数都返回NaN

【问题讨论】:

    标签: c floating-point ieee-754 icc quadruple-precision


    【解决方案1】:

    您需要声明函数并且确实需要“__”前缀。例如:

    extern "C" _Quad __logq(_Quad x) ;
    

    这适用于 Windows。不明白为什么它不能在 Mac 上运行。

    【讨论】:

    • 为什么extern "C" 用于 C 编译?
    【解决方案2】:

    我在使用带有英特尔 C++ 编译器 2021 的 Visual Studio 时遇到了类似的问题。这是一个我认为可能是 ICC 中的错误的示例。

    _Quad nn = 23.1416Q;
    int mm1 = (int)(double)__log10q(nn); // there is a bug in Intel 2021 compiler. need to do cast here
    int mm2 = __log10q(nn);
    if (mm1 != mm2) {
    printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!nn = %g, mm1 = %d, mm2 = %d\n", (double)nn, mm1, mm2);
    }
    

    在 32 位版本中,它会打印:
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!nn = 23.1416, mm1 = 1, mm2 = 2

    在 32 位调试中,它会打印:
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!nn = 23.1416, mm1 = 4266, mm2 = 4267

    在 64 位调试和发布中,它会打印:
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!nn = 23.1416, mm1 = 1, mm2 = 2

    【讨论】:

      猜你喜欢
      • 2013-11-11
      • 2015-09-14
      • 2011-12-14
      • 2012-04-23
      • 2014-02-03
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      相关资源
      最近更新 更多