一个高精度计算技术网站 - l1t1/note GitHub Wiki

在查找计算ln(2)的高效算法时,发现如下的宝藏网站[http://www.hvks.com/Numerical/arbitrary_precision.html],里面包含许多论文[http://www.hvks.com/Numerical/papers.html]和一个高精度库源代码[http://www.hvks.com/Numerical/Downloads/precisionSource.zip]。 将源代码解压到mp子目录下。 从源码附带的文档中找到如下的例子:(从其他例子复制了包含头文件的语句)

#include <complex>
#include <iostream>
#include <functional>

#include "mp/fprecision.h"
#include "mp/complexprecision.h"
#include "mp/fractionprecision.h"
using namespace std;

void continued_fraction_pi_lambert()
{
int i,j;
fraction_precision<int_precision> cf;
cout << "Start of Lambert PI. (First 8 iterations)" << endl;
for(j=1;j<=8;++j)
{
for (i = j; i >=0; --i)
{
cf += fraction_precision<int_precision>(i * 2 + 1, 1);
if (i > 0)
cf = fraction_precision<int_precision>(i*i, 1) / cf;
else
cf = fraction_precision<int_precision>(4, 1)/cf;
}
cout << j << ": " << cf << " = " << (double)cf << " Error: " <<
(double)cf - M_PI << endl;
}
cout << "end of Lambert PI" << endl;
return;
}
int main()
{
	continued_fraction_pi_lambert();
	return 1;
}

在Windows下用mingw64编译命令如下:

C:\d>g++ mp_pi.cpp mp/precisioncore.cpp -o mp_pi

运行结果如下

C:\d>mp_pi
Start of Lambert PI. (First 8 iterations)
1: 3/1 = 3 Error: -0.141593
2: 28/9 = 3.11111 Error: -0.0304815
3: 1972/627 = 3.14514 Error: 0.00354291
4: 1409008/448557 = 3.1412 Error: -0.000390978
5: 642832772/204617505 = 3.14163 Error: 3.87137e-05
6: 620973746437/197662271090 = 3.14159 Error: -2.99658e-06
7: 21256237030334666/6766070335136595 = 3.14159 Error: 2.53911e-08
8: 29359991221904052211456/9345575277160084385045 = 3.14159 Error: 6.28755e-08
end of Lambert PI

下面用这个库来计算Pi, e, ln(2), sqrt(2),如下代码在网页示例中添加:

#include <complex>
#include <iostream>
#include <functional>

#include "mp/fprecision.h"
#include "mp/complexprecision.h"
#include "mp/fractionprecision.h"
using namespace std;

void test()
{
int_precision a(11111111), d;
float_precision f1(10,64); // Initialized to zero and set precision to 64 digits
float_precision f2(2.0), f3(3), f4("0.12345678901234567890" );

d = a * a * a; // a^3
cout << "11111111^3=" << d << endl;
d /= a;
cout << "11111111^2=" << d << endl;

f1 = log( f1 );
f1 *= f2 + f3 / f4;
f1 = _float_table( _PI, 48 ); // Get PI with 48 decimals
cout << "PI with 48 digits:" << f1 << endl;
f1 = float_precision( 2 );
f1 = sqrt( f1 );
cout << "Sqrt(2) with 48 digits:" << f1 << endl;
f1 = _float_table( _LN2, 48 ); // Get LN2 with 48 decimals
cout << "ln(2) with 48 digits:" << f1 << endl;
f1 = _float_table( _EXP1, 48 ); // Get e with 48 decimals
cout << "e with 48 digits:" << f1 << endl;
}

int main()
{
	test();
	return 1;
}

在Windows下用mingw64编译命令如下:

C:\d>g++ mp_test.cpp mp/precisioncore.cpp -o mp_test

运行结果如下,所有浮点数据都以科学计数法格式显示。

C:\d>mp_test
11111111^3=1371742071330590260631
11111111^2=123456787654321
PI with 48 digits:3.1415926535897932384626433832795028841971693993749945755528582557E0
Sqrt(2) with 48 digits:1.4142135623730950488016887242096980785696718753769480731766797379E0
ln(2) with 48 digits:6.931471805599453094172321214581765680755001343602910784427860276E-1
e with 48 digits:2.7182818284590452353602874713526624977572470937004922397167671079E0

感兴趣的可以将它与gmp库的性能做个比较。

⚠️ **GitHub.com Fallback** ⚠️