简体   繁体   English

使用哪种算法在GNU C ++标准库中计算指数函数?

[英]With which algorithm exponential functions are computed in the GNU C++ Standard Library?

Please consider std::exp defined in the header cmath in C++ numerics library. 请考虑在C ++ 数字库的头文件cmath中定义的std :: exp Now, please consider an implementation of the C++ Standard Library, say libstdc++ . 现在,请考虑一下C ++标准库的实现,比如libstdc ++

Considering there are various algorithms to compute the elementary functions, such as arithmetic-geometric mean iteration algorithm to compute the exponential function and three others shown here ; 考虑到有各种算法来计算基本函数,例如计算指数函数的算术几何平均迭代算法此处显示的其他三个算法 ;

Could you please name the particular algorithm being used to compute the exponential function in libstdc++ , if possible? 如果可能的话,你能否指出用于计算libstdc ++中指数函数的特定算法?

PS: I could not pinpoint either the correct tarballs containing the std::exp implementation or comprehend the relevant file contents, I'm afraid. PS:我无法确定包含std :: exp实现的正确tar包或理解相关文件内容,我担心。

It doesn't use any intricate algorithm at all. 它根本不使用任何复杂的算法。 Note that std::exp is only defined for a very limited number of types: float , double and long double + any Integral type that is castable to double . 请注意, std::exp仅针对非常有限的类型定义: floatdoublelong double +任何可转换为double Integral类型。 That makes it not necessary to implement complicated maths. 这使得没有必要实现复杂的数学。

Currently, it uses the builtin __builtin_expf as can be verified from the source code . 目前,它使用内置的__builtin_expf ,可以从源代码中进行验证。 This compiles to a call to expf on my machine which is a call into libm coming from glibc . 这编译成我的机器上的expf调用,这是一个来自glibc libm调用。 Let's see what we find in their source code . 让我们看看我们在他们的源代码中找到了什么。 When we search for expf we find that this internally calls __ieee754_expf which is a system-dependant implementation. 当我们搜索expf我们发现它在内部调用__ieee754_expf ,这是一个依赖于系统的实现。 Both i686 and x86_64 just include a glibc/sysdeps/ieee754/flt-32/e_expf.c which finally gives us an implementation (reduced for brevity, the look into the sources 双方的i686和x86_64只是包含glibc/sysdeps/ieee754/flt-32/e_expf.c这最终给了我们一个实现(减少简洁,外观成源

It is basically a order 3 polynomial approximation for floats: 它基本上是浮点数的3阶多项式逼近:

static inline uint32_t
top12 (float x)
{
  return asuint (x) >> 20;
}

float
__expf (float x)
{
  uint64_t ki, t;
  /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
  double_t kd, xd, z, r, r2, y, s;

  xd = (double_t) x;
  // [...] skipping fast under/overflow handling

  /* x*N/Ln2 = k + r with r in [-1/2, 1/2] and int k.  */
  z = InvLn2N * xd;

  /* Round and convert z to int, the result is in [-150*N, 128*N] and
     ideally ties-to-even rule is used, otherwise the magnitude of r
     can be bigger which gives larger approximation error.  */
  kd = roundtoint (z);
  ki = converttoint (z);
  r = z - kd;

  /* exp(x) = 2^(k/N) * 2^(r/N) ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
  t = T[ki % N];
  t += ki << (52 - EXP2F_TABLE_BITS);
  s = asdouble (t);
  z = C[0] * r + C[1];
  r2 = r * r;
  y = C[2] * r + 1;
  y = z * r2 + y;
  y = y * s;
  return (float) y;
}

Similarly, for 128-bit long double , it's an order 7 approximation and for double they use more complicated algorithm that I can't make sense of right now. 类似地,对于128位long double ,它是7阶近似值 ,对于double他们使用更复杂的算法 ,这是我现在无法理解的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您使用哪些C ++标准库包装函数? - Which C++ Standard Library wrapper functions do you use? 将旧式C ++标准库从GNU版本迁移到LLVM中的库 - Moving legacy C++ standard library from GNU version to that in LLVM 函数C ++中标准库函数的重载 - Function Overloading for the standard library functions in C++ 如何在C ++中查看标准库函数? - How to view standard library functions in C++? C ++将标准库算法应用于函数 - C++ apply standard library algorithms to functions 调用C标准库函数的标准C ++方法 - Standard C++ way of calling C standard library functions C ++中包含的C标准库函数是否抛出异常? - Do C standard library functions which are included in C++ throw exception? C ++标准库中是否有任何函数或类可以保证不执行动态内存分配? - Are there any functions or classes in C++ standard library which are guaranteed to not perform dynamic memory allocation? 在 C++ 中,需要哪些标准库函数(如果有)隐式提供原子 memory 围栏? - In C++, which Standard Library functions (if any) are required to implicitly provided an atomic memory fence? 标准C++库中哪些函数在使用时不应该以std::为前缀? - Which functions in standard C++ library should not be prefixed with std:: when used?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM