⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 means.cpp

📁 利用数值分析方法
💻 CPP
字号:
#include "means.h"
using namespace std;


long double OP_exp(long double y,int digit)  //计算e^x,实现系统的exp()功能 
{
	long double error,result;
	error=1.0,result=0.0;
	for(int i=0;i<digit;i++)
		error=error/10;
	error=error/2;
	if(y==0) 
	{
		result=1;
		return result;
	}
	if(y<0) 
		return 1/OP_exp(-y,digit); 
	long double l=0.0,extra=1.0,temp=1.0;		
    int k;
	for(k=1;extra>error || extra<-error;k++)
    {		
		temp*=y;
		l=k;
		temp=temp/l;
		result+=temp;
		extra=(1+result)*temp*y/(l+1);
	}	    	
		return result+1;
}

TLargeFloat OP_ln(long double x,int digit)   //求ln(x)
{
	TLargeFloat::TDigits sCount(2*digit);
    TLargeFloat error(1.0,sCount),result(0.0,sCount);
	
	for(int i=0;i<2*digit;i++)
		error=error/10;
	error=error/2;
    if(x==1)
	{
		result=0;
		return result;
	}
    else if(x>1.5)
		return -OP_ln(1/x, digit);
	else if(x<0.1)
	{		
		long double n,a;
		n=-1;
		do
		{
			n=n-0.5;
			a=x/OP_exp(n,digit);
		}
		while(a>2 || a<1);
		return OP_ln(a, digit)+n;
	}
	TLargeFloat y(0.0,sCount),l(0.0,sCount);
    TLargeFloat extra(1.0,sCount);
    TLargeFloat temp(1.0,sCount);	
	y=x-1;
    int k;
	for(k=1;extra>error || extra<-error;k++)
    {		
		temp*=y;
		l=k;
		if(k==1)			
			temp=temp/l;	
		else
			temp=temp/(-l);
		result+=temp;         
        temp*=l;
		extra=-temp*y/(l+1);
	}
	    cout<<"\n利用泰勒展开法计算,展开的项数为: "<<k<<endl;		
	    return result;
}

TLargeFloat OP_Power(TLargeFloat x, int n,int digit)	//计算x的n次方
{
	TLargeFloat::TDigits sCount(4*digit);
	TLargeFloat power(0.0,sCount);	
	 power=1.0;
	while(n>0)
	{
		while(!(n%2))
		{
			n/=2;
			x*=x;
		}
		--n;
		power*=x;
	}	
	return power;
}


TLargeFloat OP_PowerRoot(long double a,int n,int digit)     //求x^(1/n)
{
	TLargeFloat::TDigits sCount(4*digit);
    TLargeFloat result(0.0,sCount),init(0.0,sCount),error(1.0,sCount),in(0.0,sCount);
	in=n;
	int count=0;
	for(int i=0;i<digit;i++)
		error=error/10;
	error=error/2;	
	if(a<5 || n>100)
		result=1;//根据输入决定初始点的选择以加快收敛速度
	else
		result=a;
    
	if(n==1)
	{
		result = a;
		return result;
	}
	TLargeFloat RNow(0.0,sCount), Value(0.0,sCount);	//暂存值
	do{
		count++;		
		init = result;    	
		RNow = OP_Power(init, n-1,digit);	
		result=init*(1.0-1.0/in)+a/(in*RNow);	
	//	Debug_toCout("result==",result);
	}
	while(result-init >error  || result-init <-error );
	cout<<"\n利用牛顿迭代法计算,迭代次数为: "<<count<<"次"<<endl;
	return result;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -