-1

protocol

How 0x3333 0x41DF could be 27.9 value?

I use hexadecimal to decimal value but it was 1B.E6666666666666666666.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198

1 Answers1

1

0x3333 0x41DF combined = 0x41DF3333. As a 32 bit IEEE float, this is evaluated as follows:

Convert to binary:

0x41DF3333 = 
4    1    D    F    3    3    3    3 = 
0100 0001 1101 1111 0011 0011 0011 0011 = 
01000001110111110011001100110011

Evaluate as a 32 bit IEEE float:

01000001110111110011001100110011 =


Sign (s)  Exponent (e)               Significand (S)
                                         11111111112222
                                12345678901234567890123
----      --------------------  -----------------------
0         10000011=1+2+128=131  10111110011001100110011

     s         e        assumed 1   S1          S2          S3                 S21           S22           S23   
(-1)^0 x 2^(131-127) x (1         + 1 x 1/2^1 + 0 x 1/2^2 + 1 x 1/2^3  + ... + 0 x 1/2^21  + 1 x 1/2^22  + 1 x 1/2^23)           =
1      x 2^( 4     ) x (1 x 1/2^0 + 1 x 1/2^1 + 0 x 1/2^2 + 1 x 1/2^3  + ... + 0 x 1/2^21  + 1 x 1/2^22  + 1 x 1/2^23)           = 
                       (1 x 2^4   + 1 x 2^3   + 0 x 2^2   + 1 x 2^1    + ... + 0 x 1/2^17  + 1 x 1/2^18  + 1 x 1/2^19)           = 
                       (1 x 2^23  + 1 x 2^22  + 0 x 2^21  + 1 x 2^20   + ... + 0 x 1/2^2   + 1 x 1/2^1   + 1 x 1/2^0 ) / 2^19    = 
                       (1           1           0           1                  0             1             1)          / 524288  =
                       (110111110011001100110011                                                            )          / 524288  =
                       (1101 1111 0011 0011 0011 0011                                                       )          / 524288  =
                       (14299955                                                                            )          / 524288  =
27.899999618530273

The last calculation used a calculator. Noting that tha last non-zero digit after the decimal point, 3, is not 5, we know this is not the exact value. The following code gets the exact float value:

Mac_3.2.57$cat floatGet.c
#include <stdio.h>

int main(void){
    union Data {
        int i;
        float f;
    }data;

    data.i = 0x41DF3333; 

    printf("%100.100f\n", data.f);

    return 0;
}
Mac_3.2.57$cc floatGet.c
Mac_3.2.57$./a.out 
27.8999996185302734375000000000000000000000000000000000000000000000000000000000000000000000000000000000
Mac_3.2.57$
Andrew
  • 1
  • 4
  • 19