High precision float c++

博客围绕C++中高精度浮点数展开,虽未给出具体内容,但推测会涉及在C++里实现高精度浮点数的相关技术,如算法、数据结构运用等,属于后端开发中C++编程的信息技术范畴。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//HugeInteger.cpp
#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
#define maxlen 10000
class HI
{
    public:
    friend ostream& operator<<(ostream &cout,const HI& x);

    public:
    HI() {(*this)=0;}
    HI(int inte) {(*this)=inte;}
    HI(const char* str) {(*this)=str;}
    HI operator=(int inte);
    HI operator=(const char* str);
    HI operator+(const HI&b);
    HI operator-(const HI&b);
    HI operator*(const HI&b);
    HI operator/(const HI&b);
    HI operator%(const HI&b);
    int CP(const HI&b);

    int len;
    int s[maxlen];
};

ostream& operator<<(ostream& cout,const HI& x){
    for(int i=x.len;i>=1;i--)
        cout<<x.s[i];
    return cout;
}
HI HI::operator=(int inte){
    if(inte==0){
        len=1;
        s[len]=0;
        return (*this);
    }
    else{
        for(len=0;inte>0;){
            s[++len]=inte%10;
            inte/=10;
        }
        return *this;
    }
}
HI HI::operator=(const char* str){
    len=strlen(str);
    for(int i=1;i<=len;i++){
        s[i]=str[len-i]-'0';
    }
    return (*this);
}
HI HI::operator*(const HI&x){
    HI temp;
    temp.len=len+x.len;
    int i,j;
    for(i=1;i<=temp.len;i++)
        temp.s[i]=0;
    for(i=1;i<=x.len;i++)
        for(j=1;j<=len;j++){
            temp.s[i+j-1]+=x.s[i]*s[j];
        }
    for(i=1;i<temp.len;i++){
        temp.s[i+1]+=temp.s[i]/10;
        temp.s[i]%=10;
    }
    while(i>1&&!temp.s[i]){
        i--;
    }
    temp.len=i;
    //cout<<"i="<<i<<endl;
    return temp;
}//19.3.17
HI HI::operator+(const HI&x){
    HI temp;
    int i=1;
    for(i=1;i<=x.len;i++)
        temp.s[i] += x.s[i];
    for(i=1;i<=len;i++)
        temp.s[i] += s[i];
    for(i=1;i<=len-1||i<=x.len-1;i++){
        temp.s[i+1]+=temp.s[i]/10;
        temp.s[i]%=10;
        temp.len=i+1;
    }
    if(temp.s[i]>=10){
        temp.len++;
        temp.s[i+1]=1;
        temp.s[i]/=10;
    }
    return temp;
}
int HI::CP(const HI&x){
    if(len>x.len)
        return 1;
    else if(len<x.len)
        return -1;
    else{
        for(int i=len;i>=1;i++){
            if(s[i]==x.s[i])
                continue;
            else
                return s[i]>x.s[i]? 1:-1;
        }
        return 0;
    }
}
HI HI::operator-(const HI&x){
    int i,j=0;
    HI temp(0);
    for(i=1;i<=len;i++){
        temp.s[i]=s[i]-j;
        if(i<=x.len)
            temp.s[i]-=x.s[i];
        if(temp.s[i]<0){
            temp.s[i]+=10;
            j=1;
        } 
        else
            j=0;
    }
    temp.len=len;
    while(temp.len>1&&!temp.s[temp.len]){
        temp.len--;
    }
    return temp;
}
HI HI::operator/(const HI&x){
    int i,j;
    HI temp(0),c(0);
    for(i=len;i>0;i--){
        if(!(temp.len==1&&temp.s[1]==0)){
            for(j=temp.len;j>0;j--){
                temp.s[j+1]=temp.s[j];
            }
            temp.len++;
        }
        temp.s[1]=s[i];
        c.s[i]=0;
        while((j=temp.CP(x))>=0){
            c.s[i]++;
            temp=temp-x;
            if(j==0)
                break;
        }
    }
    c.len=len;
    while((c.len>1)&&!(c.s[c.len])){
        c.len--;
    }
    return c;
}
HI HI::operator%(const HI&x){
    HI c(0),b(0);
    c=(*this)/x;
    b=(*this)-c*x;
    return b;
}
//HPFloat.cpp
#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
#include "HugeInteger.cpp"

