3

For example the given program

FILE *f = fopen("test.txt","w");
double d = 5.2;
fwrite(&d, sizeof(typeof(d)), 1, f);
fclose(f);

when U use xxd to view the content I get

//binary
0000000: 11001101 11001100 11001100 11001100 11001100 11001100  ......
0000006: 00010100 01000000                                      .@
//hex
0000000: cdcc cccc cccc 1440                      .......@

but I found How to represent FLOAT number in memory in C that it should look like

0 10000001 01001100110011001100110

so I am not sure if its the same binary in memory and file or I am completely wrong. How can i determine the float value from given example?

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Yes, you can write the value to a file and view it. But part of the problem is that you're confusing `float` and `double`. Both are floating point values. But `float` is typically 32 bits whereas `double` is 64 bits. And they have different encodings. So try changing `double d = 5.2` to `float d = 5.2`. – user3386109 Feb 11 '19 at 23:43
  • Also, you don't need `typeof`. `sizeof(d)` will do just fine. – user3386109 Feb 11 '19 at 23:44
  • in addition to what @user3386109 depending on the endianess of the processor architecture you're using you might need to read the octets back to front, – PeterT Feb 11 '19 at 23:45
  • Jes i got it using float. But im confused about 64 bit. In my example using double are 64 bits, am i wrong? –  Feb 11 '19 at 23:51

2 Answers2

2

The bit pattern you show, 0 10000001 01001100110011001100110, is for an IEEE-754 basic 32-bit binary floating-point representation, often used for float. You request the representation of a double, which often uses IEEE-754 basic 64-bit binary.

The bytes you show, cdcc cccc cccc 1440, are a representation of approximately 5.2, in a little-endian order. In the high-value byte, the first bit, in position 0x80, is zero. It is the sign bit, and zero means positive. The next seven bits, 0x40, and the four bytes from the next byte, the 1 of 0x14, are the exponent. Together, they are 0x401. The exponent is biased by 0x3ff. So, with an exponent encoded as 0x401, the actual exponent is 2, meaning 22.

The remaining bits encode the trailing bits of the significand. They are 0x4cccccccccccd. For normal numbers, the significand is put after a “radix point” (the general equivalent of a decimal point) with 1 before the point: 1.4cccccccccccd16. In decimal, that is approximately 1.3 (exactly 1.3000000000000000444089209850062616169452667236328125).

Together, the value is + 22 • 1.3000000000000000444089209850062616169452667236328125 = 5.20000000000000017763568394002504646778106689453125.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

If you want to know how the 32-bit float specifically relates to the 64-bit float you can see the elements of the sign-bit, mantissa and exponent match up.

//Your double in little endian
11001101 11001100 11001100 11001100 11001100 11001100 00010100 01000000    

//for better readability in big endian
01000000 00010100 11001100 11001100 11001100 11001100 11001100 11001101 

//the 32-bit float from the answer you linked
01000000 10100110 01100110 01100110

//1 bit sign  8/11-bit exponent   23/52-bit mantissa
0             10000000 001      01001100 11001100 11001100 11001100 11001100 11001100 1101
0             10000001          01001100 11001100 1100110

Why the value of the exponent and mantissa are the way the are is explained better in the answer you linked and elsewhere than I could do from the top of my head

PeterT
  • 7,981
  • 1
  • 26
  • 34