在GNU C ++标准库中使用哪种算法计算指数函数? [英] With which algorithm exponential functions are computed in the GNU C++ Standard Library?
问题描述
请考虑 std :: exp 在标题
Please consider std::exp defined in the header cmath in C++ numerics library. Now, please consider an implementation of the C++ Standard Library, say 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;
是否可以在 libstdc ++ 中命名用于计算指数函数的特定算法?
Could you please name the particular algorithm being used to compute the exponential function in libstdc++, if possible?
PS:恐怕我无法查明包含std :: exp实现的正确tarball或理解相关文件的内容.
PS: I could not pinpoint either the correct tarballs containing the std::exp implementation or comprehend the relevant file contents, I'm afraid.
推荐答案
它根本不使用任何复杂的算法.请注意,std::exp
仅针对非常有限的几种类型定义:float
,double
和long double
+可转换为double
的任何整数类型.这样就不必实施复杂的数学.
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
. That makes it not necessary to implement complicated maths.
当前,它使用内置的__builtin_expf
,可以从源代码.这将编译为对我计算机上的expf
的调用,这是对来自glibc
的libm
的调用.让我们看看我们在其源代码中找到的内容.当我们搜索expf
时,我们发现它在内部调用__ieee754_expf
,这是与系统有关的实现. i686和x86_64都只包含一个glibc/sysdeps/ieee754/flt-32/e_expf.c
,最终为我们提供了一个实现(为简洁起见,外观
Currently, it uses the builtin __builtin_expf
as can be verified from the source code. This compiles to a call to expf
on my machine which is a call into libm
coming from glibc
. 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. 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
基本上是浮点数的3阶多项式近似值:
It is basically a order 3 polynomial approximation for floats:
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;
}
类似地,对于128位long double
,它是 7阶近似,对于double
,他们使用
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.
这篇关于在GNU C ++标准库中使用哪种算法计算指数函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!