全站数据
8 4 2 0 5 8 1

程序精度处理怎么做

考研我志气满满 | 简单学习,快乐成才!         
问题更新日期:2024-10-21 06:15:23

问题描述

程序精度处理怎么做急求答案,帮忙回答下
精选答案
最佳答案

#include

"high_precision_t.h

"

int main(int argc, char* argv[]){High_Precision_T a(

"123.01234567890456789

");High_Precision_T b(

"-789.01234567890123456

");

//cin>>a>>b;cout<<

"a=

"<<a<<

"

b=

"<<b<<endl;

High_Precision_T c=a+b;cout<<

"a+b=

"<<c<<endl;

c=a-b;cout<<

"a-b=

"<<c<<endl;

c=a*b;cout<<

"a*b=

"<<c<<endl;

return 0;}

//high_precision_t.h

#ifndef high_precision_tH#define high_precision_tH#include <iostream>#include <sstream.h>

class High_Precision_T{private: unsigned digits[20];unsigned decpt;char sign;

void StringToDigits(string str); public:High_Precision_T();

High_Precision_T(double d);

High_Precision_T(string d);

operator double();operator string();

High_Precision_T operator+(const High_Precision_T& op);

High_Precision_T operator-(const High_Precision_T& op);

High_Precision_T operator*(const High_Precision_T& op);

friend istream& operator >>(istream & is, High_Precision_T& op){string str;is >> str;op=High_Precision_T(str);return is;}

friend ostream& operator <<(ostream & os, High_Precision_T& op){os << string(op);return os;}

};

#endif

//high_precision_t.cpp

High_Precision_T::High_Precision_T(){ memset(digits,0,20*sizeof(unsigned)); sign='+'; decpt=1;}

High_Precision_T::High_Precision_T(double data){stringstream ss;string str;ss<<data;ss>>str;

StringToDigits(str);}

High_Precision_T::High_Precision_T(string str){StringToDigits(str);}

void High_Precision_T::StringToDigits(string str){memset(digits,0,20*sizeof(unsigned));sign='+';decpt=1;

//过滤串前非数字字符int pos;for(pos=0;pos<str.length();pos++){if(str[pos]=='+'||str[pos]=='-'||str[pos]=='.'||(str[pos]>='0'&& str[pos]<='9')){break;}}str.erase(0,pos);

//处理符号位if(str[0]=='+' ||str[0]=='-'){//首位是符号位sign=str[0];str.erase(0,1);}else{sign='+';}

//过滤串后非数字字符for(pos=0;pos<str.length();pos++){if(str[pos]!='.'&& !(str[pos]>='0'&& str[pos]<='9')){break;}}str.erase(pos);

//小数点前重复的零//pos=str.find_first_of('.');while(str[0]=='0'){str.erase(0,1);}

//处理小数点pos=str.find_first_of('.');if(pos>=0){str.erase(pos,1);decpt=pos;

//还有小数点?pos=str.find_first_of('.');if(pos>=0)str.erase(pos);}

//合法数字for(unsigned i=0;i<str.length()&& i<20 ;++i){digits[i]= str[i]-48;}

}

High_Precision_T::operator double(){return 0.0;}

High_Precision_T::operator string(){stringstream ss;string str;if(sign=='-')ss<<'-';for(int i=0;i<decpt;i++)ss<<digits[i];ss<<'.';for(int i=decpt;i<20;i++)ss<<digits[i];ss>>str;return str;}//---------------------------------------------------------------------------High_Precision_T High_Precision_T::operator+(const High_Precision_T &op){

//辅助变量int int_len=decpt>=op.decpt decpt:op.decpt;//和的整数长度int dec_len=decpt>=op.decpt 20-op.decpt:20-decpt;//和的小数长度

//临时空间int *temp_digits=new int[int_len+dec_len+1];// temp_digits[0]为进位。memset(temp_digits,0,(int_len+dec_len+1)*sizeof(int));int temp_decpt=int_len+1;//int temp_sign='+'; //默认为正数

int dif=temp_decpt-this->decpt;//小数点对齐后,数组下标的差值int opdif=temp_decpt-op.decpt;

int augend;int addend;

//从最低位开始相加for(int i=int_len+dec_len;i>0;i--){//被加数augend=((i-dif)>-1 && (i-dif)<20) this->digits[i-dif]:0;(this->sign=='+')temp_digits[i]+=augend: temp_digits[i]-=augend;

//加数addend=((i-opdif)>-1 && (i-opdif)<20) op.digits[i-opdif]:0;(op.sign=='+') temp_digits[i]+=addend:temp_digits[i]-=addend;

//确保每位为0~9内的非负值while(temp_digits[i]<0){//处理借位temp_digits[i-1]-=1;//借位temp_digits[i]+=10;}if(temp_digits[i]>9){ //处理进位temp_digits[i-1]+=1;//进位temp_digits[i]-=10;}}

if(temp_digits[0]<0){//进位为负,则整个数为负数temp_sign='-';for(int i=int_len+dec_len;i>0;i--){temp_digits[i] =-temp_digits[i];if(temp_digits[i]<0){temp_digits[i-1]+=1;//借位temp_digits[i]+=10;}}temp_digits[0]=-temp_digits[0];}//

High_Precision_T temp;if(temp_digits[0]>0 && temp_decpt<20){ //未处理溢出。temp_decpt>20 有溢出temp.sign=temp_sign;temp.decpt=temp_decpt;memcpy(temp.digits,temp_digits,20*sizeof(int));}else{temp.sign=temp_sign;temp.decpt=temp_decpt-1;memcpy(temp.digits,&temp_digits,20*sizeof(int));}

delete[] temp_digits;return temp;}

High_Precision_T High_Precision_T::operator-(const High_Precision_T& op){High_Precision_T temp=op;temp.sign=op.sign=='+' '-':'+';return *this+temp;}

High_Precision_T High_Precision_T::operator*(const High_Precision_T& op){

//辅助空间int temp_digits[41];// temp_digits[0]为进位。memset(temp_digits,0,41*sizeof(int));int temp_decpt=decpt+op.decpt+1;//

int k=0;for(int i=19;i>=0;i--){for(int j=19;j>=0;j--){k=i+j+2;temp_digits[k] += digits[i]*op.digits[j];//temp_digits[k-1] += temp_digits[k]/10;temp_digits[k] %=10;}}

High_Precision_T temp;temp.sign= (sign==op.sign) '+':'-';

k=0;while(k<40){if(temp_digits[k]!=0)break;k++;}if(k>0){temp.decpt=temp_decpt-k;memcpy(temp.digits,&temp_digits[k],20*sizeof(int));}return temp;

其他回答

程序精度处理可以通过以下方法进行:

1. 选择合适的算法和数据类型:根据应用场景和需求,选择能够保证精度的算法和数据类型。

2. 数值稳定:在计算过程中,要注意数值稳定性,避免由于误差的传递导致精度损失。

3. 适当保留小数位数:对于需要高精度的计算结果,可以适当地保留小数位数。

4. 采用浮点数:对于需要进行浮点数运算的程序,要尽可能使用浮点数类型,以便得到更精确的结果。

5. 采用模运算:在需要高精度计算的情况下,可以采取模运算来代替除法运算,从而避免精度损失。

6. 进行精度控制:在进行计算时,可以采用精度控制的方法,例如舍入操作、四舍五入等,以确保得到预期的结果。

7. 避免直接运算:如果需要计算高精度数值,尽可能避免直接运算,可以使用高精度库或者第三方工具来实现高精度计算。

其他回答

程序精度处理主要是为了避免计算过程中出现舍入误差,导致最终结果偏差过大。具体做法包括使用高精度计算库、增加计算精度、避免连续相加或相减等操作、避免除数为0等情况。同时,在编写代码时应养成良好的习惯,如不直接使用浮点数进行比较、不进行不必要的类型转换等,以提高程序的精度和稳定性。

其他回答

程序精度处理是指在计算机程序中对计算数值进行精确处理的方法。在计算机中,浮点数的表示和计算都存在一定的误差,而在某些场景下,这种误差可能会对计算结果产生较大的影响。因此,程序精度处理需要通过调整计算方式、使用高精度数据类型等手段,尽可能减小误差,确保计算结果的准确性。常见的精度处理方法包括使用BigDecimal类、避免浮点数的除法运算、将小数转化为整数等。