I am using Silicon Labs C8051F344 controller and for one of my project the floating point precision required is 6 digits after decimal point. I am now facing with the following problem:
#include <C8051F340.H> float idata flt; void main(void) { PCA0MD &= ~0x40; // (Disable Watchdog timer) flt = 105563.578453; // Float value assignment to flt while(1); }
Above is just a sample program to test the value of float variable "flt". I have observed the value of flt as 105563.6 (got rounded off) in debug mode against the expected 105563.578453. What's the problem here ???? I have also tried some more values. flt= 216.416736;_____ Value at Debug-> 216.4167 flt= 2164.167365;____ Value at Debug-> 2164.167 flt= 2164167.365214;_ Value at Debug-> 2164167 flt= 2164.300214;____ Value at Debug-> 2164.3 Now float is supposed to have a precision of 6 digits after decimal point. But the assignment is happening strangely. I searched many of the existing threads but didn't find any convincing answer. Any guidance in the matter would be of great help. Thank you in advance. Regards, Jaydeep
"Not, it's not. That's 10 decimals."
Careful there.
The word decimal can be used to either represent a digit in the decimal number system. But it is also used for the digits to the right of the decimal point.
So the OP is not wrong when claiming that the number has 6 decimals.
The problem here is how many value digits that can be represented in a floating point number of a given size.
I don't think I've ever heard "decimal" used on its own to mean that - only when specifically stated as "decimal digit" (as opposed, say, to "binary digit").
AFAIK, "decimal" used alone always means "to the right of the decimal point".
And that is clearly what the OP had in mind when he said, "float is supposed to have a precision of 6 digits after decimal point"