1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <math>
#include <iostream>
using namespace std;
struct Reel
{
union
{
struct
{
unsigned Mantisse : 23;
unsigned Exposant : 8;
unsigned Signe : 1;
};
unsigned Representation;
};
};
void AffichageBinaire(unsigned int n, int Chiffres)
{
for (int i = Chiffres - 1 ; i >= 0 ; i--)
cout << ((n & (1 << i)) ? 1 : 0);
}
int main()
{
float f = 0.487654;
Reel r = *((Reel*)&f);
cout << "Nombre Reel : " << f << endl << endl;
cout << "Representation binaire : ";
AffichageBinaire(r.Representation, 32);
cout << endl;
cout << "Sign : " << r.Signe << (r.Signe ? " (Negatif)" : " (Positif)") << endl;
cout << "Normalized Significand : ";
AffichageBinaire(r.Mantisse, 23);
cout << " (" << r.Mantisse << ") " << endl;
cout << "Biased Exponent : ";
AffichageBinaire(r.Exposant, 8);
cout << " (" << r.Exposant << ") " << endl << endl;
cout << "Exposant Reel : " << r.Exposant - 127<< endl;
float Exposant = ((r.Exposant - 127.0 - 23) * (M_LN2 / M_LN10));
int e = (int)Exposant;
if (e < 0)
e--;
float s = Exposant - e;
cout << "Exposant base 10 : " << Exposant << endl;
cout << "Exposant Entier : " << e << endl;
cout << "Exposant Reste : " << s << endl << endl;
int Mantisse = (r.Mantisse + 0x000800000) * pow(10, s);
cout << "Mantisse base 10 : " << Mantisse << endl << endl;
getchar();
return 0;
} |
Partager