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

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:

-mfloat-abi=soft

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:

http://stackoverflow.com/questions/90...

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 close delete

Sort by » oldest newest most voted

LDFLAGS += -u _printf_float

Looks like "printf" here also applies to sprintf.

more

great find!

( 2015-10-05 08:18:58 +0200 )editconvert to answer

you saved me so much time. Thanks!

( 2015-10-23 13:44:47 +0200 )editconvert to answer

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

more

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

( 2014-02-17 15:52:57 +0200 )editconvert to answer

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

more

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 .

more

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

( 2015-01-27 22:21:43 +0200 )editconvert to answer

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!

Regards

more

[hide preview]

## Recent blog posts

Posted 2017-04-11 11:49:17 by Radosław Koppel
• ### Taking a deeper dive into Bluetooth 5

Posted 2017-04-07 14:53:51 by John Leonard
• ### Logging enabled by default in SDK Bluetooth examples

Posted 2017-03-29 11:27:02 by Håvard
• ### Monitor Mode Debugging with J-Link and GDB/Eclipse

Posted 2017-03-29 05:56:12 by Daniel Veilleux
• ### Getting started with Nordic's Secure DFU bootloader, a step by step guide

Posted 2017-03-20 17:13:52 by Hung Bui

## Recent questions

• ### nrf51822 - jlink 0.76V

Posted 2017-04-23 23:10:29 by normanfust
• ### Installing micro-ecc issue

Posted 2017-04-23 22:06:28 by dc
• ### BLE Advertising, a beginner's tutorial

Posted 2017-04-23 20:59:29 by chocol8
• ### Softdevice memory Consumption

Posted 2017-04-23 17:51:46 by yash
• ### how to flash programs on nrf51822 via nrf51 DK

Posted 2017-04-23 14:52:41 by krt