Keil Logo

C51: Data Type Question


Information in this article applies to:

  • C51 Version 5.50 or later

SYMPTOMS

I have assigned a variable 'big_float' to be of data type 'double' at the beginning of my program. One problem I found is as follows:

double big_float= 5000000000;

This line does not work because the value is stored as a 4 byte value in big_float even though I have assigned it as type double. However, if I do the following:

double big_float= 5000;
big_float *= 1000000;

big_float seems to hold the correct value of 5000000000. Can you tell me why I cannot directly assign the constant number to the variable big_float?

CAUSE

Double precision is not supported, but the "double" reserved word does produce a 4 byte float variable, as you have observed. The problem arises in the fact that by declaring

big_float = 5000000000;

the constant is technically considered a long by the compiler and must be cast to a float prior to assignment. The value is too big for a long, and overflows it, generating a number which looks very little like the one intended.

RESOLUTION

The following declaration:

big_float = 5000000000.;

establishes the constant as a float through use of the decimal, prevents the cast, the overflow, and permits a clean assignment, which works out in the simulator. The declaration:

big_float = 5000;

works because that didn't overflow the long/int that the constant represented. The practical upshot of all this is: Use a decimal in your float assignment constants and everything will work.

MORE INFORMATION

SEE ALSO


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.