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
You shall read this: en.wikipedia.org/.../Floating_point or this: docs.sun.com/.../data.represent.html
What makes you say that?
It's not what the C51 manual says, is it: http://www.keil.com/support/man/docs/c51/c51_ap_floatingpt.htm
It's not what the C51 manual says
Wikipedia links to the same IEEE standard. And the sun document uses the same representation (see TABLE A-6).
So where is the difference?
Since it seems like the Author does not know, how float values are represented in memory, the websites should be quite interesting for him.
Stefan,
You misunderstand!
I was referring to the OP's statement, "float is supposed to have a precision of 6 digits after decimal point" - not to your links!
That's why I used that statement as the subject for my post!
Since a floating point number is a number where the decimal point (or exponent) floats around, it can never have a fixed number of decimals. It has a fixed amount of precision. Sometimes that precision is 100% allocated to the integer part. Sometimes that precision is 100% allocated to the fraction.
Even with double precision, you would get into troubles with some of your examples. Time for you to consider the use of fixed-point arithmetic.
I need the solution to the problem i.e I want an assignment of 2164.167365 as it is upto 6decimals and no round offs or break in the digits after decimal point. Even the assignment in floating point format of 2.164167365e+3 doesn't work out.
The problem is that your are asking C51 to do something beyond its specification!
If you buy a car with a top speed of 95, it's no use complaining that it won't do 150!!
Look again at the specification: http://www.keil.com/support/man/docs/c51/c51_ap_floatingpt.htm
It tells you clearly what precision you can expect from the C51 'float' type
"the assignment in floating point format of 2.164167365e+3 doesn't work"
It works within the specification of C51!
If you really do need more precision, then you will have to obtain (or write) a library to handle it.
"If you buy a car with a top speed of 95, it's no use complaining that it won't do 150!!"
There are solutions to a lot of tough problems. Make use of a C130 Hercules and make an air drop of the car :)
In the case of the C51, I would recommend a bigger processor if needing to implement double-precision floating point. And while switching processor/compiler, it would be even simpler to select a processor/compiler combination that does support double precision (and maybe 10/12-byte extended precision).
You really have to understand, that you have access to about 7 value digits. With 6 of them after the decimal point, there can be only one to the left of the decimal point. If you have four before the decimal point, then you only have three left for decimals.
With double-precision (not available in C51) you get just below 16 value digits.
The alternative is to use fixed-point arithmetic and use suitably large integers for the computations.
I need the solution to the problem
No. You need to grasp that so far you don't even understand what the problem is, first.
2164.167365 as it is upto 6decimals
Not, it's not. That's 10 decimals. The root cause of your misunderstanding is that your idea of what counts as a "decimal" is wrong.
"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"