'print double type data and decimal represention
#include <stdint.h>
#include <stdlib.h>
typedef struct{
uint64_t f: 23;
uint64_t e: 8;
uint64_t s: 8;
} float_struct_t;
typedef union {
float fp;
uint32_t bits;
float_struct_t bitss;
} float_bits_t;
void printBitsFloat(float_bits_t *);
int main() {
float values[17];// = {0.0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
float_bits_t x1;
x1.bitss.s = 0;
x1.bitss.e = -2+127;
x1.bitss.f = 0x4ccccd; //O prefixo 0x indica que o valor é representado em hexadecimal
int valores_e[] = {-2, -1, 0, 1, 2, -2, -3, -6, 1, 4, 5, 7};
int valores_f[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x4ccccd, 0x4ccccd, 0x23d70a, 0x0, 0x200000,
0x200000, 0x480000};
for(int i = 0; i<sizeof(values)/sizeof(float); ++i) {
x1.bitss.s = values[i];
printf("%.1e: ", x1.fp);
printBitsFloat(&x1);
printf(" Sign = %d Exp(decimal) = %3d Fraction = 0x%x=%f", x1.bitss.s, x1.bitss.e-127, x1.bitss.f);
putchar('\n');
}
}
/* Imprime o valor de cada bit, da esquerda para direita, desde o
bit firstBit (0 a 63) até ao bit lastBit (0 a 63 e não superior a firstBit)
*/
void printBits64(uint64_t v, int firstBit, int lastBit) {
uint64_t mask = 1;
if(firstBit < lastBit)
abort();
if(firstBit>0)
mask = mask << firstBit;
if(mask & v)
putchar('1');
else
putchar('0');
if(firstBit == lastBit)
return;
else {
printBits64(v, --firstBit, lastBit);
}
}
void printBitsFloat(float_bits_t *x){
printBits64(x->bits, 31, 31); putchar(' ');
printBits64(x->bits, 30, 23); putchar(' ');
printBits64(x->bits, 22, 0);
}
Hello everyone, I'm trying to make a problem where a certain code is given and ask me to add to the code some additional way to print the decimal representation of the numbers. I used %f but all the results are giving me 0.0000. What could I do wrong? Later I am also asked to add the code that allows me to store and print the double type data, how can I do that? Thank you
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
