How to copy double data into an array on MCUXpresso IDE?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

How to copy double data into an array on MCUXpresso IDE?

773件の閲覧回数
sushmasan
Contributor II

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.

sushmasan_0-1739473747463.png

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,

sushmasan_1-1739474052528.png

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

 

0 件の賞賛
返信
2 返答(返信)

745件の閲覧回数
sushmasan
Contributor II

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.

 

0 件の賞賛
返信

756件の閲覧回数
ErichStyger
Specialist I

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

 

0 件の賞賛
返信