Created
November 9, 2018 15:59
-
-
Save ProfAndreaPollini/7694f55e46d07ca95f5ce65aa7763526 to your computer and use it in GitHub Desktop.
Codice per l'analisi di un numero floating point 32 bit codificato IEEE754 (funziona solo con i numeri "giusti")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
/* | |
INTERPRETZIONE DI UN UNSIGNED INT INSERITO COME NUMERO ESADECIMALE COME | |
NUMERO FLOATING POINT IEEE754 (Esercizio) | |
ROADMAP: | |
[] stampo i bit che compongono la variabile (show_bits_r) | |
[] estraggo il segno (creo una funzione) | |
[] estraggo l'esponente (dal main di ieee.c) | |
[] estraggo la mantissa | |
[] Nan? Infty? Zero? | |
[] denormalizzato? -> valore mantissa denormalizzata | |
[] normalizzato? -> calcolo mantissa (come ieee.c) | |
*/ | |
int segno(unsigned int); | |
int esponente(unsigned int); | |
int mantissa(unsigned int); | |
int isNan(int s, int e, int m); | |
int isInfty(int s, int e, int m); | |
int isZero(int s, int e, int m); | |
int isNormalized(int s, int e, int m); | |
double CalcNormalized(int s, int e, int m); | |
double CalcDenormalized(int s, int e, int m); | |
int main() | |
{ | |
unsigned int numero; | |
printf("\nN = "); | |
scanf("%x", &numero); | |
printf("\nN = %d\n", numero); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
typedef unsigned char *p_byte; | |
void show_bytes(p_byte start, size_t len) | |
{ | |
int i; | |
for (i = 0; i < len; i++) | |
printf(" %x", start[i]); | |
printf("\n"); | |
} | |
void show_bits(p_byte start, size_t len) | |
{ | |
int i, j; | |
for (i = 0; i < len; i++) | |
{ | |
for (j = 0; j < sizeof(start[0]) * 8; j++) | |
printf("%d", (start[i] >> j) & 0x01); | |
printf(" "); | |
} | |
printf("\n"); | |
} | |
#include <math.h> | |
double calc_mantissa(p_byte start, size_t len) | |
{ | |
int i, j; | |
short pos; | |
double exp_v, v, mantissa_value = 1.0; | |
for (i = len - 1; i >= 0; i--) | |
{ | |
for (j = sizeof(start[0]) * 8 - 1; j >= 0; j--) | |
{ | |
pos = i * 8 + j; | |
if (pos < 23) | |
{ | |
exp_v = -(23 - pos); | |
v = (start[i] >> j) & 0x01; | |
mantissa_value += v * pow(2.0, exp_v); | |
} | |
} | |
//printf(" "); | |
} | |
return mantissa_value; | |
} | |
void show_mantissa(p_byte start, size_t len) | |
{ | |
int i, j; | |
short pos; | |
for (i = len - 1; i >= 0; i--) | |
{ | |
for (j = sizeof(start[0]) * 8 - 1; j >= 0; j--) | |
{ | |
pos = i * 8 + j; | |
if (pos < 23) | |
{ | |
printf("%d", (start[i] >> j) & 0x01); | |
if (pos % 4 == 0) | |
printf(" "); | |
} | |
} | |
//printf(" "); | |
} | |
printf("\n"); | |
} | |
void show_bits_r(p_byte start, size_t len) | |
{ | |
int i, j; | |
for (i = len - 1; i >= 0; i--) | |
{ | |
for (j = sizeof(start[0]) * 8 - 1; j >= 0; j--) | |
printf("%d", (start[i] >> j) & 0x01); | |
printf(" "); | |
} | |
printf("\n"); | |
} | |
void show_float(float f) | |
{ | |
show_bits_r((p_byte)&f, sizeof(float)); | |
} | |
void show_pointer(void *pointer) | |
{ | |
show_bytes((p_byte)&pointer, sizeof(void *)); | |
} | |
void decode_float(float f) | |
{ | |
p_byte start = (p_byte)&f; // cast float -> unsigned char* | |
unsigned long ULTIMA_POSIZIONE = sizeof(float) - 1; | |
unsigned int MSB_BYTE = start[ULTIMA_POSIZIONE]; | |
//printf("\n%d\n", MSB_BYTE >> 7); | |
int segno = (MSB_BYTE >> 7) & 0x01; | |
//printf("\n%d\n", MSB_BYTE << 1); | |
int exponent = (MSB_BYTE << 1) | ((start[ULTIMA_POSIZIONE - 1] >> 7) & 0x1); | |
exponent = exponent & 0xFF; // exponent & 0b11111111 | |
printf("EXPONENT ->"); | |
show_bits_r((p_byte)&exponent, sizeof(int)); | |
int real_exponent = exponent - 127; | |
printf("esponente = ( %d | %d ) ", exponent, real_exponent); | |
//show_bits((p_byte)&exponent, 1); | |
show_bits_r((p_byte)&exponent, 1); | |
int mantissa = start[ULTIMA_POSIZIONE - 1] << 16; | |
mantissa = mantissa | start[ULTIMA_POSIZIONE - 2] << 8; | |
mantissa |= start[ULTIMA_POSIZIONE - 3]; | |
printf("\n mantissa = ( %d )", mantissa); | |
show_bits_r((p_byte)&mantissa, sizeof(int)); | |
show_mantissa((p_byte)&mantissa, sizeof(int)); | |
printf("\nVALORE = %f\n", pow(-1, segno) * pow(2, real_exponent) * calc_mantissa((p_byte)&mantissa, sizeof(int))); | |
} | |
int main() | |
{ | |
float numero; | |
int i; | |
printf("Inserisci un numero "); | |
scanf("%f", &numero); | |
printf("\nnumero = %f\n", numero); | |
show_float(numero); | |
decode_float(numero); | |
//for (i = 0; < sizeof(float) * 8; i++) | |
// printf("%d ", numero & 0x01); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment