sprintf() convert float value to string

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

sprintf() convert float value to string

4,750 Views
xianwu
Contributor I

I add following code  to my main(), when run it. code stuck.

float ft = 3.3;

sprintf( str, "%0.2f\n\r", ft );
UART0_putString( str );

I read the Solved: sprintf not functioning correctly - NXP Community
but my MDK, uVision IDE, don't have Project > properties > C/C++ Build > Setiings > Tool Settings > MCU C Compiler > Preprocessor.

but have project/options/C/C++/Preprocessor/.

I set PRINTF_FLOAT_ENABLE=1 SCANF_FLOAT_ENABLE=1 CR_INTEGER_PRINTF=1 in Preprocessor. issue won't gone.

0 Kudos
Reply
8 Replies

4,621 Views
xianwu
Contributor I

thank you all for help.

I figured it out. in Keil IDE , Project/option for target,  seeting select MicroLIB option. 

0 Kudos
Reply

4,682 Views
xianwu
Contributor I

When I add sprintf(str, "%f", x); 

following info showed in the buildt map file. 

when x =5.5;  it prints out 5y500000. it's partially works. 

main.o(i.main) refers to _printf_f.o(.ARM.Collect$$_printf_percent$$00000003) for _printf_f
main.o(i.main) refers to printf1.o(x$fpl$printf1) for _printf_fp_dec

main.o(i.main) refers to f2d.o(.text) for __aeabi_f2d

0 Kudos
Reply

4,646 Views
frank_m
Senior Contributor III

> following info showed in the buildt map file. 

To check the map file output would have been one of my suggestions. Many toolchains can output cross reference lists as well, including details where a symbol (e.g. function) originates from.
But as said, I don' have the ARM toolchain at ma disposal to test it. In fact I have very little experience with MDK.

Have you tried tutorials and documentations ?
I suppose the linker and library options are well documented.

And, at least in POSIX compatible platforms, the printf format character 'f' denotes a double argument, not a (single precision) float argument : https://linux.die.net/man/3/printf

By the way ...
in this expression "float ft = 3.3;", the constant "3.3" represents a double, not a float.
The stated assignment involves an implicit type conversion (from double to float), which has very little effect in your case.
You can avoid that by adding the specific suffix, i.e. "float fp = 3.3f;".
Only in more extensive floating point calculations like spectral algorithms or tight cycle-by-cycle control loops, it can make a substantial difference.

0 Kudos
Reply

4,714 Views
xianwu
Contributor I

this is my IDE info, I'm using ARMCC compiler. 
IDE-Version:
µVision V5.10.0.2
Copyright (C) 2014 ARM Ltd and ARM Germany GmbH. All rights reserved.

License Information:
xian localadmin
wanco
LIC=----

Tool Version Numbers:
Toolchain: MDK-Lite Version: 5.10.0.0
Toolchain Path: C:\Keil_v5\ARM\ARMCC\bin\
C Compiler: Armcc.Exe V5.04.0.49 [Evaluation]
Assembler: Armasm.Exe V5.04.0.49 [Evaluation]
Linker/Locator: ArmLink.Exe V5.04.0.49 [Evaluation]
Librarian: ArmAr.Exe V5.04.0.49 [Evaluation]
Hex Converter: FromElf.Exe V5.04.0.49 [Evaluation]
CPU DLL: SARMCM3.DLL V5.10.0.0
Dialog DLL: DARMCM1.DLL V1.11.00.0
Target DLL: CMSIS_AGDI.dll V1.18.4.0
Dialog DLL: TARMCM1.DLL V1.08.00.0

and the compile option is :

-c --cpu Cortex-M0+ -D__EVAL -g -O0 --apcs=interwork --split_sections -I..\..\Project_Source_Code\peripherals_lib\inc -I..\..\Project_Source_Code\utilities_lib\inc -I..\..\Project_Source_Code\common\inc -I..\..\Project_Source_Code\project\inc -DUSE_SERIAL_DEBUG
-I C:\Keil_v5\ARM\RV31\INC
-I C:\Keil_v5\ARM\PACK\ARM\CMSIS\3.20.4\CMSIS\Include
-I Device\Include
-o ".\Objects\*.o" --omf_browse ".\Objects\*.crf" --depend ".\Objects\*.d"

0 Kudos
Reply

4,698 Views
frank_m
Senior Contributor III

In your case, the linker options would be equally important.
Find out what clib you application is linked against (clib*.a), and if it contains printf floating point support.
Clib support and library selection depends very much on the toolchain.

0 Kudos
Reply

4,692 Views
xianwu
Contributor I

I checked in my toolchain folder. did not clib*.a or clib*. 

the toolchain is from ARM, called ARMCC. 

from the IDE, it's hard to tell which lib is used. and it is weird that the toolchain proved by ARM has issue with sprintf().  

Regards

Xian

0 Kudos
Reply

4,731 Views
Pavel_Hernandez
NXP TechSupport
NXP TechSupport

Hello, I research more information you could change the version of the ARM compiler, enable this option in µVision under Project - Options for Target - Code Generation - V6.18. -mfloat-abi=name

 ARM Options (Using the GNU Compiler Collection (GCC))

Let me know if you have more questions.

Best regards,
Pavel

0 Kudos
Reply

4,726 Views
frank_m
Senior Contributor III

> Hello, I research more information you could change the version of the ARM compiler, enable this option in µVision under Project - Options for Target - Code Generation - V6.18. -mfloat-abi=name

Problems with printf() - like functions are usually not related to the ABI. Those only influence operations on floating point values.

I suspect the librarcy (clib). Any version with "nano" in it's name supports no floating point formats in printf functions at all.

0 Kudos
Reply