|
|||||||||||
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:
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:
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 by declaring
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:
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:
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: Thursday, February 25, 2021 | ||||||||||
|
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.