We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I am using the double data type to store a calibration constant. I tried also with the float data type. To form the value, I read from memory decimal digits multiplied by its corresponding decimal:
to form 23.86108 I read from memory 2, 3, 8, 6, 1, 0 and 8
2 x 10 + 3 x 1 + 8 x 0.1 + 6 x 0.01 + 1 x 0.001 + 0 x 0.0001 + 8 x 0.00001
This works fine until I add a decimal digit more. It seems to have no influence.
23.86108 + 5 x 0.000001 = 23.86108 instead of 23.861085
I search on "Options for target" to see if there is a limitation, but does not find any. (" bits to round for float compare" has no effect ). Can anyone help me with this problem?. Perhaps it is something basic, but I can not solve. Thank you.
Did you go the other route - look in the compiler manual about the size of different data types? A float has a limited resolution. And the manual would also give you a hint about the use of double with C51.
Consider checking out fixed-point arithmetic.
As far as I understand, should be within the range supported by the compiler. Thanks
What are you basing that on? Have you made a mathematical analysis of single-precision floats, or maybe browsed the issue?
A floating point number contains mantissa and exponent. How large numbers can you store in 24 bits? Each decimal digit consumes just over 3 bits. If you take a bit closer look at the problem, you'll notice that there are about 7.2 decimal digits that can fit.
Next thing is that when you do math with floating point numbers, you introduce rounding errors in the last bits, which means that when printing, you don't want to try to print every single bit. That would just present the reader with false precision.
As indicated you need to look in to how floating point numbers work. It is not like a calculator. If you need something more precise try long integers. that gives you up to 4294967296.