0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

e^xのマクローリン展開をホーナー法で

Last updated at Posted at 2024-10-12
\begin{align}
e^x &= 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \frac{x^4}{4!} + \cdots \\
&= 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!}(1+\frac{x}{4}\cdots) \\
&= 1 + x + \frac{x^2}{2!}(1 + \frac{x}{3}(1+\frac{x}{4}\cdots)) \\
&= 1 + x(1 + \frac{x}{2}(1 + \frac{x}{3}(1+\frac{x}{4}\cdots))) \\
\end{align}

このように計算することで階乗を計算する必要がなくなり、ループ回数iに階乗計算による制限がなくなることで精度を高めることができる。sin,cosは同様にくくり、r*x*x/(i*(i-1))の項が現れ、4の剰余で1の符号を判断するなどして計算できる。

#include <stdio.h>
int main()
{
    double r = 1; // result
    double x = 1;

    for (int i = 10; i > 0; --i)
        r = r*x/i + 1;

    printf("%.15lf\n", r);

    return 0;
}

美しい。
誰もこのコードを見てネイピア数の計算できるとは思わない(多分)。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?