As the title says, let the code talk.
//feel free to use it in your project with nothing guaranteed
//mail to: bruceadi(at)hotmail.com
//to verity Double-precision floating-point format, see the link http://en.wikipedia.org/wiki/Double_precision
#include <iostream>
#include <limits>
class Double
{
public:
explicit Double(double v)
{
/*1, 11, 52*/
sign = (*(unsigned __int64*)(&v) >> 63) ? -1: 1;
unsigned __int64 t = exp = (*(unsigned __int64*)(&v) >> 52) & 0x7ff;
if(0 != t && t != 0x7ff)
{
exp =(int)(t - 1023);
}
man = *(unsigned __int64*)(&v) << 12;
}
operator double() const
{
if(!exp)
{
return 0;
}
else if(0x7ff == exp)
{
return !man ? std::numeric_limits<double>::infinity() :
std::numeric_limits<double>::quiet_NaN();
}
double e = exp;
double final = 1.0;
unsigned __int64 m = man;
while( e < 0)
{
final *= 0.5;
++e;
}
while(e > 0)
{
final *= 2.0;
--e;
}
double prec = final;
while(m)
{
prec *= 0.5;
if(m >> 63)
{
final += prec;
}
m <<= 1;
}
return final;
}
private:
int exp;
char sign;
unsigned __int64 man;
};
int main ()
{
double f[5];
f[0] = 1.0/3;
f[1] = std::numeric_limits<double>::quiet_NaN();
f[2] = std::numeric_limits<double>::infinity();
f[3] = 1e-20;
f[4] = 0.0;
for(size_t i = 0; i < 5; ++i)
{
std::cout << f[i] << " : " << (double)(Double(f[i])) << std::endl;
}
return 0;
}