Using floats with sprintf, gcc-arm-none-eabi, nRF51822

eliotstock gravatar image

asked 2014-02-17 14:52:39 +0100

updated 2014-02-17 15:54:26 +0100

I want to use some floating point variables in my code and format a string with them in. These are latitude and longitude values from a GPS which I'm then sending to a GSM modem. I'm using the gcc-arm-none-eabi toolchain with the unofficial "pure-gcc" Makefiles.

typedef struct
    float latitude;
    float longitude;
    float altitude;
    float accuracy;
} position_t;
    memset(&at_command, 0, sizeof(at_command));
    sprintf(at_command, "AT+UHTTPC=0,5,\"/d/position\",\"position.ffs\",\"d=%s&lat=%f&lng=%f\",0\r",
            p_device_id, p_position->latitude, p_position->longitude);
    gsm_at((uint8_t *)at_command, gsm_response);

I read somewhere that because the M0 has no floating point unit, in order to use floats I need to use a build flag to emulate them in software like so:


(See http://gcc.gnu.org/onlinedocs/gcc/ARM...)

However, I notice my code builds and runs without this.

The docs for my libc (gcc-arm-none-eabi-4_8-2013q4/share/doc/gcc-arm-none-eabi/html/libc/sprintf.html in your filesystem, if you use gcc-arm-none-eabi) list all format specifiers, but then says this.

Depending on how newlib was configured, not all format specifiers are supported.

I notice some other people are simply hacking their floats into two decimals and printing those independently instead, eg:


That's an option, I guess, but I'm interested in avoiding it if possible.

What I'm seeing when debugging is that the float values are there (even without the -mfloat-abi=soft flag, strangely), but the output string from sprintf is missing the values.

How do I build with support for floats in sprintf?

edit retag flag offensive close delete report spam


5 answers

Sort by » oldest newest most voted
eliotstock gravatar image

answered 2014-02-17 15:54:14 +0100

updated 2014-02-17 15:54:26 +0100

The trick seems to be to add this linker flag to your Makefile:

LDFLAGS += -u _printf_float

Looks like "printf" here also applies to sprintf.

edit flag offensive delete publish link more


great find!

Mahesh Venkitachalam ( 2015-10-05 08:18:58 +0100 )editconvert to answer

you saved me so much time. Thanks!

Luis Rodenas ( 2015-10-23 13:44:47 +0100 )editconvert to answer
EarthLord gravatar image

answered 2014-02-17 15:14:14 +0100

Hi Eliot, Printing floats is working for me with GNU GCC 4.8.3 in nrf51822. I am using printf as shown by a kind person here. For example

    int i = 7;
    float ab = 1.2;
    printf("A printf test: %.3f %d\n", ab, i);

gives an output of

A printf test: 1.200 7

Try this. As far as I know, I don't see anything special related to floats in my makefile. PS. Cool start-up. All the best! Prithvi

edit flag offensive delete publish link more

Thanks! There was in fact a linker flag in the Makefile in that project which did the trick. Posting it below.

Eliot Stock ( 2014-02-17 15:52:57 +0100 )editconvert to answer
EarthLord gravatar image

answered 2014-02-17 16:06:58 +0100

nargetdev gravatar image

updated 2015-08-19 03:09:20 +0100

Strange. I don't have that flag. Well, its fine in any way as long as it works.

edit flag offensive delete publish link more
cdhmanning gravatar image

answered 2015-01-27 22:16:39 +0100

Just a side comment to what you're trying to do...

Floats only give about 7 digits of precision. That will give you a worst case precision of about 2 metres .

edit flag offensive delete publish link more


Fine for my use case but useful to know, thanks.

Eliot Stock ( 2015-01-27 22:21:43 +0100 )editconvert to answer
Pher gravatar image

answered 2015-01-27 13:58:50 +0100

updated 2015-01-28 11:26:25 +0100

Hi Elliot, do you now if it is possible to do the same thing on KEIL MDK?? By now, i cant use printf functions with floats inside! Every time i try to use it, in the firsts prints i have always the result of 0.000000 and the program crashes a little bit after with an hard fault handler!


edit flag offensive delete publish link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

Question Tools

1 follower


Asked: 2014-02-17 14:52:39 +0100

Seen: 9,737 times

Last updated: jan. 28 '15