E001

调用orsci中的高精度计算numb和prec类型。

 

//E001
//By JiangWei 演示orsci高精度计算模块中的部分功能。
//2019-02-21
//http://www.jiangw.cn
//http://www.orsci.cn

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

#include "orsciHP.h"
using namespace orsci;

#define APPNAME "E001 - 高精度计算演示 http://www.orsci.cn"

int _tmain(int argc, _TCHAR* argv[])
{
cout << APPNAME << endl;

//演示:计算500的阶乘。测试大整数numb类型
{
cout << "Demo1:计算500的阶乘!" << endl;
numb x; //定义大整数
x = 1;
for (int k = 1; k < 500; k ++) //计算500的阶乘
{
x = x * k;
}
cout << x << endl;
cout << "有效数字长度:" << x.ofValidDigitalLen() << endl;
}

prec::resetMaxDecPrecision(500); //设定有效数字的位数,默认64。注:prec设置精度对所有prec定义的对象都有效,只需设置一次。

//演示:计算除法。Demo3:大浮点数prec乘法。
{
cout << "Demo2:计算1/7的除法,保留500位有效数字!" << endl;

prec y; //定义高精度浮点数

y = 1;
prec z = y / 7;
cout << z << endl;
cout << "Demo3:计算(1/7) * (1/7),保留500位有效数字!" << endl;
cout << z * z << endl;
}

//演示:大浮点数的部分函数演示
{
cout << "Demo4:计算ln(2),保留500位有效数字!" << endl;

prec y; //定义高精度浮点数
y = TMath::ln(2, 500); //求500位有效精度下,2的自然对数。
cout << y << endl;

cout << "Demo5:计算exp(ln(2)),保留500位有效数字!" << endl;
cout << TMath::exp(y, 500) << endl;

cout << "Demo6:计算sqrt(2),保留500位有效数字!" << endl;
cout << TMath::sqrt(2, 500) << endl;
}

//演示:计算pi的值
{
cout << "Demo7:计算pi,保留500位有效数字!" << endl;
cout << TMath::pi_Extract(500) << endl;
}

//演示:prec64,prec128类型,此外还有prec256,prec512,prec1024等类型,用法与此一致。
{
prec64 x;
prec128 y;
x = 1;
x = x / 7;
y = 1;
y = y / 7;
cout << "Demo8:prec64类型,1/7" << endl;
cout << x << endl;
cout << "Demo9:prec128类型,1/7" << endl;
cout << y << endl;
}

{ //结束程序。
cout << "Any key to stop...";
char pp; cin>>pp;
}

return 0;
}

输出

E001 - 高精度计算演示 http://www.orsci.cn
Demo1:计算500的阶乘!
24402736519822201374024775708460938525071486856063856843848271767716907463077639
95210992895004406563726027232954296407168326757444156354400961570410318658570955
81514387866120754592171817254085834909576484982545268861134034654153892212560462
09052884377575789315095542997269887355620752885480676547307949427729557569908769
79191075075980846482122542653968655491431092619954405562029122162376747419062032
71264886597405912779325782331794953914417585385774256356014053034901553682143924
87807886450728452104698917002598371430024974139231362832507181133868476260177124
98493783128253551308963773013187695903550721788011490477880671596952727889810626
12464749813289009764933015189347172414927585036840091873938596204452794390519438
18904356466635138691630171046656415256400468052538157966849034240124154292819589
12232255258291902474459826680339104727701885771184037454867590346029172715141656
71156031747086553777736024079976476940430293521089081532707196834886096025787662
77937632789749393176350090138527306763501109562572800000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000
有效数字长度:1132
Demo2:计算1/7的除法,保留500位有效数字!
1.428571428571428571428571428571428571428571428571428571428571428571428571428571
42857142857142857142857142857142857142857142857142857142857142857142857142857142
85714285714285714285714285714285714285714285714285714285714285714285714285714285
71428571428571428571428571428571428571428571428571428571428571428571428571428571
42857142857142857142857142857142857142857142857142857142857142857142857142857142
85714285714285714285714285714285714285714285714285714285714285714285714285714285
714285714285714285714285714285714e-1
Demo3:计算(1/7) * (1/7),保留500位有效数字!
2.040816326530612244897959183673469387755102040816326530612244897959183673469387
75510204081632653061224489795918367346938775510204081632653061224489795918367346
93877551020408163265306122448979591836734693877551020408163265306122448979591836
73469387755102040816326530612244897959183673469387755102040816326530612244897959
18367346938775510204081632653061224489795918367346938775510204081632653061224489
79591836734693877551020408163265306122448979591836734693877551020408163265306122
44897959183673469387755102040816e-2
Demo4:计算ln(2),保留500位有效数字!
6.931471805599453094172321214581765680755001343602552541206800094933936219696947
15605863326996418687542001481020570685733685520235758130557032670751635075961930
72757082837143519030703862389167347112335011536449795523912047517268157493206515
55247341395258829504530070953263666426541042391578149520437404303855008019441706
41671518644712839968171784546957026271631064546150257207402481637773389638550695
26066834113727387372292895649354702576265209885969320196505855476470330679365443
254763274495125040606943814710469e-1
Demo5:计算exp(ln(2)),保留500位有效数字!
2
Demo6:计算sqrt(2),保留500位有效数字!
1.414213562373095048801688724209698078569671875376948073176679737990732478462107
03885038753432764157273501384623091229702492483605585073721264412149709993583141
32226659275055927557999505011527820605714701095599716059702745345968620147285174
18640889198609552329230484308714321450839762603627995251407989687253396546331808
82964062061525835239505474575028775996172983557522033753185701135437460340849884
71603868999706990048150305440277903164542478230684929369186215805784631115966687
13013015618568987237235289
Demo7:计算pi,保留500位有效数字!
3.141592653589793238462643383279502884197169399375105820974944592307816406286208
99862803482534211706798214808651328230664709384460955058223172535940812848111745
02841027019385211055596446229489549303819644288109756659334461284756482337867831
65271201909145648566923460348610454326648213393607260249141273724587006606315588
17488152092096282925409171536436789259036001133053054882046652138414695194151160
94330572703657595919530921861173819326117931051185480744623799627495673518857527
24891227938183011949129834
Demo8:prec64类型,1/7
1.42857142857142857142857142857142857142857142857142857142857142857142857e-1
Demo9:prec128类型,1/7
1.428571428571428571428571428571428571428571428571428571428571428571428571428571
428571428571428571428571428571428571428571428571428571429e-1

参考 姜维. 《数据分析与数据挖掘》。程序orsci开发包(C++语言)。