float面试题
如何用整数来模拟float or double型类型运算。
[解决办法]
回到解放前了
[解决办法]
扩大倍数,类似于将整数拆为十进制表示.
1.5
+0.5
2.0
1.5=1+5*10^(-1)
0.5=0+5*10^(-1)
ans=1+10*10^(-1)=2
[解决办法]
高精度???
[解决办法]
用一个来做底数,一个来做指数
#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;}
[解决办法]
使用2的幂方的权。
[解决办法]