Skip to content

Instantly share code, notes, and snippets.

@fjc-oai
Created November 5, 2014 03:53
Show Gist options
  • Save fjc-oai/69664e3d6ca5c2ce2f0c to your computer and use it in GitHub Desktop.
Save fjc-oai/69664e3d6ca5c2ce2f0c to your computer and use it in GitHub Desktop.
string convert(string &decimal){
int p=decimal.size();
for(int i=0;i<decimal.size();i++)
if(decimal[i]=='.'){
p=i;
break;
}
int n1=atoi(decimal.substr(0,p).c_str());
double n2=atof(decimal.c_str())-n1;
string s1,s2;
while(n1>0){
s1=(n1%2==0?"0":"1")+s1;
n1/=2;
}
while(n2>0){
if(s2.size()>10)
break;
n2*=2;
if(n2>=1){
s2+="1";
n2=n2-1;
}else{
s2+="0";
}
}
if(s2.empty())
return s1;
else
return s1+"."+s2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment