This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Why sizeof(double) = 2?

Hello,

I am using F28335. In my code, I find that sizeof(double) = 2?

If I define double a = 0.5, dose anyone know how is "a" stored as two bytes binary.

Thanks.

  • Hi Fei Pan,

    On the C2000 the double is a 32-bits floating point variable, that is two minimum addressable units (each being 16-bits). For a 64-bits floating point variable use "long double".

    Best Regards,

     Tobias

  • Hello Tobia,

    Thanks for your quick reply. So double is a single precision number. Since we know unsigned char occupies one byte, I have the following code:

    union {
    double d;
    unsigned char bd[4];
    } doubleConverter;

    And I find the for a single precision number 3eaa aaab ≈ 0.33. So if I do some thing like this:

    doubleConverer.bd[0] = 0x3e;

    doubleConverer.bd[0] = 0xaa;

    doubleConverer.bd[0] = 0xaa;

    doubleConverer.bd[0] = 0xab;

    Can I get doubleConverter.d = 0.33?

    Best regards

    Fei

  • Hi Fei,

    Yes, "double" is a single precision number.

    To complicate matters further, "char" is 16-bits on the C2000...

    You could probably (I haven't tested) write:

    doubleConverer.bd[0] = 0xaaab;

    doubleConverer.bd[1] = 0x3eaa;

    Best Regards,

     Tobias

  • Hello Tobias,

    Thanks for your quick reply. Do you know any type in C2000 is only 8-bits? 

    I am doing serial communication. And each time I can get 8-bits. If there is no 8-bits type in C2000, does that mean the received 8-bits will automatically be changed to 16-bits?

    Sorry for so much trouble. Thanks a lot.

    Best regards,

    Fei

  • Hi Fei,

    You're welcome!

    Unfortunately there are no 8-bits types in the C2000, 16-bits is the minimum size that can be addressed.

    Normally when storing an 8-bits value (e.g. one received from the UART) in a 16-bits variable the upper 8-bits will be zero. By using the shift operator (<<) you can store two 8-bits values in a 16-bits variable if space is a concern.

    Best Regards,

     Tobias