作者:乔山办公网日期:
返回目录:excel表格制作
2²=4<5
2.5²=6.25>5
[(2+2.5)/2]²=2.25²=5.0625>5
[(2.25+2)/2]²=2.125²=4.515625<5
[(2.125+2.25)/2]²=2.1875=4.78515625<5
[2.1875+2.25)/2]²=2.21875²≈知4.92285<5
[(2.21875+2.25)/2]²=2.234375²≈4.9924316<5
[(2.234375+2.25)/2]²=2.2421875²≈5.0274>5
[(2.234375+2.2421875)/2]²=2.23783125²≈5.00788>0
[(2.234375+2.23783125)/2]²=2.2361031²≈5.00157>0
[(2.234375+2.2361031)/2]²=2.235253²≈4.996366<0
∴√5在道(2.235253,2.2361031)之间
∴√5≈2.24
x^2 = A ( A 是常数 )
求x.
大概思想是设 x = ∑(i=0->+∞) a(i) * 10^(-i)
x^2
= ( ∑(i=0->+∞) a(i) * 10^(-i) )^2
= ∑(i=0->+∞) ∑(j=0->i) a(j)a(i-j) * 10^(-i)
一步一步求a(i)
精度也很好e799bee5baa6e58685e5aeb9362控制, 毕竟是10进制展开的.只要0<=a(i+1)<10就行.
#include <math.h>
#include <malloc.h>
#include <memory.h>
#include <stdio.h>
typedef unsigned char U8;
typedef unsigned short U16;
typedef unsigned long U32;
#define LEN_QUEST 100 /* precision */
#define A 2 /* A^0.5 */
#define LEN (LEN_QUEST+1)
U32 InvMpy ( U32 *a, U32 m, U32 n ) /* res = a(m)a(n) + a(m+1)a(n-1) + ... + a(n)a(m) */
{
U32 i;
U32 res;
if ( n < m )
return 0;
if ( n == m )
return ( a[m] * a[m] );
res = 0;
for ( i = 0; i <= n; i++ )
{
res += a[m+i] * a[n-i];
}
return res;
}
U32 Calc ( U32 *res, U32 rest, U32 precision )
{
U32 tmp;
U32 max;
U32 isRight = 0;
rest = 10 * rest;
tmp = InvMpy ( res, 1, precision-1 );
if ( tmp > rest )
return 0;
if ( precision > LEN && tmp != rest )
return 1;
max = ( rest - tmp ) >> 1;
if ( max > 9 )
max = 9;
for ( int i = max; i >= 0; i-- )
{
res[precision] = i;
tmp = InvMpy ( res, 0, precision );
if ( tmp > rest )
continue;
tmp = rest - tmp;
tmp = Calc ( res, tmp, precision+1 );
if ( tmp != 0 )
{
isRight = 1;
break;
}
}
return isRight;
}
void main()
{
U32 *res;
U32 rest;
res = (U32*) malloc ( 2* sizeof(U32) * LEN );
memset ( res, 0xFF, 2* sizeof(U32) * LEN );
res[0] = (U32)sqrt(1.0L*A);
rest = A - res[0] * res[0];
Calc ( res, rest, 1 );
printf( "%d.",res[0] );
for ( U32 i = 1; i < LEN; i++ )
{
printf ( "%d",res[i] );
}
printf ( "\n" );
printf ( "The %dth is %d\n", LEN_QUEST, res[LEN_QUEST] );
free ( res );
}
输出结果是
1.4142048801688724209698078569677307324784621070388503875343276415728
The 100th is 8
根号2的根号2次方= 1.63252691943815
double 数最大精百度度只有20位
要精度达30位需自己创知建大数。
下面用迭道代专法和库函数分属别计算根号2。
#include <math.h>
void main()
{
int i;
double x1,x0=1;
long double y;
for( i=0;i<1000;i++)
{
x1=x0-(x0*x0-2.)/(2.*x0);
x0=x1;
}
printf("%.30lf\n",x0);
y = sqrt(2.0l);
printf("%.30lf",y);
}
1.4142900000000000000 -- 迭代
1.4142100000000000000 -- 库
1.41420488016887242097 -- 目标