| ||||||||
Technical Support Support Resources Product Information | C51: DATA TYPE QUESTIONInformation in this article applies to:
SYMPTOMSI 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? CAUSEDouble 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. RESOLUTIONThe 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 ALSOLast Reviewed: Friday, July 15, 2005 | |||||||
| ||||||||