Hello Team,
The questions seems to be very basic, I know.
I am trying to copy a double value into a character array, I tried sprintf and snprintf, those didn't work.
Finally I have used gcvt that is working as expected.
But when I checked my environment, gcvt was commented out in stdlib.h under a specific macro.
Because of this, when I compiled the code, I am getting a warning message,
implicit declaration of function 'gcvt' [-Wimplicit-function-declaration]
This is the IDE version I am using currently,
In my application, I have to compare to perform a firmware comparison, that's why trying to convert a double into a string, so that string comparison is easy. (Note: Initially I tried double comparison, but the results are unpredictable)
Example:
double a = 4.4000;
char arr[15] ;
sprintf(arr,"%f",a); // arr contains full of zero's, even tried with "%lf" also.
snprintf(arr,4,"%f",a); // behavior is same as above
gcvt(a,4,arr); // arr contains the 4.45
Even I googled for gcvt, looks like it is deprecated and I can't use it.
Can someone please let me know, what is missing and why I can't use sprintf?
Regards,
San
Hi @ErichStyger ,
Thanks for sharing the info.
There is a reason for comparing two strings.
I am working on a project, where there are 2 separate devices (Example: DevA, DevB) and there is a serial communication interface used between those.
There is a fixed firmware version, which is let's say "3.4" (string), which is always compared with what is the firmware in DevB.
Before sending a data from DevA to DevB, initially there is a firmware comparison done.
Get the DevB firmware version (Which is a double value).
Since the fixed value is a string, due to this reason I am trying to convert the double to string. Hope you understand it better.
Also I am using sprintf not printf, in your response you mentioned printf.
The library I am using is -> Newlib (Semihost), for which "Enable Printf/scanf float" options are disabled.
Hi San,
Converting floating point numbers into strings for comparison will not help you. I recommend you have a read at https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ and the related articles on that subject.
About printf() and the like: what library are you using? Keep in mind that for newlib-nano you have to enable float/double support for the library, see https://mcuoneclipse.com/2014/07/11/printf-and-scanf-with-gnu-arm-libraries/ and https://community.nxp.com/t5/MCUXpresso-IDE/Printf-won-t-print-floats/m-p/1093043 .
The reason is about code/data overhead involved. A comparison of different library options is here: https://mcuoneclipse.com/2023/01/28/which-embedded-gcc-standard-library-newlib-newlib-nano/
I hope that helps,
Erich