如何对数编程?对数

2023-09-11 00:19:14 作者:◎拖黎帶氺╰二爷

难道他们只是想出了用蛮力的做法还是有一种算法,这样做?

Are they just figured out using a brute force approach or is there an algorithm in doing so?

推荐答案

一个功能的实现,如自然对数在任何像样的数学库将保持低于ULP错误(最不precision单位)。数学运算库函数的实现者的目标是要找到一些最佳逼近,应该达到用尽可能少的计算,可以预期的准确性。泰勒级数通常较差的选择,因为需要太多的术语来实现所需的精度。

The implementation of a function such as the natural logarithm in any decent math library will keep the error below an ulp (unit of least precision). The goal of the implementor of a math library function is to find some optimal approximation, one that achieves the desired accuracy with as few calculations as possible. Taylor series are typically a poor choice because far too many terms are needed to achieve the desired accuracy.

选择典型的武器是降低的范围内的所有重新presentable实数一些非常小的区域,然后使用该产生所需的功能在这个狭窄的范围内准确逼近一些最佳逼近。选择这个最佳逼近典型的武器是一个多项式或有理多项式(两个多项式之比)。实施只包含多项式系数。这些系数通过一些优化技术,构建如雷梅斯交换算法。

The typical weapons of choice are to reduce the range from all representable real numbers to some very small region, and then use some optimal approximation that yields an accurate approximation of the desired function over this narrow range. The typical weapons of choice for this optimal approximation are a polynomial or a rational polynomial (ratio of two polynomials). The implementation just contains the polynomial coefficients. Those coefficients are constructed by some optimizing technique such as the Remes Exchange algorithm.

在自然对数的情况下,有一种简单的方法,以减少的范围内。实数几乎普遍重presented在尾数和指数方面: X = M * 2 P ,其中 P 是一个整数和 M 的是1和2之间。因此日志( X )=日志( M )+ < I> P *日志(2)。后一术语,的 P 的日志*(2),只是一个乘以一个已知常数。问题因此减少寻找1和2(或1/2和1之间)之间的一个数的对数。进一步减小范围可通过使用的事实,√2是对数中的[1,2)的中间进行。因此,所有需要的是一种方法来计算1和√2之间的一个数的对数。这通常是用一个有理多项式。第二阶多项式多项式到三阶的比率工作得很好这一点。

In the case of the natural logarithm, there is an easy way to reduce the range. Real numbers are almost universally represented in terms of a mantissa and an exponent: x=m*2p, where p is an integer and m is between 1 and 2. Thus log(x) = log(m)+p*log(2). The latter term, p*log(2), is just a multiplication by a known constant. The problem thus reduces to finding the logarithm of a number between 1 and 2 (or between 1/2 and 1). A further range reduction can be made by using the fact that √2 is logarithmically in the middle of [1,2). Thus all that is needed is a way to calculate the logarithm of a number between 1 and √2. This is typically done with a rational polynomial. The ratio of a second order polynomial polynomial to a third order works quite nicely for this.