Arm-none-eabi-gcc: print a floating number using printf

I am writing a C program for the cortex-M3 microcontroller for SAM3N. When I try to print floating point numbers, it just prints "f". Example: printf("%f",43.12); prints only f , not 43.12 .

But integer printing works fine.

How to enable full float printing? I know that the compiler turned off floating point printing by default in order to reduce the size of the code (i.e., it looks like they linked the cut version). Also note that the makefile does not use CFLAGS=-Dprintf=iprintf .

Tool Details:

  • ARM / GNU C compiler: (crosstool-NG 1.13.1 - Atmel build: 13) 4.6.1
  • The above tool comes with Atmel 6.0.
+6
source share
3 answers

Can you try adding the option below in the linker settings

-lc -lrdimon -u _printf_float

and he worked for me in ARM-CORTEXM0

+6
source

It may be that your platform / libs does not support the %f format specifier for printf/sprintf . As a first approach, you can roll your own printf for float / double:

 void printDouble(double v, int decimalDigits) { int i = 1; int intPart, fractPart; for (;decimalDigits!=0; i*=10, decimalDigits--); intPart = (int)v; fractPart = (int)((v-(double)(int)v)*i); if(fractPart < 0) fractPart *= -1; printf("%i.%i", intPart, fractPart); } 
+1
source

In compilers, where floats are disabled by default, it is unusual for libraries to handle them by default. Browse your links and you will find information on how to recompile the corresponding libraries with float support or (more likely) where to find the version that was already built with it.

0
source

Source: https://habr.com/ru/post/926813/


All Articles