class HPF{
    public:
    friend ostream& operator<<(ostream &cout,const HPF&x);

    public:
    HPF(){(*this)=0;};
    HPF(int x){(*this)=x;}
    HPF(char *s){(*this)=s;}
    HPF operator=(int x);
    HPF operator=(char *s);
    HPF operator+(HPF& x);
    HPF operator-(HPF& x);
    HPF operator*(HPF& x);
    HPF operator/(HPF& x);

    int fp,sign;
    HI num;
};

ostream& operator<<(ostream &cout,const HPF&x){
    if(x.sign<0) cout<<'-';
    int i;
    int gap=x.fp-x.num.len;
    int k=0;
    if(gap>=0){
        cout<<"0.";
        for(k=0;k<gap;k++) cout<<0;
        cout<<x.num;
    }
    for(i=x.num.len;i>x.fp;i--) cout<<x.num.s[i];
    cout<<'.';
    for(i=x.fp;i>0;i--) cout<<x.num.s[i];
    cout<<endl;
    return cout;
}
HPF HPF::operator=(int x){
    fp=0;
    if(x<0){
        sign=-1;
        num=-x;
    }
    else{
        sign =1;
        num=x;
    }
    return (*this);
}
HPF HPF::operator=(char *s){
    if(s[0]=='-') sign =-1;
    else sign=1;
    fp=0;
    int i,j,l;
    l=strlen(s);
    for(i=1;i<l;i++){
        if(s[i]=='.') fp=l-1-i;
    }
    int k=1;
    for(j=l-1;j>=0;j--){
    	if(s[j]=='-'){
    		continue;
    	}
        if(s[j]!='.'){
            num.s[k++]=s[j]-'0';
            num.len++;
            //cout<<"recrease"<<endl;
        }
    }
    while(num.len>1&&!(num.s[num.len])) num.len--;
    return (*this);
}
void left(HPF& x,int n){
    x.fp+=n;
    x.num.len+=n;
    int i=0;
    for(i=x.num.len;i>n;i--){
        x.num.s[i]=x.num.s[i-n];
    }
    while(i>0){
        x.num.s[i]=0;
        i--;
    }
}
void right(HPF& x,int n){
    x.fp-=n;
    x.num.len-=n;
    int i;
    for(i=1;i<=x.num.len;i++){
        x.num.s[i]=x.num.s[i+n];
    }
}
HPF HPF::operator+(HPF& x){
    HPF temp;
    if(fp>x.fp){
        left(x,fp-x.fp);
    }
    else{
        left((*this),x.fp-fp);
    }
    if(sign==x.sign){
        temp.sign=sign;
        temp.num=x.num+num;
        temp.fp=fp;
    }
    else{
        temp.fp=fp;
        if(num.CP(x.num)>0){
            temp.sign=sign;
            temp.num=num-x.num;
        }
        else if(num.CP(x.num)<0){
            temp.sign=x.sign;
            temp.num=x.num-num;
        }
        else{
            temp.fp=0;
            temp.num=0;
            temp.sign=1;
        }
    }
    return temp;
}
HPF HPF::operator-(HPF& x){
    x.sign=-x.sign;
    HPF temp =(*this)+x;
    x.sign*=-1;
    return temp;
}
HPF HPF::operator*(HPF& x){
    HPF temp;
    temp.sign=x.sign*sign;
    temp.fp=fp+x.fp;
    temp.num=num*x.num;
    if(temp.num.CP(0)==0){
        temp.fp=0;
        temp.num=0;
        temp.sign=1;
    }
    return temp;
}
HPF HPF::operator/(HPF& x){
    HPF temp;
    HPF t=(*this);
    temp.sign=x.sign*sign;
    left(t,100+x.fp);
    temp.num=t.num/x.num;
    temp.fp=100;
    return temp;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值