This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

String to Float?

Is there a string to float function lurking somewhere in the libraries? Thanks

Parents
  • The usual atof(), strtof(), most of libc is available.

    You're really converting strings to floats on a Cortex-M0 with very little memory and a 16MHz processor however? I try not even to use floating point on these chips if at all possible, it's slow and thus power inefficient and bloats the code. atof() for instance is 300 bytes, strtof is 500 bytes. That's running near 1% of your available flash with a softdevice, adds up quickly.

    Other options often include using scaled integers and converting things before you send them to the device, if whatever sending the value is more powerful.

    .. correcting the memory usage ..

    atof() itself is 300 bytes but also needs a other functions from the float/double/other libraries linked in. If you're already doing some float arithmetic you may already have some of them linked, if you don't the total code pulled in by one atof() from a base case with no other float requirements is 3Kb. strtof(), although that routine is larger at 500 bytes, requires a total of 2.4Kb of code to work.

    I didn't measure how long it takes to do one conversion, I don't think it's terribly quick.

Reply
  • The usual atof(), strtof(), most of libc is available.

    You're really converting strings to floats on a Cortex-M0 with very little memory and a 16MHz processor however? I try not even to use floating point on these chips if at all possible, it's slow and thus power inefficient and bloats the code. atof() for instance is 300 bytes, strtof is 500 bytes. That's running near 1% of your available flash with a softdevice, adds up quickly.

    Other options often include using scaled integers and converting things before you send them to the device, if whatever sending the value is more powerful.

    .. correcting the memory usage ..

    atof() itself is 300 bytes but also needs a other functions from the float/double/other libraries linked in. If you're already doing some float arithmetic you may already have some of them linked, if you don't the total code pulled in by one atof() from a base case with no other float requirements is 3Kb. strtof(), although that routine is larger at 500 bytes, requires a total of 2.4Kb of code to work.

    I didn't measure how long it takes to do one conversion, I don't think it's terribly quick.

Children
  • Mmm OK. 143K flash and 6K ram is still a lot more than I am used to working with so I am not too worried about that. The speeed of conversion may be an issue. The format of the string is always the same - GPS data (ddmm.mmmmm). Maybe I can unpack the ddmm and mmmmm bits seperately into uints and ulongs then multiply/add into a float and then divide.

  • Depends what you need to do with the data whether you need it to be a float at all. I assume that ddmm.mmmmm is degrees and decimal minutes to 5dp and also it could be dddmm.mmmmm for longitude. The resolution there is 1/100,000 th of a minute. 180 degrees is the max value which is 180 * 60 * 100000, 1 / 100000ths of a minute and that's 1,080,000,000 which is 0x405f7e00 and fits happily inside an int32_t including the sign. So you could represent all the possible values from -180 to 180 degrees with a precision of 1/100,000th minute of arc in a 32bit integer.

    If whatever calculations you're doing can be done in the unit "minutes / 100000", then you can use integer arithmetic all the way through.

    And yes then parsing that string is something you could do with a short custom piece of code which requires no libraries and about 10 single-cycle multiplies only.

  • Thanks, this is very helpful - much appreciated.

Related