math precision question -- very odd

I have a double float question. The simple C routine below returns the printed value X= 64046.1090 .

With a double X, how can this be? I am using Dynamic C 10.64 and running on a BL4S160 SBC… I have tried all sorts of X values, some shorter, some longer all have precision issues…

Thanks, Al

main()
{

   double X;

         X = 64046.1100; 


       printf("%15.4f", X);


       }

The double data type in dynamic C has the same precision as the float data type (they are identical in all but name i.e. double and float are both 32 bit floats).

With 32 bit floating point values you have about 7.5 digits of usable precision but due to the nature of the floating point format you will find that many values cannot be represented precisely even if they are less than 7.5 digits. It is down to the fact that we generally use a base 10 representation to display the values but they are stored using base 2.

Regards,
Peter

Thanks Peter,
This is the answer to my question.

I thought that Dynamic C was – at least – ANSI C compliant. I did look for info on math precision in the documentation but could not find any. Can you provide me a pointer to the docs where this and other non ANSI compliant comments are listed.

Cheers

Al

The Dynamic C function reference manual states repeatedly that float and double are both 32 bit values. http://ftp1.digi.com/support/documentation/90001215_A.pdf

The other differences from ANSI C are harder to pin down but The most recent versions of DC like 10.66 have introduced many ANSI C features and made DC a good bit closer to ANSI C.

Regards,
Peter

Thanks Peter,

Yes, I see it does mention that float = double = 32 bits but only as subtle notes inside of complex math function definitions.

This fact is not stated under the general Dyn C overview (or is it?). The fact that double = float is only discussed as side notes is a cause for user confusion…

Al

Yes it is a bit odd that they don’t mention this in the main Dynamic C manual, the double type insn’t even in the keywords list!

I’m probably just used to too many embedded compilers which don’t support doubles over the years and realised very quickly that it wasn’t supported with DC :slight_smile:

Regards,
Peter

Peter
I appreciate your kindness to assist me. Your advice put me on the right path after many hours of experimenting regarding math precision. Thanks again…

Do you do consulting on Dyn C and the Rabitt SBC line?

Cheers

Al

You’re welcome.

I’m always open to proposals on the consulting side If there is a specific need you have drop me an email at peter at glasetech dot ie.

Regards,
Peter