Math Alg. Root 数学算法笔记:开根号
生活随笔
收集整理的這篇文章主要介紹了
Math Alg. Root 数学算法笔记:开根号
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
from?開根號(hào)基礎(chǔ)公式 - 百度文庫(kù)
from?開方(數(shù)學(xué)術(shù)語)_百度百科
對(duì)于任意實(shí)數(shù)的開方,可以使用切線法得到其任意精度的結(jié)果,切線法的迭代公式為:
取任意初始值
以上迭代序列將會(huì)收斂:
實(shí)際應(yīng)用中一般取初始值為稍微大?
的實(shí)數(shù),這樣可以加快序列的收斂速度。
e.g. (C)
// 2015-12-24 // By: ChenYu #include "math.h" #include "stdio.h" #define ABS(a) ((a)<0?-(a):(a)) #ifdef _WIN32typedef unsigned __int64 uint64; #elsetypedef unsigned long long uint64; #endif // calculate a approximate value static double calcInitRoot(double x, int n) {const uint64 exptMask=((uint64)1<<11)-1;const uint64 fracMask=((uint64)1<<52)-1;uint64 xInt=*(uint64*)&x;int xExpt=(int)((xInt>>52)&exptMask)-1023;xInt=((uint64)((xExpt+1024*n-1)/n)<<52)+(xInt&fracMask)/n;return *(double*)&xInt; }double calcRoot(double x, int n) {int i, j, s=1-((x<0)<<(n&1));double a=ABS(x);double x1, x0=calcInitRoot(a, n);double err=x0*1e-14;if(x==0)return 0;for(i=1; i<50; i++){double xn=1;for(j=0; j<n-1; j++)xn*=x0;x1=((n-1)*x0*xn+a)/(xn*n);// printf("x%d=%.14f\n", i, x1);if(ABS(x1-x0)<=err)break;x0=x1;}return s*x1; }void main() {double x=-31141.592653589793;int n=11;double y=calcRoot(x, n);printf("root(%g,%d)=%+.14f\n", x, n, y);printf("root(%g,%d)=%+.14f\n", x, n, pow(ABS(x), 1.0/n)); }總結(jié)
以上是生活随笔為你收集整理的Math Alg. Root 数学算法笔记:开根号的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习日记day16 ps
- 下一篇: C语言文件英文,c语言文件操作(国外英文