User defined floating point data type
Here, I am giving a user defined data-type class Double. this does not require any underlying floating point data-type as it works on the String data type
/*
hi,
check this out.
this program does not implement any underlying floating point data type.
this program just only works on the basic datatypes int_64, char and string
so it will avoid the different answers on different platform
*/
/* using string to store a double value */
//keeping significand and exponent in direct decimal format
#include <string>
#include <cstring>
#include <sstream>
#include <iostream>
using namespace std;
class Decimal
{
char sign;
string significand;
int64_t exponent;
char ch;
string str;
public:
void print() //check how to overload <<
{
int64_t val=0;
string str="";
string str1="";
for(int64_t i=0;i<=exponent;i++)
{
ch=significand.at(i);
val=10*val + (ch-48);
}
for(int64_t i=exponent+1;i<significand.length();i++)
{
str1=significand.at(i);
str.append(str1);
}
if(sign=='+')
{
if(str.length()>0)
cout<<val<<"."<<str;
else
cout<<val;
}
else
{
if(str.length()>0)
cout<<"-"<<val<<"."<<str;
else
cout<<"-"<<val;
}
}//end print function
int64_t p(int64_t n,int64_t m)
{
int64_t res=1;
for(int64_t i=0;i<m;i++)
res=res*n;
return res;
}
int64_t to_int(string str)
{
int64_t val=0;
char ch;
for(int64_t i=0;i<str.length();i++)
{
ch=str.at(i);
val=10*val + (ch-48);
}
return val;
}
string to_string(int64_t n)
{
int64_t temp=n;
string temp1;
while(temp>0)
{
ch=(temp%10)+48;
str=ch;
temp1.insert(0,str);
temp=temp/10;
}
return temp1;
}
int64_t to_decimal(string str)
{
int64_t a;
int64_t sum=0;
for(int64_t i=0;i<str.length();i++)
{
a= (str.at(i))-48;
sum+=(a*p(2,i));
}
return sum;
}
void str_to_Decimal(string num)
{
int64_t zeros_just_after_decimal_point=0;
int64_t i=0;
if(num[0]!='-')
{
ch=num[0];
sign='+';
if(num[0]=='+')
{
sign='+';
ch=num[1];
i++;
}
}
else
{
sign='-';
ch=num[1];
i++;
}
int64_t a=0;
int64_t b=0;
while(ch!='.' && ch!='\0')
{
a= a*10+(ch-48);
ch=num[++i];
}
if(ch!='\0')
{
ch=num[++i];
while(ch!='\0')
{
b=b*10+(ch-48);
if(b==0)
zeros_just_after_decimal_point++;
ch=num[++i];
}
}
string temp1,temp2,str;
int64_t temp=a,exp=0;
while(temp>0)
{
ch=(temp%10)+48;
exp++;
str=ch;
temp1.insert(0,str);
temp=temp/10;
}
exponent=exp-1;
temp=b;
while(temp>0)
{
ch=(temp%10)+48;
exp++;
str=ch;
temp2.insert(0,str);
temp=temp/10;
}
significand="";
significand.append(temp1);
for(i=0;i<zeros_just_after_decimal_point;i++)
{
ch='0';str=ch;
significand.append(str);
}
significand.append(temp2);
}
Decimal operator+(Decimal d)
{
/* this addition rounds to the exponent which is having greater value of exponent*/
Decimal res;
res=Decimal();
int64_t val1=0,val2=0;
//case 1 (if both signs are positive)
if(sign=='+' && d.sign=='+')
{
res.sign='+';
int64_t diff=0;
if(exponent>=(d.exponent))
{
res.exponent=exponent;
diff=exponent-(d.exponent);
d.exponent=exponent;
for(int64_t i=0;i<diff;i++)
{
ch='0';str=ch;
d.significand.insert(0,str);
}
}//if exponent>d.exponent
else
{
res.exponent=d.exponent;
diff=(d.exponent)-exponent;
exponent=d.exponent;
for(int64_t i=0;i<diff;i++)
{
ch='0';str=ch;
significand.insert(0,str);
}
}//else exponent>d.exponent
for(int64_t i=0;i<=res.exponent;i++)
{
ch=significand.at(i);
val1=10*val1 + (ch-48);
ch=d.significand.at(i);
val2=10*val2 + (ch-48);
}
int64_t val=val1+val2;
int64_t temp;
string temp1,temp2;
temp=val;
while(temp>0)
{
ch=(temp%10)+48;
str=ch;
temp1.insert(0,str);
temp=temp/10;
}
val1=0; val2=0;
if(significand.length()<=d.significand.length())
{
for(int64_t i=res.exponent+1;i<significand.length();i++)
{
ch=significand.at(i);
val1=10*val1 + (ch-48);
ch=d.significand.at(i);
val2=10*val2 + (ch-48);
}
val=val1+val2;
temp=val;
int64_t exp=0;
while(temp>0)
{
ch=(temp%10)+48;
exp++;
str=ch;
temp2.insert(0,str);
temp=temp/10;
}
if(exp>(significand.length()-exponent))
{
val=(to_int(temp1))+1;
temp1=to_string(val);
temp2.erase(0,1);
}
res.significand=temp1+temp2;
}//if significand
else
{
for(int64_t i=res.exponent+1;i<d.significand.length();i++)
{
ch=significand.at(i);
val1=10*val1 + (ch-48);
ch=d.significand.at(i);
val2=10*val2 + (ch-48);
}
val=val1+val2;
temp=val;
int64_t exp=0;
while(temp>0)
{
ch=(temp%10)+48;
exp++;
str=ch;
temp2.insert(0,str);
temp=temp/10;
}
if(exp>(d.significand.length()-res.exponent))
{
val=(to_int(temp1))+1;
temp1=to_string(val);
temp2.erase(0,1);
}
res.significand=temp1+temp2;
}//else significand
}//if sign
return res;
}
};// class Decimal
int main()
{
int64_t a,b,z;
char ch='c';
string str1,str2;
Decimal obj1,obj2,obj3;
obj1=Decimal();
obj2=Decimal();obj3=Decimal();
do{
cout<<"\n welcome!!! please enter two positive floating point numbers. \n";
cin>>str1;
cin>>str2;
obj1.str_to_Decimal(str1);
obj2.str_to_Decimal(str2);
//obj3.str_to_Decimal("-1831564.165");
obj3=obj1+obj2;
cout<<"\n";
obj1.print();
cout<<"+";
obj2.print();
cout<<"= ";
obj3.print();
cout<<"\n \n press e to exit and c to continue";
cin>>ch;
}while(ch=='c');
return 0;
}
/*
hi,
check this out.
this program does not implement any underlying floating point data type.
this program just only works on the basic datatypes int_64, char and string
so it will avoid the different answers on different platform
*/
/* using string to store a double value */
//keeping significand and exponent in direct decimal format
#include <string>
#include <cstring>
#include <sstream>
#include <iostream>
using namespace std;
class Decimal
{
char sign;
string significand;
int64_t exponent;
char ch;
string str;
public:
void print() //check how to overload <<
{
int64_t val=0;
string str="";
string str1="";
for(int64_t i=0;i<=exponent;i++)
{
ch=significand.at(i);
val=10*val + (ch-48);
}
for(int64_t i=exponent+1;i<significand.length();i++)
{
str1=significand.at(i);
str.append(str1);
}
if(sign=='+')
{
if(str.length()>0)
cout<<val<<"."<<str;
else
cout<<val;
}
else
{
if(str.length()>0)
cout<<"-"<<val<<"."<<str;
else
cout<<"-"<<val;
}
}//end print function
int64_t p(int64_t n,int64_t m)
{
int64_t res=1;
for(int64_t i=0;i<m;i++)
res=res*n;
return res;
}
int64_t to_int(string str)
{
int64_t val=0;
char ch;
for(int64_t i=0;i<str.length();i++)
{
ch=str.at(i);
val=10*val + (ch-48);
}
return val;
}
string to_string(int64_t n)
{
int64_t temp=n;
string temp1;
while(temp>0)
{
ch=(temp%10)+48;
str=ch;
temp1.insert(0,str);
temp=temp/10;
}
return temp1;
}
int64_t to_decimal(string str)
{
int64_t a;
int64_t sum=0;
for(int64_t i=0;i<str.length();i++)
{
a= (str.at(i))-48;
sum+=(a*p(2,i));
}
return sum;
}
void str_to_Decimal(string num)
{
int64_t zeros_just_after_decimal_point=0;
int64_t i=0;
if(num[0]!='-')
{
ch=num[0];
sign='+';
if(num[0]=='+')
{
sign='+';
ch=num[1];
i++;
}
}
else
{
sign='-';
ch=num[1];
i++;
}
int64_t a=0;
int64_t b=0;
while(ch!='.' && ch!='\0')
{
a= a*10+(ch-48);
ch=num[++i];
}
if(ch!='\0')
{
ch=num[++i];
while(ch!='\0')
{
b=b*10+(ch-48);
if(b==0)
zeros_just_after_decimal_point++;
ch=num[++i];
}
}
string temp1,temp2,str;
int64_t temp=a,exp=0;
while(temp>0)
{
ch=(temp%10)+48;
exp++;
str=ch;
temp1.insert(0,str);
temp=temp/10;
}
exponent=exp-1;
temp=b;
while(temp>0)
{
ch=(temp%10)+48;
exp++;
str=ch;
temp2.insert(0,str);
temp=temp/10;
}
significand="";
significand.append(temp1);
for(i=0;i<zeros_just_after_decimal_point;i++)
{
ch='0';str=ch;
significand.append(str);
}
significand.append(temp2);
}
Decimal operator+(Decimal d)
{
/* this addition rounds to the exponent which is having greater value of exponent*/
Decimal res;
res=Decimal();
int64_t val1=0,val2=0;
//case 1 (if both signs are positive)
if(sign=='+' && d.sign=='+')
{
res.sign='+';
int64_t diff=0;
if(exponent>=(d.exponent))
{
res.exponent=exponent;
diff=exponent-(d.exponent);
d.exponent=exponent;
for(int64_t i=0;i<diff;i++)
{
ch='0';str=ch;
d.significand.insert(0,str);
}
}//if exponent>d.exponent
else
{
res.exponent=d.exponent;
diff=(d.exponent)-exponent;
exponent=d.exponent;
for(int64_t i=0;i<diff;i++)
{
ch='0';str=ch;
significand.insert(0,str);
}
}//else exponent>d.exponent
for(int64_t i=0;i<=res.exponent;i++)
{
ch=significand.at(i);
val1=10*val1 + (ch-48);
ch=d.significand.at(i);
val2=10*val2 + (ch-48);
}
int64_t val=val1+val2;
int64_t temp;
string temp1,temp2;
temp=val;
while(temp>0)
{
ch=(temp%10)+48;
str=ch;
temp1.insert(0,str);
temp=temp/10;
}
val1=0; val2=0;
if(significand.length()<=d.significand.length())
{
for(int64_t i=res.exponent+1;i<significand.length();i++)
{
ch=significand.at(i);
val1=10*val1 + (ch-48);
ch=d.significand.at(i);
val2=10*val2 + (ch-48);
}
val=val1+val2;
temp=val;
int64_t exp=0;
while(temp>0)
{
ch=(temp%10)+48;
exp++;
str=ch;
temp2.insert(0,str);
temp=temp/10;
}
if(exp>(significand.length()-exponent))
{
val=(to_int(temp1))+1;
temp1=to_string(val);
temp2.erase(0,1);
}
res.significand=temp1+temp2;
}//if significand
else
{
for(int64_t i=res.exponent+1;i<d.significand.length();i++)
{
ch=significand.at(i);
val1=10*val1 + (ch-48);
ch=d.significand.at(i);
val2=10*val2 + (ch-48);
}
val=val1+val2;
temp=val;
int64_t exp=0;
while(temp>0)
{
ch=(temp%10)+48;
exp++;
str=ch;
temp2.insert(0,str);
temp=temp/10;
}
if(exp>(d.significand.length()-res.exponent))
{
val=(to_int(temp1))+1;
temp1=to_string(val);
temp2.erase(0,1);
}
res.significand=temp1+temp2;
}//else significand
}//if sign
return res;
}
};// class Decimal
int main()
{
int64_t a,b,z;
char ch='c';
string str1,str2;
Decimal obj1,obj2,obj3;
obj1=Decimal();
obj2=Decimal();obj3=Decimal();
do{
cout<<"\n welcome!!! please enter two positive floating point numbers. \n";
cin>>str1;
cin>>str2;
obj1.str_to_Decimal(str1);
obj2.str_to_Decimal(str2);
//obj3.str_to_Decimal("-1831564.165");
obj3=obj1+obj2;
cout<<"\n";
obj1.print();
cout<<"+";
obj2.print();
cout<<"= ";
obj3.print();
cout<<"\n \n press e to exit and c to continue";
cin>>ch;
}while(ch=='c');
return 0;
}
Comments
Post a Comment