2つの整数で浮動小数点数演算をシミュレートする
1つは底数を行い、1つは指数を行い、加減乗除演算を実現する.
注意精度、既に数が0の場合.
注意精度、既に数が0の場合.
#include <stdio.h>
#include <malloc.h>
#include <iostream>
using namespace std;
/*
int 4
-2147483648 ~ 2147483647
10 , 9
*/
double pow10(int exp) //10
{
return pow(double(10), double(exp));
}
class newDouble
{
public:
newDouble(void){bot=0; exp=0;};
newDouble( double d )
{
bot =0;
exp =0;
int flag = d>0?1:-1; //
d = abs(d); //
while( d< pow10(8) && d!=0 ) // 8
{
d *= 10;
exp--;
}
bot = flag*(int)(d);
}
newDouble operator+( newDouble b )
{
(*this).SpecifyPrecision(7); // 7 ,
b.SpecifyPrecision(7);
int dis = exp - b.exp;
newDouble c;
if( dis >0 )
{
c.bot = (int)(bot*pow((double)10,(double)dis)) + b.bot;
c.exp = b.exp;
}
else
{
c.bot = bot + (int)(b.bot*pow((double)10,(double)dis));
c.exp = exp;
}
return c;
}
newDouble operator-( newDouble b )
{
(*this).SpecifyPrecision(7); // 7 ,
b.SpecifyPrecision(7);
int dis = exp - b.exp;
newDouble c;
if( dis >0 )
{
c.bot = (int)(bot*pow((double)10,(double)dis)) - b.bot;
c.exp = b.exp;
}
else
{
c.bot = bot - (int)(b.bot*pow((double)10,(double)dis));
c.exp = exp;
}
return c;
}
newDouble operator*( newDouble b )
{
(*this).SpecifyPrecision(4); // 4 ,
b.SpecifyPrecision(3);
newDouble c;
c.bot = bot * b.bot;
c.exp = exp + b.exp;
return c;
}
newDouble operator/( newDouble b )
{
if( b.bot ==0 )
{
cout << "the divider can not be zero"<<endl;
return newDouble();
}
(*this).SpecifyPrecision(7); // 8
b.SpecifyPrecision(4); // 4
newDouble c;
c.bot = bot / b.bot;
c.exp = exp - b.exp;
return c;
}
newDouble operator=( double d )
{
bot =0;
exp =0;
int flag = d>0?1:-1; //
d = abs(d); //
while( d< pow10(8)&&d!=0 ) // 8
{
d *= 10;
exp--;
}
bot = flag*(int)(d);
return *this; //
}
double ToDouble( newDouble b )
{
return (double)(b.bot)*pow((double)10, (double)(b.exp));
}
// ,
void SpecifyPrecision( int num )
{
if( bot ==0 ) return;
int flag = bot>0?1:-1;
bot = abs(bot);
while( bot < pow10(num) )
{
bot *= 10;
exp--;
}
while( bot > pow10(num+1) )
{
bot /=10;
exp++;
}
bot = flag*bot;
}
private:
int bot;
int exp;
};
ostream & operator<<(ostream &out , newDouble d )
{
out << d.ToDouble(d);
return out;
}
int main(void)
{
newDouble a(50);
newDouble b(8);
cout << "a=" << a<<endl;
cout << "b=" << b<<endl;
cout <<"a+b=" << a+b <<endl;
cout <<"a-b=" << a-b <<endl;
cout <<"a*b=" << a*b <<endl;
cout <<"a/b=" << a/b <<endl<<endl;
a = -0.5;
b = -0.03;
cout << "a=" << a<<endl;
cout << "b=" << b<<endl;
cout <<"a+b=" << a+b <<endl;
cout <<"a-b=" << a-b <<endl;
cout <<"a*b=" << a*b <<endl;
cout <<"a/b=" << a/b <<endl<<endl;
a = 2.5;
b = 4;
cout << "a=" << a<<endl;
cout << "b=" << b<<endl;
cout << "(a*b)+(b-a)+(b/a)=" <<(a*b)+(b-a)+(b/a)<<endl<<endl;
while(1)
{
double t=0;
cout << "Please input a( double ): "<<endl;
cin >> t;
a =t;
cout << "Please input b( double ): "<<endl;
cin >> t;
b =t;
cout << "a=" << a<<endl;
cout << "b=" << b<<endl;
cout <<"a+b=" << a+b <<endl;
cout <<"a-b=" << a-b <<endl;
cout <<"a*b=" << a*b <<endl;
cout <<"a/b=" << a/b <<endl<<endl;
}
return 0;
}