-2

The question is in the title. Say, for a 64-bit machine.

I want to know how the floating number is represented because I would like to know the result of

1.002 * 10^3 - 1.000 * 10 ^3

in machine representation. Thanks for your ideas.

zell
  • 9,830
  • 10
  • 62
  • 115
  • 1
    The result of the above operation is 2. Are you looking for the `float` representation of 2? – barak manos May 22 '14 at 16:13
  • 1
    Result is `2`. As whole numbers with fewer than ~7 (float) or ~16 (double) decimal digits of precision, these can be represented exactly. – Jeff May 22 '14 at 16:14
  • Even with the [half-precision floating-point format](http://en.wikipedia.org/wiki/Half-precision_floating-point_format) it appears that these numbers are exactly representable. But since you say 64-bit machine, do you consider double-precision? – Jeppe Stig Nielsen May 22 '14 at 16:19
  • 2
    @barakmanos `1.002E3` is clearly `1002`, but `1.002 * 1E3` might not be, because 1.002 is just about a power of two and 1002 just below. It depends how the expression is interpreted (and see http://stackoverflow.com/questions/18031221/rounding-oddity-what-is-special-about-100/18036308#18036308 for a quick study of the result of multiplying an exact floating-point number by an inexact one in order to produce a product that would, if carried mathematically, arrived to a number representable exactly as a floating-point number). – Pascal Cuoq May 22 '14 at 16:54
  • 1
    @barakmanos If `1.002 * 1E3` is not `1002`, then clearly `1.002 * 1E3 - 1000.0` is not 2, since this subtraction is exact. – Pascal Cuoq May 22 '14 at 16:57
  • @PascalCuoq: Thank you for pointing that out. In any case, regardless of the specific value at hand, a simple C program can reveal the exact representation of any `float` value (as depicted in my answer below). – barak manos May 22 '14 at 17:05
  • 1
    Are you looking for the binary (64 bit) representation of a floating point number? That can be found here: kipirvine.com/asm/workbook/floating_tut.htm "1 bit for the sign, 11 bits for the exponent, and 52 bits for the mantissa". – Hans Then May 22 '14 at 17:16
  • Since 1.002 * 10^3 is not C, it is not quite clear whether 1.002e3 is meant (which is 1002 and can be stored exactly), or 1.002 * 1e3 (which is not exact, although a clever compiler might notice that 1.002e3 is meant). – Rudy Velthuis May 29 '14 at 18:27

1 Answers1

2

If you are you looking for the representation of a float value in memory, then you can write down a small C program to reveal the answer. Taking the value specified in your question, for example:

float f = (float)(1.002*1000-1.000*1000);
char* p = (char*)&f;
int   i;

// Little Endian
for (i=0; i<sizeof(f); i++)
    printf("%.2X",p[i]);

// Big Endian
for (i=sizeof(f)-1; i>=0; i--)
    printf("%.2X",p[i]);

Please note that the "%.2X" string is correspondent with CHAR_BIT being equal to 8, and should generally be set as "%.NX", with N being equal to CHAR_BIT/4.

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • 1
    The `%a` format specifier gives a slightly more readable representation. – tmyklebu May 22 '14 at 17:12
  • @tmyklebu: Thanks for the tip (as I wasn't really aware of this particular specifier). – barak manos May 22 '14 at 17:25
  • This answer is wrong as the operation is NOT necessarily 2: 1.002 * 10 ^3 cannot be exactly represented in bits. See the comments earlier from Pascal Cuoq. Therefore, I vote up but do NOT accept this answer. Thanks anyway. – zell May 26 '14 at 13:54
  • @zell: The answer tells you how to get the representation of **any** `float` value. In your question, the value is 2 or very close to it, so I used 2 as an example. Since you insist, I changed it to be the precised value specified in your question. – barak manos May 26 '14 at 13:59