[英]Why my natural log function is so imprecise?
Firstly, I'm using this approximation of a natural log.首先,我使用的是自然对数的近似值。 Or look here (4.1.27) for a better representation of formula.
或查看此处(4.1.27) 以获得更好的公式表示。
Here's my implementation:这是我的实现:
constexpr double eps = 1e-12;
constexpr double my_exp(const double& power)
{
double numerator = 1;
ull denominator = 1;
size_t count = 1;
double term = numerator / denominator;
double sum = 0;
while (count < 20)
{
sum += term;
numerator *= power;
#ifdef _DEBUG
if (denominator > std::numeric_limits<ull>::max() / count)
throw std::overflow_error("Denominator has overflown at count " + std::to_string(count));
#endif // _DEBUG
denominator *= count++;
term = numerator / denominator;
}
return sum;
}
constexpr double E = my_exp(1);
constexpr double my_log(const double& num)
{
if (num < 1)
return my_log(num * E) - 1;
else if (num > E)
return my_log(num / E) + 1;
else
{
double s = 0;
size_t tmp_odd = 1;
double tmp = (num - 1) / (num + 1);
double mul = tmp * tmp;
while (tmp >= eps)
{
s += tmp;
tmp_odd += 2;
tmp *= mul / tmp_odd;
}
return 2 * s;
}
}
You probably can see why I want to implement these functions.您可能会明白我为什么要实现这些功能。 Basically, I want to implement a pow function.
基本上,我想实现一个 pow function。 But still my approach gives very imprecise answers, for example my_log(10) = 2.30256, but according to google (ln 10 ~ 2.30259).
但是我的方法仍然给出了非常不精确的答案,例如 my_log(10) = 2.30256,但根据 google (ln 10 ~ 2.30259)。
my_exp() is very precise since it's taylor expansion is highly convergant. my_exp() 非常精确,因为它的泰勒展开是高度收敛的。 my_exp(1) = 2.718281828459, meanwhile e^1 = 2.71828182846 according to google.
根据谷歌,my_exp(1) = 2.718281828459,同时 e^1 = 2.71828182846。 But unfortunately it's not the same case for natural log, and I don't even know how is this series for a natural log derived (I mean from the links above).
但不幸的是,自然对数的情况并非如此,我什至不知道自然对数的这个系列是如何派生的(我的意思是来自上面的链接)。 And I couldn't find any source about this series.
而且我找不到关于这个系列的任何来源。
Where's the precision errors coming from?精度误差从何而来?
if (num < 1) return my_log(num * E) - 1;
has an imprecision in the multiplication.乘法有不精确性。 Multiplying by 2 is more accurate.
乘以 2 更准确。 Of course,
my_log(num) = my_log(2*num) - ln(2)
so you'll need to change the 1.0
constant.当然,
my_log(num) = my_log(2*num) - ln(2)
所以你需要更改1.0
常量。
Yes, now you'll have a rounding error in -ln(2)
instead of a rounding error in *E
.是的,现在您将在
-ln(2)
中出现舍入错误,而不是在*E
中出现舍入错误。 That's typically less bad.这通常不那么糟糕。
Also, you can save repeated rounding errors by first checking if (num<1/16) and then use my_log(num) = my_log(16*num) - ln(16)
.此外,您可以通过首先检查 if (num<1/16) 然后使用
my_log(num) = my_log(16*num) - ln(16)
来保存重复的舍入错误。 That's only a single rounding error.这只是一个舍入误差。
As for the error in your core loop, I suspect the culprit is s += tmp;
至于你的核心循环中的错误,我怀疑罪魁祸首是
s += tmp;
. . This is a repeated addition.
这是重复添加。 You can use Kahan summation there.
您可以在那里使用 Kahan 求和。
The line tmp *= mul / tmp_odd;
行
tmp *= mul / tmp_odd;
means that each term is also being divided by the denominators of all previous terms, ie 1, 1*3, 1*3*5, 1*3*5*7, ...
rather than 1, 3, 5, 7, ...
as the formula states.表示每个术语也被所有先前术语的分母除以,即
1, 1*3, 1*3*5, 1*3*5*7, ...
而不是1, 3, 5, 7, ...
正如公式所述。
The numerator and denominator should therefore be computed independently:因此,分子和分母应独立计算:
double sum = 0;
double value = (num - 1) / (num + 1);
double mul = value * value;
size_t denom = 1;
double power = value;
double term = value;
while (term > eps)
{
sum += term;
power *= mul;
denom += 2;
term = power / denom;
}
return 2 * sum;
...
// Output for num = 1.5, eps = 1e-12
My func: 0.405465108108004513
Cmath log: 0.405465108108164385
------------
Much better!好多了!
Reducing the epsilon to 1e-18
, we hit the accuracy limits of naïve summation:将 epsilon 减少到
1e-18
,我们达到了朴素求和的准确度限制:
// Output for num = 1.5, eps = 1e-18
My func: 0.40546510810816444
Cmath log: 0.405465108108164385
---------------
Kahan-Neumaier to the rescue: Kahan-Neumaier救援:
double sum = 0;
double error = 0;
double value = (num - 1) / (num + 1);
double mul = value * value;
size_t denom = 1;
double power = value;
double term = value;
while (term > eps)
{
double temp = sum + term;
if (abs(sum) >= abs(term))
error += (sum - temp) + term;
else
error += (term - temp) + sum;
sum = temp;
power *= mul;
denom += 2;
term = power / denom;
}
return 2 * (sum + error);
...
// Output for num = 1.5, eps = 1e-18
My func: 0.405465108108164385
Cmath log: 0.405465108108164385
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.