Skip to content

Instantly share code, notes, and snippets.

@noisy
Created December 29, 2013 15:36
Show Gist options
  • Save noisy/8171558 to your computer and use it in GitHub Desktop.
Save noisy/8171558 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sstream>
#include <math.h>
#include <iomanip>
/////////////////////////////////
void format(char buf[]);
char* binrep(double d);
char* strinv(char tab[]);
char* dec2bin(unsigned int d);
int bin2dec(char tab[], int n);
/////////////////////////////////
using namespace std;
int main()
{
int i;
float f, ftmp;
double d, dtmp;
long double ld, ldtmp;
char *ctmp;
format(strinv(binrep(0.1)));
/************** float ******************/{
for(i=-1; f != 1 ; i--)
{
f = 1.0f + pow(2.0f, i);
}
i+=2;
cout<<"FLOAT----------------------------\n";
cout<<"epsilon maszynowy:\t\t 2^"<< i <<" = "<<pow(2,i)<<endl;
cout<<"Precyzja:\t\t\t "<<pow(2,i-1)<<endl;
for(ftmp=0.5, i=0; ftmp != 0 ; ftmp/=2, i--)
{
f=ftmp;
// format(strinv(binrep(f)));
// getchar();
}
cout<<"Najmniejsza dodatnia libcza:\t 2^"<<i<<" czyli "<< f <<endl<<endl;
}
/**************** double *******************/{
for(i=-1; d != 1 ; i--)
{
d = 1.0 + pow(2.0, i);
// format(strinv(binrep(pow(2,i))));
}
i+=2;
cout<<"DOUBLE---------------------------\n";
cout<<"epsilon maszynowy:\t\t 2^"<< i <<" czyli "<<pow(2,i)<<endl;
cout<<"Precyzja:\t\t\t "<<pow(2,i-1)<<endl;
for(dtmp=0.5, i=0; dtmp != 0 ; dtmp/=2, i--)
{ d=dtmp;
// format(strinv(binrep(d)));
}
cout<<"Najmniejsza dodatnia libcza:\t 2^"<<i<<" czyli "<< d <<endl<<endl;
}
/******************* long double ***********/{
for(i=-1; ld != 1 ; i--)
ld = 1+ pow((long double)2, i);
i+=2;
cout<<"LONG DOUBLE----------------------\n";
cout<<"epsilon maszynowy:\t\t 2^"<< i <<" czyli "<<pow(2,i)<<endl;
cout<<"Precyzja:\t\t\t "<<pow(2,i-1)<<endl;
for(ldtmp=0.5, i=0; ldtmp != 0 ; ldtmp/=2, i--)
{
ld=ldtmp;
format(strinv(binrep(ld)));
}
cout<<"Najmniejsza dodatnia libcza:\t 2^"<<i<<" czyli "<<setprecision(16445)<< ld <<endl;
}
getchar();
return 0;
}
/***************************************************/
char* binrep(double d){
char* c = (char*)(&d);
char *buffer=new char[sizeof(d)*8+1];
for(int i=0;i<sizeof(d);++i)
{
for(int j=0;j<8;++j)
{
char c2 = *(c+i);
buffer[i*8+j]=c2&(1<<j)?'1':'0';
}
}
buffer[sizeof(d)*8]='\0';
return buffer;
}
void format(char buf[]){
const int BIAS=16383;
int tmp;
cout<<"\ns³ mantysa \t³ cecha\n";
cout<<"ÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n";
cout<<buf[0]<<"³ "; for(int i=1; i<18; cout<<buf[i++]); cout<<"\t³ "; for(int i=16; i<80; cout<<buf[i++]); cout<<endl;
cout<<((buf[0]=='0')?'+':'-' )<<"³ "<<(tmp=bin2dec(&buf[1],16))<<"-"<<BIAS<<"="<<tmp-BIAS; cout<<"\t³ "<<(tmp==0?"0":"1")<<"+"<<( float( bin2dec(&buf[16],64) ) / pow(2,64) )<<endl;
}
char* strinv(char tab[]){
int n=strlen(tab);
for(int i=0; i<n/2; i++)
swap(tab[i], tab[n-i-1]);
return tab;
}
int bin2dec(char tab[], int n){
int suma=0;
for(int i=0; i<n;i++)
suma+=(tab[n-i-1]=='1')?pow(2,i):0;
return suma;
}
char* dec2bin(unsigned int d){
stringstream s;
do{s<< d%2;}while(d/=2);
char *tab=new char[s.str().size()+1];
strcpy(tab,s.str().c_str());
tab[s.str().size()]='\0';
return strinv(tab);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment