ARM Technical Support Knowledge Articles

DATA TYPE QUESTION

Applies to: C51 C Compiler

Answer


Information in this article applies to:


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 in 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

Article last edited on: 2005-07-15 10:36:51

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential