某次碰到pi,想用编程打印出它的比较多的有效位(至少比背的要多)。
开始考虑到 pi/4 = arctan(1)
arctan(x)展成多项式 arctan(x) = (1/1!)x - (1/3)(x^3) + (1/5)(x^5) - ....
所以有 pi/4 = 1 - 1/3 + 1/5 - 1/7 + .....
但是上式后面的式子收敛太慢了,编程很难求到很多的有效位,
而后查到Machin公式 pi/4 = 4arctan(1/5) - arctan(1/239),这个公式可以自己证明一下(忘的差不多了,涂了半天才算对)
这样泰勒展开式收敛很快,我就是照着这个公式编程的,代码如下
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define KS_MIN(a, b) ((a)<(b)?(a):(b))
#define KS_MAX(a, b) ((a)>(b)?(a):(b))
#define BITS 20000
typedef struct BigInt
{
int L;
int *d;
BigInt(){
L = 1;
d = (int*)malloc((BITS + 16)*sizeof(int));
d[0] = 0;
}
~BigInt(){ free(d); }
}BigInt;
// out = in / x;
void division(BigInt &out, BigInt &in, int x)
{
out.L = in.L;
me